how to make the function "length" from "map" and "sum"
Hello, I am working my way through Simon Thompson's Haskell book, and one exercise has me baffled. That is "how would you define the "length" function using "map" and "sum" "? Any help on this would be appreciated. John Mann ===== "I'd rather be debugging...." - Anon __________________________________ Do you Yahoo!? SBC Yahoo! DSL - Now only $29.95 per month! http://sbc.yahoo.com
John Mann wrote:
Hello, I am working my way through Simon Thompson's Haskell book, and one exercise has me baffled. That is "how would you define the "length" function using "map" and "sum" "? Any help on this would be appreciated.
Create a list of 1's of the same length as the input list (with map),
then compute its sum (with sum).
i.e.:
length = sum . map (const 1)
or:
length l = sum (map (const 1) l)
or:
length l = sum (map (\x -> 1) l)
--
Glynn Clements
participants (2)
-
Glynn Clements -
John Mann