[Haskell-beginners] Better way to write this?

Tim Perry perry2of5 at yahoo.com
Tue Mar 9 15:26:29 EST 2010

I'm working my way through the 99 sample programs for haskell and I'm on #4: find the number of elements in a list.

I wrote the obvious recursion. Then I rewrote it using foldl
myLength :: [a] -> Int
myLength [] = 0
myLength xs = foldl addOne 0 xs
   where addOne lhs rhs = lhs + 1

However, the solution given uses notation that confuses my little mind. Can someone point me to a good resource on this notation?

myLength :: [a] -> Int
myLength =  foldr (\x n -> n + 1) 0


More information about the Beginners mailing list