datatype 'a seq = Nil | Cons of 'a * (unit -> 'a seq); fun takeq(0,xq) = [] | takeq(n,Nil) = [] | takeq(n,Cons(x,xf)) = x :: takeq(n-1,xf()); fun filterq p Nil = Nil | filterq p (Cons(x,xf)) = if p x then Cons(x, fn()=>filterq p (xf())) else filterq p (xf()); fun depth next x = let fun dfs [] = Nil | dfs(y::ys) = Cons(y, fn()=> dfs(next y @ ys)) in dfs [x] end; fun breadth next x = let fun bfs [] = Nil | bfs(y::ys) = Cons(y, fn()=> bfs(ys @ next y)) in bfs [x] end; fun next l = [#"A"::l, #"B"::l, #"C"::l]; fun is_pal l = (l = rev l); (* walk over the strings in depth-first fashion *) val strings1 = filterq is_pal (depth next []); (* walk over the strings in breadth-first fashion *) val strings2 = filterq is_pal (breadth next []); (* print out the first 50 from each *) foldl (fn (x,_) => print(implode(x)^"\n")) () (takeq(50,strings1)); foldl (fn (x,_) => print(implode(x)^"\n")) () (takeq(50,strings2));