Browse thread
[Caml-list] [ANN] The Missing Library
-
John Goerzen
-
Kenneth Knowles
- Alexander V. Voinov
-
John Goerzen
-
Maxence Guesdon
-
John Goerzen
- Maxence Guesdon
-
John Goerzen
-
Alain.Frisch@e...
-
John Goerzen
-
Alain.Frisch@e...
-
Nicolas Cannasse
-
Yamagata Yoriyuki
- Gerd Stolpmann
-
Nicolas Cannasse
-
Yamagata Yoriyuki
- Jacques GARRIGUE
- Nicolas Cannasse
-
Yamagata Yoriyuki
-
Yamagata Yoriyuki
-
Nicolas Cannasse
- oliver@f...
-
Alain.Frisch@e...
-
John Goerzen
- Henri DF
- Shawn Wagner
- james woodyatt
-
Alain.Frisch@e...
- Basile STARYNKEVITCH
-
John Goerzen
- Kenneth Knowles
- Florian Hars
-
Maxence Guesdon
- Eric C. Cooper
-
Kenneth Knowles
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2004-04-29 (13:38) |
From: | John Goerzen <jgoerzen@c...> |
Subject: | Re: [Caml-list] [ANN] The Missing Library |
On Thu, Apr 29, 2004 at 10:54:09AM +1000, skaller wrote: > > > if (!start <> 0) then print "]" > > > > > > and please don't tell me that isn't a total mess compared with > > > > Of course, your example ignores nicer features available for > > this sort of thing. > > Indeed. If I may be so bold to observe .. you've control > inverted. You're now driving the iteration. You've > got rid of the HOF List.iter and implemented your own > special one. Isn't fold_left an iterator too, just one that works with slightly different semantics? Of course, your example could be simplified to: let print_list l = let count = ref 0 in let printfunc item = print_string (if !count = 0 then "[" else ";"); print_string item; count := !count + 1 in List.iter printfunc l; if !count > 0 then print_string "]";; > The point of the code I displayed was that it > control inverts *without* getting rid of the > driver loop. > > Also .. a small bug in your translation: it prints "[]" > for an empty list. The original prints "". No. Again, my code was: let print_list l = let printfunc count item = print_string (if count = 0 then "[" else ";"); print_string item; count + 1 in if (List.fold_left printfunc 0 l) > 0 then print_string "]";; Try it. If the list is empty, printfunc is never called since there is nothing to iterate over. The return value from fold_left is zero since the count was never incremented, so the if statement evalutes to false and "]" is never called. -- John ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners