1/12
25/11/26
what are accumulators for?
they allow us to preserve context we may need, and also to tell our function where it needs to go after.
so far, we’ve only been able to look at individual placements in a tree or data structure
what are the three kinds of accumulators
context preserving
result so far
worklist
the last two are used to make functions tail recursive
accumulator recipe as told by your notes
take the appropriate template
encapsulate the template, distinctly name the parameter (usually add suffix0)
add accumulator parameter
note that we don’t directly pass the accumulator to the recursion (we’ve gotta do something to it first)
do recursion examples (pretend what the output of the function is and what you would want the acc to be in order to find out what it is)
work through recursive calls until you get an example that helps
write the accumulator type comment and invariant (the type comment is just the type. and then the invariant describes the relationship between the accumulator and the outer list
then we have to initialize (when you edit the parameters of the first call of the function in order to make it have an accumulator before the first case)
then preserve the accumulator (determine what you have to do to the acc when you call the recursion in order for it to be what you need the second time around)
then exploit it (use it to do something once it is in your code)
tail recursion
this is when the recursive call of a function is in tail position
tail position example
;; factorial-tail : Natural -> Natural
;; Computes n! using a tail-recursive accumulator.
(define (factorial-tail n)
(local [(define (helper k acc)
(cond
[(zero? k) acc]
[else (helper (sub1 k) (* k acc))]))]
(helper n 1)))
(factorial-tail 5) ; evaluates to 120inside the local def, we could say that the calls to helper are in tail position, because helper is in the result position of the cond, which is in the result position of the cond, which is in the result position of factorial-tail
overall we could say that the call to helper in the local body is in tail position as well, because the result of helper is the final result of factorial tail
whichever expression becomes the final result of the enclosing function is considered to sit in the tail position of the enclosing function
why does tail recursion matter?
because recursive functions will build up context, and use up hella computer processing power unless the recursion happens in tail position
how do we exploit result-so-far accumulators?
well, they should always be the result of our base case
and the accumulator value will be used in the preserving it for the next step.
these are often used to make something tail recursive
how to make a function tail recursive using a result so far accumulator
template according to accumulator recipe (accumulator, encapsulated)
delete part of template that wraps around the recursive call (the expression that is in actual tail position)
move computation that would’ve been around the recursion into to the accumulator argument position
accumulators and mutual recursion
you gotta pass the accumulator all the way through!!
why do we have worklist accumulators
they allow us to make arbitrary arity trees be tail recursive,
in the same way rsf helped us make structural recursion tail recursive
they are always required for tail-recursive traversal of an arbitrary arity tree
how do worklist accumulators work?
well, they’re actually simpler than you thought, in that they really just exist to eliminate the self-recursive call in a arb-tree template. often, these are lists of the children of nodes we’ve visited, being passed as the list we operate on in the recursion.
worklist accumulator example
(define (count w)
;; rsf is Natural; the number of wizards seen so far
;; (count Wk)
;; (fn-for-wiz Wk 0)
;; (fn-for-wiz Wh 1)
;; (fn-for-wiz Wc 2)
(local [(define (fn-for-wiz w todo rsf)
(fn-for-low (append (wiz-kids w) todo)
;; we make a new list called todo that is just rest low
(add1 rsf)))
(define (fn-for-low todo rsf)
(cond [(empty? todo) rsf]
[else
;(+ (fn-for-wiz (first low) rsf)
; (fn-for-low (rest low) rsf))
;; we dont need these amymore ^^^^
;; fn-for-wiz now handles the rsf, and we can just tell it to keep callin
(fn-for-wiz (first todo) (rest todo) rsf)]))]
(fn-for-wiz w empty 0)))complex worklist accumulator example
in one of the videos, gregor makes the worklist accumulator a struct so that he can pass two bits of data at once with this one accumulator, and make shawty tail recursive