Hello, could someone please explain why "fix" is necessary here: fix (\f l -> if null l then [] else let (s,e) = break (==' ') l in s:f (drop 1 e)) Source: http://www.haskell.org/haskellwiki/Blow_your_mind Thanks. phiroc
On 02/05/07, phiroc@free.fr <phiroc@free.fr> wrote:
Hello,
could someone please explain why "fix" is necessary here:
fix (\f l -> if null l then [] else let (s,e) = break (==' ') l in s:f (drop 1 e))
Because you're writing a recursive function. If you just had: if null l then [] else let (s,e) = break (==' ') l in s:XXX (drop 1 e) Then what goes in place of 'XXX'? There's no name for this particular bit of code, so you can't call it. Using fix allows you to attach a name to an arbitrary expression so that you can call it recursively. The following would work exactly the same: words :: String -> [String] words l = if null l then [] else let (s,e) = break (==' ') l in s:words (drop 1 e) -- -David House, dmhouse@gmail.com
participants (2)
-
David House -
phiroc@free.fr