Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Christophe Raffalli <Christophe.Raffalli@univ-savoie.fr>
To: Steve Stevenson <steve@cs.clemson.edu>
Cc: caml-list@inria.fr
Subject: Re: Imperative list operations
Date: Thu, 16 Sep 1999 14:06:09 +0000	[thread overview]
Message-ID: <37E0F951.2D3E07C7@univ-savoie.fr> (raw)
In-Reply-To: <14302.41638.913957.615588@merlin.cs.clemson.edu>


Try this:

(************** dlist.mli ***********************)
type 'a dlist

val empty : unit -> 'a dlist
val move_left : 'a dlist -> 'a dlist
val move_right : 'a dlist -> 'a dlist
val get_content : 'a dlist -> 'a
val is_left_end : 'a dlist -> bool
val is_right_end : 'a dlist -> bool
val is_not_end : 'a dlist -> bool
val is_empty : 'a dlist -> bool
val insert_right : 'a -> 'a dlist -> unit


(************** dlist.ml ************************)


type 'a dlist =
  | Cell of 'a cell
  | End_Left of 'a end_left
  | End_Right of 'a end_right

and 'a cell = { 
    dlist_cell : 'a; 
    mutable dlist_left : 'a dlist;
    mutable dlist_right : 'a dlist 
  }

and 'a end_left = {
    mutable back_right : 'a dlist;
  } 

and 'a end_right = {
    mutable back_left : 'a dlist;
  } 

let empty () =
  let rec l = 
    End_Left { back_right = End_Right { back_left = l }}
  in l

let move_left l =
  match l with
  | End_Left sr ->
      raise (Invalid_argument "move_left")
  | End_Right sl ->
      sl.back_left
  | Cell s ->
      s.dlist_left

let move_right l =
  match l with
  | End_Left sr ->
      sr.back_right
  | End_Right sl ->
      raise (Invalid_argument "move_right")
  | Cell s ->
      s.dlist_right

let get_content l =
  match l with
  | End_Left sr ->
      raise (Invalid_argument "get_content")
  | End_Right sl ->
      raise (Invalid_argument "get_content")
  | Cell s ->
      s.dlist_cell

let is_left_end = function
    End_Left _ -> true
  | _ -> false

let is_right_end = function
    End_Right _ -> true
  | _ -> false

let is_not_end = function
    Cell _ -> true
  | _ -> false

let is_empty = function
    End_Left sr -> is_right_end sr.back_right
  | End_Right sl -> is_left_end sl.back_left
  | _ -> false

let insert_right a l = 
  let li, lr = match l with
    End_Left sr ->
      let lr = sr.back_right in
      let li = Cell {
	dlist_cell = a;
	dlist_left = l;
	dlist_right = lr;
      }	in
      sr.back_right <- li;
      li, lr
  | End_Right sl ->
      raise (Invalid_argument "insert_left")
  | Cell s ->
      let lr = s.dlist_right in
      let li = Cell {
	dlist_cell = a;
	dlist_left = l;
	dlist_right = lr;
      }	in
      s.dlist_right <- li;
      li, lr
  in
  match lr with
    End_Right sl -> sl.back_left <- li
  | Cell s -> s.dlist_left <- li
  | End_Left sr -> raise (Failure "impossible in insert_left")

let insert_left a l = 
  let li, ll = match l with
    End_Right sl ->
      let ll = sl.back_left in
      let li = Cell {
	dlist_cell = a;
	dlist_left = ll;
	dlist_right = l;
      }	in
      sl.back_left <- li;
      li, ll
  | End_Left sr ->
      raise (Invalid_argument "insert_right")
  | Cell s ->
      let ll = s.dlist_left in
      let li = Cell {
	dlist_cell = a;
	dlist_left = ll;
	dlist_right = l;
      }	in
      s.dlist_left <- li;
      li, ll
  in
  match ll with
    End_Left sr -> sr.back_right <- li
  | Cell s -> s.dlist_right <- li
  | End_Right sl -> raise (Failure "impossible in insert_left")

(*
  test

let x = empty ();;
insert_right 2 x;;
let x' = move_right x;;
insert_right 3 x';;
insert_left 1 x';;
get_content (move_left x');;
get_content (move_right x');;
insert_right 4 x';;
insert_left 0 x';;
get_content (move_left x');;
get_content (move_right x');;
get_content (move_left (move_left x'));;
get_content (move_right (move_right x'));;
*)




      parent reply	other threads:[~1999-09-17 12:11 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-09-14 19:36 Steve Stevenson
1999-09-15 12:35 ` Jerome Vouillon
1999-09-15 13:09   ` Steve Stevenson
1999-09-15 13:33 ` John Prevost
1999-09-15 14:35 ` Stefan Monnier
1999-09-17 12:45   ` Markus Mottl
1999-09-16 14:06 ` Christophe Raffalli [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=37E0F951.2D3E07C7@univ-savoie.fr \
    --to=christophe.raffalli@univ-savoie.fr \
    --cc=caml-list@inria.fr \
    --cc=steve@cs.clemson.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox