[Haskell-beginners] recursion and pattern matching

Brent Yorgey byorgey at seas.upenn.edu
Wed Oct 19 03:34:42 CEST 2011


On Tue, Oct 18, 2011 at 05:04:10PM -0700, Alia wrote:
> Ok, so having spent some further time on this. Methinks I have a solution below.
> 
> The aha moment occurred when I fell upon the definition of foldr and then looked at
> the recursive functions.
> 
> 
> foldr :: (a -> b -> b) -> b -> [a] -> b
> foldr f z []     = z
> foldr f z (x:xs) = f x (foldr f z xs)
> 
> treeFold is implemented two ways, the first is as Brent advised, the second places
> the accumulator after the function and follows from foldr. I suspect the first approach
> is probably more practical because you can curry the accumulator
> away. 

I don't think it makes a big difference which way you order the
arguments.  Since there is one argument for each constructor of Tree,
I just like having the arguments in the same order as the constructors
in the definition.  But it's not really important.

> It was a cool exercise in all. Thanks Brent! (-:  

Glad you enjoyed it! Nicely done!

-Brent



More information about the Beginners mailing list