Is reduce() foldl() or foldr()?

Piet van Oostrum piet at cs.uu.nl
Sun Jun 7 16:36:24 CEST 2009


>>>>> Tim Northover <T.P.Northover at sms.ed.ac.uk> (TN) escribió:

>TN> Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
>>> Calling all functional programming fans... is Python's built-in reduce() 
>>> a left-fold or a right-fold?

>TN> I get:

>>>>> reduce(lambda a, b: a/b, [1.0, 2.0, 3.0])
>TN> 0.16666666666666666

>TN> which looks like a left fold to me.

Yes, see the Haskell result:

Prelude> foldl (/) 1.0 [1.0, 2.0, 3.0]
0.16666666666666666
Prelude> foldr (/) 1.0 [1.0, 2.0, 3.0]
1.5

-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list