[Haskell-cafe] Re: defining last using foldr

Al Falloon afalloon at synopsys.COM
Thu Aug 16 14:34:29 EDT 2007

Kurt Hutchinson wrote:
> On 8/15/07, Alexteslin <alexteslin at yahoo.co.uk> wrote:
>> 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.
>> I answered some exercises on using foldr such as summing for example, but
>> this one i am trying:
>> myLast :: [Int] -> Int
>> myLast (x:xs) = foldr (some function) x xs.
>> With summing as i understood foldr goes through the list and sums up by (+)
>> operator and one argument like 0.  Like: foldr (+) 0 xs
> I don't think you can do it directly using just foldr. However, you
> can use foldr to build up a function that, when applied to the list,
> will evaluate to the last element. foldr will be acting like a
> function composition engine that evaluates to a function to process
> the list.

Something like this? (spoiler warning)

More information about the Haskell-Cafe mailing list