
I seem to understand the basic idea behind the fold function, i think but do not seem to be able to write them myself. My understanding is that it works by replacing every constructor with a fold ? Could anyone please point me in the direction of a suitable resource of attempt to explain this to me :) Cheers, Paty _________________________________________________________________ Hot chart ringtones and polyphonics. Go to http://ninemsn.com.au/mobilemania/default.asp

I seem to understand the basic idea behind the fold function, i think but do not seem to be able to write them myself. My understanding is that it works by replacing every constructor with a fold ?
I think you mean "replaces every constructor with a function or constant". Think of a list [1,2,3]. Recall this is "syntactic sugar" (abbreviation) for 1:(2:(3:[])). That looks like : 1 \ : 2 \ : 3 \ [] in memory. Recall (+) is the function that takes two arguments and returns their sum. Now do foldr (+) 0 [1,2,3] What happens is that (:) is replaced by the first argument (+) and [] is replaced by the second argument 0, as follows: + 1 \ + 2 \ + 3 \ 0 This is just 1+(2+(3+0)), which is 6. You can see this from the definition of foldr: foldr :: (a -> b -> b) -> b -> [a] -> b foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs) where clearly every [] is replaced by z and every : by f. A similar fold can be written for any datatype; it just takes a different number of arguments, one for every constructor. The general term is "catamorphism". E.g.: data Expr = Num Int | Plus Expr Expr | Times Expr Expr foldExpr :: Int -> (b -> b -> b) -> (b -> b -> b) -> b foldExpr n p t (Num i ) = n i foldExpr n p t (Plus e1 e2) = p (foldExpr n p t e1) (foldExpr n p t e2) foldExpr n p t (Times e1 e2) = t (foldExpr n p t e1) (foldExpr n p t e2) Hope this helps.
Could anyone please point me in the direction of a suitable resource of attempt to explain this to me :)
You could try googling for "catamorphism", or looking at some of Erik Meijer et al's papers on "bananas, lenses, and barbed wire" and so on. --KW 8-)

Think of a list [1,2,3]. Recall this is "syntactic sugar" (abbreviation) for 1:(2:(3:[])). That looks like
FYI, I've just added my answer to your question to the Haskell Wiki, at
http://www.haskell.org/hawiki/WhatIsaFold
Enjoy!
--KW 8-)
--
Keith Wansbrough

Keith is entirely correct.
You can see this from the definition of foldr:
foldr :: (a -> b -> b) -> b -> [a] -> b foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs)
where clearly every [] is replaced by z and every : by f.
I had heard this before when I was first beginning and didn't really find it "clear" :). I think if you write foldr with f in infix notation it's a bit more clear:
foldr f z [] = z foldr f z (x:xs) = x `f` foldr f z xs
or even write the second line as
foldr f z (x:xs) = x `f` xs' where xs' = foldr f z xs
I think in this case it's a bit more clear how "f" is replacing the ":". - Hal

On Wed, 5 Nov 2003 07:17:23 -0800 (PST)
Hal Daume III
Keith is entirely correct.
You can see this from the definition of foldr:
foldr :: (a -> b -> b) -> b -> [a] -> b foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs)
where clearly every [] is replaced by z and every : by f.
I had heard this before when I was first beginning and didn't really find it "clear" :). I think if you write foldr with f in infix notation it's a bit more clear:
foldr f z [] = z foldr f z (x:xs) = x `f` foldr f z xs
or even write the second line as
foldr f z (x:xs) = x `f` xs' where xs' = foldr f z xs
I think in this case it's a bit more clear how "f" is replacing the ":".
- Hal
Simply choose better names, data Tree a = Empty | Leaf a | Branch (Tree a) (Tree a) foldTree empty leaf branch Empty = empty foldTree empty leaf branch (Leaf a) = leaf a foldTree empty leaf branch (Branch l r) = branch (foldTree empty leaf branch l) (foldTree empty leaf branch r) data List a = Nil | Cons a (List a) foldList nil cons Nil = nil foldList nil cons (Cons a as) = cons a (foldList nil cons as)
participants (4)
-
Derek Elkins
-
Hal Daume III
-
Keith Wansbrough
-
Patty Fong