In Concrete Abstractions, there is this example of recursion:
(define subtract-the-first (lambda (n)
(if (= n 0) 0
(- (subtract-the-first (- n 1)) n))))
This I understand. For instance, if n = 3, this function evaluates to:
(- (+ (+ (+ 1) 2) 3)) -> -6
However, in one of the follow-up examples, one is supposed to explain why it’s not possible to switch the order of operations. For instance, let’s look at this:
(define subtract-the-first2 (lambda (n)
(if (= n 0) 0
(- n (subtract-the-first2 (- n 1))))))
If I call (subtract-the-first2 4), the result is 2. However, I don’t quite understand the evaluation. Obviously, I am making a mistake here, because see this:
(- 4 (+ 3 (+ 2 (+ 1))) ), which is equal to (- 4 6) and thus evaluates to -2.
I appreciate any pointers as I’ve been banging me head against the wall for half an hour or so already…
Thank you!
You can check the evaluation of this function by adding some
semiquotes andunquotes:Then evaluate:
This evaluates to
2. (I don’t see where you got the pluses…)