https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Flist..rkt%29._foldl%29%29
foldl doesn't actually reverse a list (that's 'reverse'), it iterates through a list (or multiple lists) applying a function as it goes. Each cycle the function is given the next item in the list plus an accumulator; at the end of the iteration the accumulator is set to the result of the calculation. Example: (foldl + 0 '(2 3 4)) Result of foldl: 9 This is equivalent to the following: Function: + Accumulator: 0 First cycle: (+ 2 0) ; => 2 ; first item from list and accumulator Accumulator: 2 Second cycle: (+ 3 2) ; => 5 ; next item from list and accumulator Accumulator: 5 Third cycle: (+ 4 5) ; => 9 ; next item from list and accumulator Result of foldl: 9 (foldl cons '() '(a b c)) ; => '(c b a) First cycle: (cons 'a '()) ; => '(a) ; first arg, accumulator Second cycle: (cons 'b '(a)) ; => '(b a) ; next arg, accumulator Third cycle: (cons 'c '(b a)) ; => (c b a) ; next arg, accumulator Result of foldl: '(c b a) You can pass your own procedures as well: (foldl (lambda (arg1 accumulator) (printf "arg1: ~a | acc: ~a\n" arg1 accumulator) (cons arg1 accumulator)) '() '(a b c)) Prints: arg1: a | acc: () arg1: b | acc: (a) arg1: c | acc: (b a) And returns: '(c b a) It works the same if you have multiple lists, except your function needs to take one argument for each list you're iterating over plus one for the accumulator: (foldl (lambda (arg1 arg2 accumulator) (printf "arg1: ~a | arg2: ~a | acc: ~a\n" arg1 ar\ g2 accumulator) (cons arg1 (cons arg2 accumulator))) '() '(a b c) '(1 2 3)) Prints: arg1: a | arg2: 1 | acc: () arg1: b | arg2: 2 | acc: (a 1) arg1: c | arg2: 3 | acc: (b 2 a 1) And returns: '(c 3 b 2 a 1) Does that help? On Wed, Apr 19, 2017 at 4:58 PM, Lawrence Bottorff <borg...@gmail.com> wrote: > 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. > -- 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.