Accumulators

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/12

flashcard set

Earn XP

Description and Tags

25/11/26

13 Terms

1
New cards

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

2
New cards

what are the three kinds of accumulators

  1. context preserving

  2. result so far

  3. worklist


the last two are used to make functions tail recursive

3
New cards

accumulator recipe as told by your notes

  1. take the appropriate template

  2. encapsulate the template, distinctly name the parameter (usually add suffix0)

  3. add accumulator parameter

  4. note that we don’t directly pass the accumulator to the recursion (we’ve gotta do something to it first)

  5. 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)

  6. work through recursive calls until you get an example that helps

  7. 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

  8. 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)

  9. 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)

  10. then exploit it (use it to do something once it is in your code)


4
New cards

tail recursion

this is when the recursive call of a function is in tail position

5
New cards

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 120

inside 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

6
New cards

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

7
New cards

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

8
New cards

how to make a function tail recursive using a result so far accumulator

  1. template according to accumulator recipe (accumulator, encapsulated)

  2. delete part of template that wraps around the recursive call (the expression that is in actual tail position)

  3. move computation that would’ve been around the recursion into to the accumulator argument position


9
New cards

accumulators and mutual recursion

you gotta pass the accumulator all the way through!!

10
New cards

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

11
New cards

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.

12
New cards

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)))


13
New cards

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