Foldr and Foldl on DrRacket

I see how this is done on

(foldl * 1 '(1 2 3 4 5)) == 120
(foldr * 1 '(1 2 3 4 5)) == 120

but i cant figure out how it gets 2 for (foldl - 1 '(1 2 3 4 5)) == 2

I would think (foldl - 1 '(1 2 3 4 5)) would be ((((1-1) -2) -3) -4) -5), a negative number. What did I miss?

I can understand why (foldl + 1 '(1 2 3 4 5)) == 16

+4
source share
1 answer

(foldl - 1 '(1 2 3 4 5))virtually equivalent to (- 5 (- 4 (- 3 (- 2 (- 1 1))))), or in infix, 5 - (4 - (3 - (2 - (1 - 1)))).

Similarly, (foldr - 1 '(1 2 3 4 5))virtually equivalent (- 1 (- 2 (- 3 (- 4 (- 5 1))))), or, infix, 1 - (2 - (3 - (4 - (5 - 1)))).

+6
source

All Articles