* Queue.fold give wrong order?
@ 2010-01-19 15:10 Tom Wilkie
2010-01-19 15:22 ` [Caml-list] " David Allsopp
0 siblings, 1 reply; 3+ messages in thread
From: Tom Wilkie @ 2010-01-19 15:10 UTC (permalink / raw)
To: caml-list; +Cc: Tom Wilkie
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 = ()
^ permalink raw reply [flat|nested] 3+ messages in thread
* RE: [Caml-list] Queue.fold give wrong order?
2010-01-19 15:10 Queue.fold give wrong order? Tom Wilkie
@ 2010-01-19 15:22 ` David Allsopp
0 siblings, 0 replies; 3+ messages in thread
From: David Allsopp @ 2010-01-19 15:22 UTC (permalink / raw)
To: 'Tom Wilkie', caml-list
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] Queue.fold give wrong order?
@ 2010-01-19 15:53 Bruno Verlyck, Bruno.Verlyck
0 siblings, 0 replies; 3+ messages in thread
From: Bruno Verlyck, Bruno.Verlyck @ 2010-01-19 15:53 UTC (permalink / raw)
To: Tom Wilkie; +Cc: caml-list
Hi,
From: Tom Wilkie <tom@acunu.com>
Date: Tue, 19 Jan 2010 15:10:43 +0000
> 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?
It's already the case.
> # Queue.fold (fun acc a -> a::acc) [] q;;
> - : int list = [3; 2; 1]
Your (fun acc a -> a::acc) is first called with args [] and 1, and
yields a next value for accu of [1]; and so on.
HTH,
Bruno.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2010-01-19 15:53 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-01-19 15:10 Queue.fold give wrong order? Tom Wilkie
2010-01-19 15:22 ` [Caml-list] " David Allsopp
2010-01-19 15:53 Bruno Verlyck, Bruno.Verlyck
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox