[Haskell-beginners] Better way to write this?

Brent Yorgey byorgey at seas.upenn.edu
Tue Mar 9 17:28:51 EST 2010


On Tue, Mar 09, 2010 at 10:35:06PM +0200, Mihai Maruseac wrote:
> 
> myLength xs = foldl addOne 0 xs
>   where addOne x n = n + 1

Almost, Tim's original addOne is actually 

  addOne x n = x + 1

The difference is that the given solution uses foldr whereas Tim was
using foldl.  Either one works in this case since + is commutative.

-Brent


More information about the Beginners mailing list