In the notes from lecture3 - the factorial example - might it be simpler for the ifact-helper to only keep track of 2 values instead of 3: (define ifact-helper(lambda(p c) (if (= c 1) p (ifact-helper (* p c) (- c 1)))))
where ifact would be defined as (define ifact(lambda(n)(ifact-helper n (- n 1)))) -i found the way it was explained with the count maybe overcomplicating it
Yes, I would prefer your solution. Simple to see how it multiplies n * (n - 1) * ... * 2. No need to drag the constant value of n along with ifact-helper parameters.
Ohhhh, I see why the ifact-helper uses 3 values - one for product, one for counter, and one to remember the initial value - because in most iterative solutions, the product changes and you still need to remember the initial value to re-use in the operations at each iteration - it just happens in this case of the factorial where the initial value becomes irrelevant in the calculations
Exactly! You are counting down from n - 1, so the iteration stops naturally at 1. In the lecture they are counting up, and need to remember when to stop.
yes that is correct
Join our real-time social learning platform and learn together with your friends!