how to make the function "length" from "map" and "sum"

Glynn Clements glynn.clements@virgin.net
Fri, 11 Jul 2003 03:05:22 +0100


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 <glynn.clements@virgin.net>