Home | Markdown | Gemini | Microblog
_____|~~\_____ _____________
_-~ \ | \
_- | ) \ |__/ \ \
_- ) | | | \ \
_- | ) / |--| | |
__-_______________ /__/_______| |_________
( |---- | |
`---------------'--\\\\ .`--' -Glyde-
`||||
type ’a lazy = unit -> ’a;
fun force (f:’a lazy) = f ();
fun delay x = (fn () => x) : ’a lazy;
datatype ’a sequ = NIL | CONS of ’a * ’a sequ lazy;
fun first 0 s = []
| first n NIL = []
| first n (CONS (i,r)) = i :: first (n-1) (force r);
fun filters p NIL = NIL
| filters p (CONS (x,r)) =
if p x
then CONS (x, fn () => filters p (force r))
else
filters p (force r);
fun nat_pairs () =
let
fun from_pair (x,0) =
CONS ((x,0), fn () => from_pair (0,x+1))
| from_pair (up,dn) =
CONS ((up,dn), fn () => from_pair (up+1,dn-1))
in from_pair (0,0)
end;
(* Test
val test = first 10 (nat_pairs ())
*)
fun nat_pairs_not_null () =
filters (fn (x,y) => x > 0 andalso y > 0) (nat_pairs ());
(* Test
val test = first 10 (nat_pairs_not_null ());
*)
{- Just to make it look like the ML example -}
first = take
filters = filter
{- Implementation -}
nat_pairs = from_pair 0 0
where
from_pair x 0 = [x,0] : from_pair 0 (x+1)
from_pair up dn = [up,dn] : from_pair (up+1) (dn-1)
{- Test:
first 10 nat_pairs
-}
nat_pairs_not_null = filters (\[x,y] -> x > 0 && y > 0) nat_pairs
{- Test:
first 10 nat_pairs_not_null
-}