On the road to functional programming enlightenment, I discovered two beautiful one-liners for depth-first and breadth-first search in Haskell. Here they are:
dfs, bfs :: (a -> [a]) -> a -> [a] dfs succ start = start : concatMap (dfs succ) (succ start) bfs succ start = concat $ takeWhile (not . null) $ iterate (concatMap succ) [start]
Both functions generate the list of states reachable from a starting state in a finite number of steps. The inputs to
dfs are the starting state and a “successors” function taking a state to the list of its “child” states.
What consistently impresses me about Haskell is that algorithms can be expressed in code as concisely as in English, if only you can learn the language to the same level of native fluency. As a native English speaker who’s only been writing Haskell casually for a year, I puzzled over the two one-liners in this post for about an hour before getting them right!