[Haskell-cafe] defining last using foldr

Chaddaï Fouché chaddai.fouche at gmail.com
Wed Aug 15 17:29:51 EDT 2007

2007/8/15, Alexteslin <alexteslin at yahoo.co.uk>:
> I am really sorry, but i still can't define the function.  The chapter the
> exercise is in precedes algebraic types or Maybe a types.  And is seems that
> must being easy to define.

If you don't have Maybe, you still have it's older brother, namely
lists (as strongly hinted by Jon Fairbairn).

> so you need an f so that c `f` x is c (for any c and x) and
> yet (b `f` c) is c for any c and b -- this is impossible (or
> I'm asleep).

Well, it isn't "impossible" but quite hard (and not even standard H98
if I'm not mistaken) and obviously not in the range of the
possibilities here.
Still you could "approximate" it, and in fact I believe the [Char] ->
Char requirement is hinting at such a (arguably and IMO ugly)
solution. For example you could assume there would be no "NUL"
character in a string ([Char] is a synonym for String, or vice-versa),
and then get a simple myLast for String in this restrictive case (and
looking exactly like Alexteslin sample).


More information about the Haskell-Cafe mailing list