Better way to write this?

5 messages
Open this post in threaded view
|

Better way to write this?

 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. http://haskell.org/haskellwiki/99_questions/1_to_10I 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 Thanks, Tim
Open this post in threaded view
|

Better way to write this?

 >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 "\x n -> n + 1" in the above is an anonymous function. More at: http://www.haskell.org/haskellwiki/Anonymous_functionhttp://www.haskell.org/haskellwiki/Lambda_abstractionRegards, Rahul
Open this post in threaded view
|