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

Ross Paterson ross at soi.city.ac.uk
Mon Mar 22 07:42:43 EDT 2010


On Mon, Mar 22, 2010 at 10:30:32AM +0000, Johannes Waldmann wrote:
> Nice! - Where's the 'nub'?

A bit longer:

bfs :: Eq a => (a -> [a]) -> a -> [a]
bfs f s = concat $ takeWhile (not . null) $ map snd $ iterate step ([], [s])
  where step (seen, xs) = let seen' = xs++seen in (seen', nub $ [y | x <- xs, y <- f x, notElem y seen'])


More information about the Haskell-Cafe mailing list