[Haskell-cafe] Re: defining last using foldr

Aaron Denney wnoise at ofb.net
Tue Aug 14 17:52:25 EDT 2007

On 2007-08-14, Chaddaï Fouché <chaddai.fouche at gmail.com> wrote:
> 2007/8/14, Alexteslin <alexteslin at yahoo.co.uk>:
>> Well, i have tried cons (:) operator but when it passed to foldr doesn't work
>> because cons operator operates first character and then the list but the
>> foldr argument takes a function (a->a->a).  Maybe i am missing the point
>> here?
> What Aaron was saying was that in this list :
> 1 : 2 : 3 : 4 : []
> A fold replaced the cons (:) by another function (and [] by another constant).
> Your problem isn't so easy to do with a foldr, a foldl would be easier
> and a foldr1 or foldl1 even better. Are you sure you can't use one of
> those other folds ?

The problem with foldl is that you can't easily make it polymorphic
because of how the null case is handled.  foldl1 and foldr1 are trivial,

Aaron Denney

More information about the Haskell-Cafe mailing list