[Haskell-cafe] Exercise in YAHT

algorithms at gmx.de algorithms at gmx.de
Sun Aug 6 05:50:42 EDT 2006

Dear Hugs users,

could you answer the following question from a Haskell novice?

In "Yet another Haskell Tutorial" by Hal Daumé III there is the following exercise:

Exercise 4.8 Write functions listHead, listTail, listFoldl and listFoldr
which are equivalent to their Prelude twins, but function on our List datatype. Don’t
worry about exceptional conditions on the first two.

My question is solely on listfoldr, in fact I only want to ask whether the solution provided in the tutorial is really so obviously wrong as I perceive it to be:

(What follows uses the definition

data List a = Nil
            | Cons a (List a)

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

This IS wrong, isn't it? The y does not even appear after all.

And what can you say about my attempt
for listFoldr?

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

My attempt yields a similar, albeit not identical result compared with the Prelude function foldr when :t-ing it under Hugs:

Hugs> :t foldr 
foldr :: (a -> b -> b) -> b -> [a] -> b

Hugs> :t listFoldr 
listFoldr :: (a -> a -> a) -> a -> List a -> a

Thank you very much.


Echte DSL-Flatrate dauerhaft für 0,- Euro*. Nur noch kurze Zeit!
"Feel free" mit GMX DSL: http://www.gmx.net/de/go/dsl

More information about the Haskell-Cafe mailing list