I use racket and I got the result 4
for following simple code:
(let/cc done
((let/cc esc
(done (+ 1 (let/cc k
(esc k)))))
3))
and I was going to execute this code step-by-step.
First, I changed the first let/cc
into the form of call/cc
like below:
(call/cc (λ (done)
((let/cc esc
(done (+ 1 (let/cc k
(esc k)))))
3)))
Of course, this produces 4
also.
Second, since I found the mechanism of call/cc
in the internet which says call/cc
do following 4 steps:
- Captures the current continuation.
- Constructs a function
C
that takes one argument, and applies the current continuation with that argument value. - Passes this function as an argument to
expr
--- i.e., it invokes(expr C)
. - Returns the result of evaluating
(expr C)
, unlessexpr
callsC
, in which case the value that is passed toC
is returned.
Thus, I followed above steps for the first call/cc
like:
- Current continuation is an identity.
C
refers(λ (x) x)
.Since
expr
is(λ (done) ((let/cc esc (done (+ 1 (let/cc k (esc k))))) 3))
,(expr C)
is:((λ (done) ((let/cc esc (done (+ 1 (let/cc k (esc k))))) 3)) (λ (x) x))
To return the result value of above code, I execute above in racket.
But, above code (modified by me) is not executed and produces an error:
> application: not a procedure; > > expected a procedure that can be applied to arguments > > given: 4 > > arguments...: > > 3
Please what I did wrong. I'm confusing the concept of continuation. Thanks.