[Haskell-beginners] proper way to read fold types

prad prad at towardsfreedom.com
Sat Jul 24 17:50:42 EDT 2010


here are the descriptions for foldr and foldr1

foldr :: (a -> b -> b) -> b -> [a] -> b
foldr1 :: (a -> a -> a) -> [a] -> a

i'm trying to make sense of the a vs b in foldr, so here goes:

foldr takes 3 arguments:
	1. some function f, illustrated within () of type b
	2. some value of type b
	3. some list with elements of type a

foldr applies f to each element of [a], computing a new function (f a)
which is then applied to the item of type b, computing a result of type
b, which is then combined with #2 (this would be the accumulator)

finally, the net computation of foldr results in some item of type b.


foldr1 takes 2 arguments:
	1. some function g, illustrated within () of type a
	2. some list with elements of type a

foldr1 applies g to each element of [a], computing a new function (g a)
which is then applied to a non-explicitly defined item of type a,
computing a result also of type a.

the net computation of foldr1 results in some item of type a.


i know how i can use the folds in some situations, but explaining their
type definitions to reveal how they work, is coming out pretty
convoluted when i make the attempt. :(

-- 
In friendship,
prad

                                      ... with you on your journey
Towards Freedom
http://www.towardsfreedom.com (website)
Information, Inspiration, Imagination - truly a site for soaring I's


More information about the Beginners mailing list