It just occured to me how to use Computational Expressions to write a Generator/sequence expression for Lazy Lists.

Basically it is the list monad switching the list for the lazyList type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#light

module LazyListMonad =
    let result v = LazyList.cons v (LazyList.empty())

    let bind (llist:'a LazyList) f = LazyList.map f llist |> LazyList.concat

    let zero<'a> = LazyList.empty(): 'a LazyList

    let append xs ys = LazyList.append xs ys

    type LazyBuilder() =
        member b.Bind (llist, f) = bind llist f
        member b.Return (v) = result v
        member b.Let (v, f) = bind (result v) f
        member b.For (s, f) = bind (LazyList.of_seq s) f
        member b.Combine (listA, listB) = LazyList.append listA listB
        member b.Zero () = LazyList.empty ()
        member b.Delay (f) = f():'a LazyList
    
let LazyL = new LazyListMonad.LazyBuilder()

let list = 
    LazyL
        {  for i in 1 .. 6 do
            for j in 10 .. 20 do 
                if i%2=0 && j%2=0 then
                    yield (i,j)
        }

Not sure if the computational expression is implemented correctly but I just hacked that out in a few minutes after I had my eureka moment. Now what has me perplexed is the fact that using yield gets the following warning in fsi

warning FS0191: We recommend you use 'return' instead of '->' within computation expressions, as future extensions to the syntax may require this change.

are you guys planning to remove yield. I can swap it with return and it works fine. The only problem I have is using yield reads better.

Other than that the only thing left is to use a computation expression to describe a LazyList via its unfold function, and potentially combine both to have a robust computational expression that lets you describe a lazylist

By on 7/3/2008 5:22 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper