On Thursday 18 October 2007 15:22:32 Brian Hurt wrote:
You have to explicitly force the lazy value first-
If you force all lazy values before the pattern match then you are doing eager
evaluation, not lazy. Moreover, you must also generate an auxiliary data
structure to store the forced values in order to pattern match over them.
You don't need to force all of them, you just need to force *one* (at
least).
(I forget what it's called at the moment), but baring that...
You can't ignore that: its the whole point! :-)
Consider a function that concatenates adjacent "Some _" values in a list:
# let rec f = function
| [] -> []
| Some x :: Some y :: t -> f(Some(x @ y) :: t)
| h :: t -> h :: f t;;
val f : 'a list option list -> 'a list option list = <fun>
# f [Some [1]; Some [2;3]; None; Some [4;5]];;
- : int list option list = [Some [1; 2; 3]; None; Some [4; 5]]
Thanks to pattern matching, this solution is almost as beautiful as my wife.
An equivalent with lazy lists might be:
# type 'a llist = Nil | Cons of 'a * 'a llist lazy_t;;
Bad definition of a lazy list- the first element always has to be
forced. Using my definition from above: