Version française
Home     About     Download     Resources     Contact us    
Browse thread
Queue.fold give wrong order?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: David Allsopp <dra-news@m...>
Subject: RE: [Caml-list] Queue.fold give wrong order?
Tom Wilkie wrote:
> Dear all
> 
> Is Queue.fold going over items in the wrong order?  It says "equivalent
> to List.fold_left" but I would expect the behaviour to be, when
> inserting items 1, then 2, then 3 a fold would be given items in that
> order?
> 
> Is there a good reason for this?  Could be have a rev_fold for the
> opposite order please?
> 
> Thanks
> 
> Tom
> 
> # ocaml
>         Objective Caml version 3.10.2
> 
> # open Queue;;
> # let q = Queue.create ();;
> val q : '_a Queue.t = <abstr>
> # Queue.add 1 q;;
> - : unit = ()
> # Queue.add 2 q;;
> - : unit = ()
> # Queue.add 3 q;;
> - : unit = ()
> # Queue.fold (fun acc a -> a::acc) [] q;;
> - : int list = [3; 2; 1]
> # Queue.iter (fun a -> print_endline (string_of_int a)) q;;
> 1
> 2
> 3
> - : unit = ()

List.fold_left (fun acc a -> a::acc) [] [1; 2; 3];;
List.iter (fun a -> print_endline (string_of_int a)) [1; 2; 3];;

If that still puzzles you then please, with respect, re-post to the
beginners' list.


David