Re: [Haskell-cafe] breadth first search one-liner?

2010-03-22 Thread Ross Paterson
On Mon, Mar 22, 2010 at 11:02:32AM +0100, Johannes Waldmann wrote: > Dear all, there is this neat "one-line" BFS implementation > > bfs :: Eq a > => ( a -> [a] ) -> a -> [a] > bfs next start = > let xs = nub $ start : ( xs >>= next ) > in xs > > but it has a problem: it only works fo

[Haskell-cafe] breadth first search one-liner?

2010-03-22 Thread Johannes Waldmann
Dear all, there is this neat "one-line" BFS implementation bfs :: Eq a => ( a -> [a] ) -> a -> [a] bfs next start = let xs = nub $ start : ( xs >>= next ) in xs but it has a problem: it only works for infinite graphs. This is fine: take 20 $ bfs ( \ x -> [2*x, x+1] ) 1 but this is