[Haskell-cafe] Exercise in YAHT

Bulat Ziganshin bulat.ziganshin at gmail.com
Sun Aug 6 06:38:35 EDT 2006


Hello algorithms,

Sunday, August 6, 2006, 1:50:42 PM, you wrote:

> listFoldr f y Nil = y
> listFoldr f y (Cons x xs) = f x (foldr f z xs)

this is definitely wrong - using foldr instead of listFoldr and 'z'
appearing from nowhere :)


> listFoldr f y Nil = y
> listFoldr f y (Cons x xs) = f y (listFoldr f (f y x) xs)

y is a final value that should be used only at the last point. for
example:

sum = foldr1 (+) 0

sum [1,2,3] = 1+2+3+0

so, 'y' in second statement for listFoldr should be passed to
recursive call of listFoldr (so it can be eventually used by the first
statemnet) but not used in call to f. second, your definition calls
'f' two times, so the final result will be something like:

sum [1,2,3] = 1+2+3+(1+2+3+0)

(you can try)


-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Haskell-Cafe mailing list