Greetings,
I have a few questions about call/cc I work on mzscheme v.360
When I try to execute
( (call/cc (lambda (exit) (apply (call/cc (lambda (k)(exit k))) '(1 2 3)) ) ) 2 )
I get this error
apply: expects type <procedure> as 1st argument, given: 2; other arguments were: (1 2 3)
It seems normal to me. If I clearly understand the behavior of call/ cc, the execution is as follow
to try to execute E = ( (call/cc ... ) 2 ) 1- try to execute the (call/cc ... ) 2- this means: try to execute (apply (call/cc (lambda (k)(exit k))) '(1 2 3)) where exit is the continuation -> (lambda (v) (v 2)) 2.1- try to execute (call/cc (lambda (k)(exit k)))) 2.2- this means: try to execute (lambda (k)(exit k)) where k is the continuation (lambda (v) (apply v '(1 2 3)) 2.3- try to execute (exit k) but exit is a continuation 3- stop all calculus and E returns the result of ((lambda (v) (v 2)) (lambda (v) (apply v '(1 2 3))) 4- this generate an error.
If my understanding is good, then I don't understand why the following code generate an error:
( (call/cc (lambda (exit)
(apply (call/cc (lambda (k)(exit k))) '(1 2 3)) ) ) + )
procedure application: expected procedure, given: 6; arguments were: #primitive:+
It looks like the call to the continuation exit does not stop all current calculus.
Can someone explain me where I did wrong ?
Thank you
Ben
Afficher les réponses par date
Greetings,
I have a few questions about call/cc I work on mzscheme v.360
When I try to execute
( (call/cc (lambda (exit) (apply (call/cc (lambda (k)(exit k))) '(1 2 3)) ) ) 2 )
I get this error
apply: expects type <procedure> as 1st argument, given: 2; other arguments were: (1 2 3)
It seems normal to me. If I clearly understand the behavior of call/ cc, the execution is as follow
to try to execute E = ( (call/cc ... ) 2 ) 1- try to execute the (call/cc ... ) 2- this means: try to execute (apply (call/cc (lambda (k)(exit k))) '(1 2 3)) where exit is the continuation -> (lambda (v) (v 2)) 2.1- try to execute (call/cc (lambda (k)(exit k)))) 2.2- this means: try to execute (lambda (k)(exit k)) where k is the continuation (lambda (v) (apply v '(1 2 3)) 2.3- try to execute (exit k) but exit is a continuation 3- stop all calculus and E returns the result of ((lambda (v) (v 2)) (lambda (v) (apply v '(1 2 3))) 4- this generate an error.
If my understanding is good, then I don't understand why the following code generate an error:
( (call/cc (lambda (exit)
(apply (call/cc (lambda (k)(exit k))) '(1 2 3)) ) ) + )
procedure application: expected procedure, given: 6; arguments were: #primitive:+
It looks like the call to the continuation exit does not stop all current calculus.
Can someone explain me where I did wrong ?
Thank you
Ben
2.2- this means: try to execute (lambda (k)(exit k)) where k is the continuation (lambda (v) (apply v '(1 2 3))
Actually, the continuation k is more like
(lambda (v) (exit (apply v '(1 2 3))))
Because if the argument to call/cc does not call the continuation explicitly and instead just returns a value, that value is then passed to the continuation. I.e.
(call/cc (lambda (v) e))
is really more like
(call/cc (lambda (v) (v e)))
This is not inherent to the notion of continuation, it just happens to be the way the call/cc in Scheme was chosen to work.
Stefan
Thank you.
This explains clearly why I have such an error.
Benoît Fraikin
On 08-04-30, at 01:20, Stefan Monnier wrote:
2.2- this means: try to execute (lambda (k)(exit k)) where k is the continuation (lambda (v) (apply v '(1 2 3))
Actually, the continuation k is more like
(lambda (v) (exit (apply v '(1 2 3))))
Because if the argument to call/cc does not call the continuation explicitly and instead just returns a value, that value is then passed to the continuation. I.e.
(call/cc (lambda (v) e))
is really more like
(call/cc (lambda (v) (v e)))
This is not inherent to the notion of continuation, it just happens to be the way the call/cc in Scheme was chosen to work.
Stefan