When foldl is used in practice it is best to think of foldl in terms of its specification and not of its implementation.
Here is how foldl is specified when called with two lists. The call: (foldl f x0 (cons a0 (cons a1 (cons a2 '()))) (cons b0 (cons b1 (cons b2 '())))) evaluates to the same as (f a2 b2 (f a1 b1 (f a0 b0 x0))) does. As a check we can try it out: > (foldl (λ args (cons 'f args)) 'x0 (cons 'a0 (cons 'a1 (cons 'a2 '()))) (cons 'b0 (cons 'b1 (cons 'b2 '()))))'(f a2 b2 (f a1 b1 (f a0 b0 x0))) Note that (λ args (cons 'f args)) is the function that just prepends a symbol f to its list of arguments. Note: More on the implementation of foldl here: http://stackoverflow.com/questions/32152351/confused-by-init-base-in-foldr-foldl-racket 2017-04-19 22:58 GMT+02:00 Lawrence Bottorff <borg...@gmail.com>: > I see this: > > > (foldl cons '() '(1 2 3 4)) > '(4 3 2 1) > > and have to conclude that foldl is reversing the list. This is bolstered > by this: > > (define (my-reverse lst) > (foldl cons empty lst)) > > which is a way of reversing a list. Good. But then I see this: > > (foldl (lambda (a b result) > (begin > (printf "a: ~a, b: ~a, result: ~a\n" a b result) > (* result (- a b)))) > 1 > '(0 2 4 6) > '(3 4 5 6)) > > giving this: > > a: 0, b: 3, result: 1 > a: 2, b: 4, result: -3 > a: 4, b: 5, result: 6 > a: 6, b: 6, result: -6 > 0 > > and I'm not seeing any reversal, right? The input > > 1 > '(0 2 4 6) > '(3 4 5 6) > > seems to only be putting the result variable first. Or am I missing > something? > > -- > You received this message because you are subscribed to the Google Groups > "Racket Users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to racket-users+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- -- Jens Axel Søgaard -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.