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

Ross Paterson ross at soi.city.ac.uk
Mon Mar 22 08:01:04 EDT 2010


A bit closer to the original:

bfs :: Eq a => (a -> [a]) -> a -> [a]
bfs f s = concat $ takeWhile (not . null) levels
  where levels = foldr trim [] $ [s] : map (nub . (>>= f)) levels
        trim xs xss = xs : map (\\ xs) xss


More information about the Haskell-Cafe mailing list