@@ -0,0 +1,50 @@
4+{-| Library for efficient Lazy evaluation and Infinite Streams.
9+import Signal ((<~), foldp, Signal)
12+data Lazy a = L { run : () -> a }
14+{-| Memoize a thunk so it is evaluated at most once -}
15+thunk : (() -> a) -> Lazy a
16+thunk t = L { run = (Native.Lazy.thunk t) }
19+run l = case l of L r -> r.run ()
21+data Stream a = S { hd : a, tl : Lazy (Stream a) }
26+tl : Stream a -> Stream a
29+repeat : a -> Stream a
30+repeat x = S { hd = x, tl = thunk (\() -> repeat x) }
32+cycle : a -> [a] -> Stream a
33+cycle x xs = let cycle' ys = case ys of
34+ [] -> S { hd = x, tl = thunk (\() -> cycle' xs) }
35+ (y :: ys) -> S { hd = y, tl = thunk (\() -> cycle' ys) }
38+zipWith : (a -> b -> c) -> Stream a -> Stream b -> Stream c
39+zipWith f xs ys = S { hd = f (hd xs) (hd ys)
40+ , tl = thunk (\() -> zipWith f (tl xs) (tl ys)) }
42+observeWhen : Stream a -> Signal b -> Signal a
43+observeWhen str sig = let tls = foldp (\_ -> tl) str sig in
46+take : Int -> Stream a -> [a]
49+ n -> hd xs :: take (n - 1) (tl xs)