Ask your own question, for FREE!
MIT 6.001 Structure and Interpretation of Computer Programs, Spring 2005 12 Online
OpenStudy (anonymous):

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

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

yes that is correct

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!