Version française
Home     About     Download     Resources     Contact us    
Browse thread
Help me find this pdf
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Zheng Li <li@p...>
Subject: Re: Help me find this pdf

Just for fun :>

----
# let rec f ?(pred=[]) s = 
    let conv = function [] -> [<>] | l -> [<'Some l>] in
    [< match s with parser
       | [<'Some x>] -> f ~pred:(pred@x) s
       | [<'h>] -> [<conv pred; 'h; f s>]
       | [<>] -> [<conv pred>] >];;
# open Sdflow;;
# of_list [Some[1]; Some[2; 3]; None; Some[4; 5]] |> f |> to_list;;
- : int list option list = [Some [1; 2; 3]; None; Some [4; 5]]
----

Note that any first element's evaluation is also lazy. 

The [f] above is defined with plain Stream, but you can archive the same with
SDFlow's high-order combinator here such as map, map_fold etc.

"Loup Vaillant" <loup.vaillant@gmail.com> writes:
> If we really want to, we can delay the evaluation at all costs, by
> delaying even the car of the Cons cells
>
> type 'a node_t =
>     | Empty
>     | Cons of 'a Lazy.t * 'a lazylist
> and 'a lazylist = 'a node_t Lazy.t
> ;;
>
> For instance, when figuring out if the list is empty or not, the first
> element isn't forced. You can even force the second element of the
> list without forcing the first:
>
> match Lazy.force zlst with
>   | Cons(_, tl) ->
>     match Lazy.force tl with
>       | Cons(hd, _) ->
>         let head = Lazy.force hd in
>           [...]
> ;;
>
> Or, assuming I can get away with unsafe code:
>
> let hd zlst = match Lazy.force zlst with
>   | Cons(h, _) = h
> ;;
> let tl zlst = match Lazy.force zlst with
>   | Cons(_, t) = t
> ;;
>
> (* and finally *)
> match Lazy.force hd (tl zlst)
> with [...]
> ;;

-- 
Zheng Li
http://www.pps.jussieu.fr/~li