Re: [racket-users] confusion about call-with-current-continuation

2018-10-26 Thread Shu-Hung You
Because applying the continuation captured by Racket's
call-with-current-continuation will *replace* existing continuations.
Therefore the exception handler is removed. To obtain a continuation
that will only *extend* the current continuation, use
call-with-composable-continuation.

(Another way to get "div by 0" is to wrap the call to saved-k in a
prompt via call-with-continuation-prompt so saved-k would not remove
the portion of continuation that installs the handler.)
On Fri, Oct 26, 2018 at 3:53 AM  wrote:
>
>
> Thanks Shu-hung.
>
> So here is my understanding:
>
> #lang racket
>
> (begin
>
>   (define saved-k #f) ; wrapped in "prompt 1"
>
>   (+ 1 (call/cc
> (lambda (k)
>   (set! saved-k k)
>   0))) ; wrapped in "prompt 2"
>
>   (println 'hello) ; wrapped in "prompt 3"
>
>   (saved-k 100) ; wrapped in "prompt 4"
>
>   (println 'hello2) ; wrapped in "prompt 5"
>
>   )
>
> When we call (saved-k 100), control flow jump to the continuation saved 
> before, but it still in "prompt 4".
> After "prompt 4" all finished, the next prompt is "prompt 5" (prompt is also 
> a continuation, but marked) , so print 'hello2.
> Then all end.
>
> Everything is fine.
>
> But what about this code:
>
> #lang racket
>
> (begin
>
>   (define saved-k #f) ; wrapped in "prompt 1"
>
>   (/ 1 (call/cc
> (lambda (k)
>   (set! saved-k k)
>   1))) ; wrapped in "prompt 2"
>
>   (println 'hello) ; wrapped in "prompt 3"
>
>   (with-handlers ([exn:fail:contract:divide-by-zero?
>(lambda (exn) (println "div by 0"))])
> (saved-k 0)) ; wrapped in "prompt 4"
>
>   (println 'hello2) ; wrapped in "prompt 5"
>
>   )
>
> Since the continuation executed in "prompt 4", why I can not print "div by 0" 
> ?
>
>
> On Friday, October 26, 2018 at 3:57:57 AM UTC+8, Shu-Hung You wrote:
>>
>> FWIW here's the part in the document that could be helpful:
>>
>> 1. The section in the Guide that talks about the concept of prompts
>> and their usage in the continuation in Racket:
>> https://docs.racket-lang.org/guide/prompt.html
>> The prompt is kind of like a mark on the continuations that lets the
>> control operators find the portion of continuations they want to
>> manipulate.
>>
>> 2. About your original question (why (print 'hello) isn't captured),
>> there are two things happening here:
>>
>> 2a. Begin is a special syntactic form. It does not just mean
>> evaluating a sequence of expressions in order. When used at certain
>> contexts such as top-level (module-level) context, begin splices the
>> forms it contains to the surrounding context. The last paragraph in
>> the guide ( https://docs.racket-lang.org/guide/begin.html ) briefly
>> talked about it. The reference (
>> https://docs.racket-lang.org/reference/begin.html#(form._((quote._~23~25kernel)._begin))
>> ) made it explicit that the forms
>> (begin expr expr ...)   and
>> (begin form form ...)
>> are different.
>>
>> As an example, in the following module, with or without begin makes no
>> difference at all. The subforms are spliced to the module-level
>> context:
>>
>> #lang racket
>>
>> (define (even??? x) (odd??? (- x 1)))
>>
>> ;; At module-level context, this begin works as if the
>> ;; program were just written directly without begin:
>> ;; (displayln ...)
>> ;; (define (odd??? ...
>> (begin
>> (displayln "No difference")
>> (define (odd??? x) (odd? x)))
>>
>> (odd??? 9)
>> (even??? 8)
>>
>> This, combined with the following reason (2b) is why your code didn't
>> work as expected.
>>
>> 2b. The evaluation of module-level expressions and definitions are
>> implicitly wrapped in a prompt. That is, for a module
>>
>> #lang racket
>>
>> expr1
>> (define def2 expr2)
>> expr3
>> expr4
>> (define def5 expr5)
>> ...
>>
>> the evaluation of each expr is implicitly wrapped by the default
>> prompt as if it were evaluated using:
>>
>> procedure instantiate a module M:
>> for each module-level expression/definition EXPR do:
>>   with DEFAULT_PROMPT:
>>   result = eval(EXPR)
>>   ...
>>
>> The control operators (e.g. call/cc and abort) by default looks for
>> the default prompt. This effectively delimits the control effect of
>> each module-level expression and therefore the call/cc in your code
>> will not capture the println portion.
>>
>> This behavior is described in the reference about how the body of a
>> module is evaluated, starting from the paragraph ``A module body is
>> executed only when the module is explicitly instantiated ...''
>> https://docs.racket-lang.org/reference/module.html
>>
>> On Thu, Oct 25, 2018 at 2:00 PM  wrote:
>> >
>> >
>> > About (+ 1 (prompt (* 2 (call/cc (lambda (k) (set! x k) 2)
>> >
>> > 1. "prompt" form does not exist in racket.
>> > It is a procedure application or special form ?
>> >
>> > 2. assume this procedure or special form exists to construct a prompt.
>> >
>> > (+ 1 (prompt (* 2 (call/cc (lambda (k) (set! x k) 2)
>> >
>> > After the (lambda (k) ...) 

Re: [racket-users] confusion about call-with-current-continuation

2018-10-26 Thread serioadamo97

Thanks Shu-hung.

So here is my understanding:

#lang racket

(begin

  (define saved-k #f) ; wrapped in "prompt 1"

  (+ 1 (call/cc
(lambda (k)
  (set! saved-k k)
  0))) ; wrapped in "prompt 2"

  (println 'hello) ; wrapped in "prompt 3"

  (saved-k 100) ; wrapped in "prompt 4"

  (println 'hello2) ; wrapped in "prompt 5"
  
  )

When we call (saved-k 100), control flow jump to the continuation saved 
before, but it still in "prompt 4".
After "prompt 4" all finished, the next prompt is "prompt 5" (prompt is 
also a continuation, but marked) , so print 'hello2.
Then all end.

Everything is fine.

But what about this code:

#lang racket

(begin

  (define saved-k #f) ; wrapped in "prompt 1"

  (/ 1 (call/cc
(lambda (k)
  (set! saved-k k)
  1))) ; wrapped in "prompt 2"

  (println 'hello) ; wrapped in "prompt 3"

  (with-handlers ([exn:fail:contract:divide-by-zero?
   (lambda (exn) (println "div by 0"))])
(saved-k 0)) ; wrapped in "prompt 4"

  (println 'hello2) ; wrapped in "prompt 5"
  
  )

Since the continuation executed in "prompt 4", why I can not print "div by 
0" ?


On Friday, October 26, 2018 at 3:57:57 AM UTC+8, Shu-Hung You wrote:
>
> FWIW here's the part in the document that could be helpful: 
>
> 1. The section in the Guide that talks about the concept of prompts 
> and their usage in the continuation in Racket: 
> https://docs.racket-lang.org/guide/prompt.html 
> The prompt is kind of like a mark on the continuations that lets the 
> control operators find the portion of continuations they want to 
> manipulate. 
>
> 2. About your original question (why (print 'hello) isn't captured), 
> there are two things happening here: 
>
> 2a. Begin is a special syntactic form. It does not just mean 
> evaluating a sequence of expressions in order. When used at certain 
> contexts such as top-level (module-level) context, begin splices the 
> forms it contains to the surrounding context. The last paragraph in 
> the guide ( https://docs.racket-lang.org/guide/begin.html ) briefly 
> talked about it. The reference ( 
>
> https://docs.racket-lang.org/reference/begin.html#(form._((quote._~23~25kernel)._begin))
>  
> ) made it explicit that the forms 
> (begin expr expr ...)   and 
> (begin form form ...) 
> are different. 
>
> As an example, in the following module, with or without begin makes no 
> difference at all. The subforms are spliced to the module-level 
> context: 
>
> #lang racket 
>
> (define (even??? x) (odd??? (- x 1))) 
>
> ;; At module-level context, this begin works as if the 
> ;; program were just written directly without begin: 
> ;; (displayln ...) 
> ;; (define (odd??? ... 
> (begin 
> (displayln "No difference") 
> (define (odd??? x) (odd? x))) 
>
> (odd??? 9) 
> (even??? 8) 
>
> This, combined with the following reason (2b) is why your code didn't 
> work as expected. 
>
> 2b. The evaluation of module-level expressions and definitions are 
> implicitly wrapped in a prompt. That is, for a module 
>
> #lang racket 
>
> expr1 
> (define def2 expr2) 
> expr3 
> expr4 
> (define def5 expr5) 
> ... 
>
> the evaluation of each expr is implicitly wrapped by the default 
> prompt as if it were evaluated using: 
>
> procedure instantiate a module M: 
> for each module-level expression/definition EXPR do: 
>   with DEFAULT_PROMPT: 
>   result = eval(EXPR) 
>   ... 
>
> The control operators (e.g. call/cc and abort) by default looks for 
> the default prompt. This effectively delimits the control effect of 
> each module-level expression and therefore the call/cc in your code 
> will not capture the println portion. 
>
> This behavior is described in the reference about how the body of a 
> module is evaluated, starting from the paragraph ``A module body is 
> executed only when the module is explicitly instantiated ...'' 
> https://docs.racket-lang.org/reference/module.html 
>
> On Thu, Oct 25, 2018 at 2:00 PM > 
> wrote: 
> > 
> > 
> > About (+ 1 (prompt (* 2 (call/cc (lambda (k) (set! x k) 2) 
> > 
> > 1. "prompt" form does not exist in racket. 
> > It is a procedure application or special form ? 
> > 
> > 2. assume this procedure or special form exists to construct a prompt. 
> > 
> > (+ 1 (prompt (* 2 (call/cc (lambda (k) (set! x k) 2) 
> > 
> > After the (lambda (k) ...) evaluated, value of k is (* 2 []), then I 
> call 
> > (k 0) ; < When does the control flow come back here? 
> > 
> > (k 0) ---jump to--> (* 2 []), and then? 
> > 
> > What happens after (*2 0) ? What is the continuation of (*2 0)? 
> > 
> > 
> > On Friday, October 26, 2018 at 2:11:43 AM UTC+8, Joao Pedro Abreu De 
> Souza wrote: 
> >> 
> >> well, let's go by parts : 
> >> 
> >> 1) call/cc in principle will capture the complete continuation of a 
> expression, right? Delimited continuation will capture ... welll, delimited 
> continuations. But delimited by what? By a prompt. 
> >> In a delimited 

Re: [racket-users] confusion about call-with-current-continuation

2018-10-25 Thread Joao Pedro Abreu De Souza
Shu-hung, very cool information.

Em qui, 25 de out de 2018 às 16:57, Shu-Hung You <
shu-hung@eecs.northwestern.edu> escreveu:

> FWIW here's the part in the document that could be helpful:
>
> 1. The section in the Guide that talks about the concept of prompts
> and their usage in the continuation in Racket:
> https://docs.racket-lang.org/guide/prompt.html
> The prompt is kind of like a mark on the continuations that lets the
> control operators find the portion of continuations they want to
> manipulate.
>
> 2. About your original question (why (print 'hello) isn't captured),
> there are two things happening here:
>
> 2a. Begin is a special syntactic form. It does not just mean
> evaluating a sequence of expressions in order. When used at certain
> contexts such as top-level (module-level) context, begin splices the
> forms it contains to the surrounding context. The last paragraph in
> the guide ( https://docs.racket-lang.org/guide/begin.html ) briefly
> talked about it. The reference (
>
> https://docs.racket-lang.org/reference/begin.html#(form._((quote._~23~25kernel)._begin))
> ) made it explicit that the forms
> (begin expr expr ...)   and
> (begin form form ...)
> are different.
>
> As an example, in the following module, with or without begin makes no
> difference at all. The subforms are spliced to the module-level
> context:
>
> #lang racket
>
> (define (even??? x) (odd??? (- x 1)))
>
> ;; At module-level context, this begin works as if the
> ;; program were just written directly without begin:
> ;; (displayln ...)
> ;; (define (odd??? ...
> (begin
> (displayln "No difference")
> (define (odd??? x) (odd? x)))
>
> (odd??? 9)
> (even??? 8)
>
> This, combined with the following reason (2b) is why your code didn't
> work as expected.
>
> 2b. The evaluation of module-level expressions and definitions are
> implicitly wrapped in a prompt. That is, for a module
>
> #lang racket
>
> expr1
> (define def2 expr2)
> expr3
> expr4
> (define def5 expr5)
> ...
>
> the evaluation of each expr is implicitly wrapped by the default
> prompt as if it were evaluated using:
>
> procedure instantiate a module M:
> for each module-level expression/definition EXPR do:
>   with DEFAULT_PROMPT:
>   result = eval(EXPR)
>   ...
>
> The control operators (e.g. call/cc and abort) by default looks for
> the default prompt. This effectively delimits the control effect of
> each module-level expression and therefore the call/cc in your code
> will not capture the println portion.
>
> This behavior is described in the reference about how the body of a
> module is evaluated, starting from the paragraph ``A module body is
> executed only when the module is explicitly instantiated ...''
> https://docs.racket-lang.org/reference/module.html
>
> On Thu, Oct 25, 2018 at 2:00 PM  wrote:
> >
> >
> > About (+ 1 (prompt (* 2 (call/cc (lambda (k) (set! x k) 2)
> >
> > 1. "prompt" form does not exist in racket.
> > It is a procedure application or special form ?
> >
> > 2. assume this procedure or special form exists to construct a prompt.
> >
> > (+ 1 (prompt (* 2 (call/cc (lambda (k) (set! x k) 2)
> >
> > After the (lambda (k) ...) evaluated, value of k is (* 2 []), then I call
> > (k 0) ; < When does the control flow come back here?
> >
> > (k 0) ---jump to--> (* 2 []), and then?
> >
> > What happens after (*2 0) ? What is the continuation of (*2 0)?
> >
> >
> > On Friday, October 26, 2018 at 2:11:43 AM UTC+8, Joao Pedro Abreu De
> Souza wrote:
> >>
> >> well, let's go by parts :
> >>
> >> 1) call/cc in principle will capture the complete continuation of a
> expression, right? Delimited continuation will capture ... welll, delimited
> continuations. But delimited by what? By a prompt.
> >> In a delimited contninuation style(not really, but I dont want to mix
> functions), we will do something like (+ 1 (prompt (* 2 (call/cc (lambda
> (k) (set! x k) 2)
> >>
> >> this will set x with (* 2 []), because delimited capture from prompt.
> call/cc in racket works in a similar way, if around the most extern s-exp
> exists a prompt.
> >>
> >> 2) well, when I dont know exactly why begin dont work and lambda do,
> but I have a clue : if you do (syntax->datum (expand '(begin (print "hi")
> (print "hello" in drracket, you will receive
> >> '(begin (#%app print '"hi") (#%app print '"hello"))
> >>
> >> and if you do (syntax->datum (expand '((lambda () (print "hi") (print
> "hello") you will receive '(#%app (lambda () (#%app print '"hi") (#%app
> print '"hello")))
> >>
> >> Probably, as begin dont is involved in a #%app, he dont is invoked with
> a prompt. But this is just speculation of my part.
> >>
> >> Em qui, 25 de out de 2018 às 14:13,  escreveu:
> >>>
> >>> Thank Joao
> >>>
> >>> I change my code like this:
> >>>
> >>> #lang racket
> >>>
> >>>
> >>> ((lambda ()
> >>>
> >>>   (define saved-k #f)
> >>>
> >>>   (println (+ 1 (call/cc
> >>> (lambda (k) ; k is the captured 

Re: [racket-users] confusion about call-with-current-continuation

2018-10-25 Thread Shu-Hung You
FWIW here's the part in the document that could be helpful:

1. The section in the Guide that talks about the concept of prompts
and their usage in the continuation in Racket:
https://docs.racket-lang.org/guide/prompt.html
The prompt is kind of like a mark on the continuations that lets the
control operators find the portion of continuations they want to
manipulate.

2. About your original question (why (print 'hello) isn't captured),
there are two things happening here:

2a. Begin is a special syntactic form. It does not just mean
evaluating a sequence of expressions in order. When used at certain
contexts such as top-level (module-level) context, begin splices the
forms it contains to the surrounding context. The last paragraph in
the guide ( https://docs.racket-lang.org/guide/begin.html ) briefly
talked about it. The reference (
https://docs.racket-lang.org/reference/begin.html#(form._((quote._~23~25kernel)._begin))
) made it explicit that the forms
(begin expr expr ...)   and
(begin form form ...)
are different.

As an example, in the following module, with or without begin makes no
difference at all. The subforms are spliced to the module-level
context:

#lang racket

(define (even??? x) (odd??? (- x 1)))

;; At module-level context, this begin works as if the
;; program were just written directly without begin:
;; (displayln ...)
;; (define (odd??? ...
(begin
(displayln "No difference")
(define (odd??? x) (odd? x)))

(odd??? 9)
(even??? 8)

This, combined with the following reason (2b) is why your code didn't
work as expected.

2b. The evaluation of module-level expressions and definitions are
implicitly wrapped in a prompt. That is, for a module

#lang racket

expr1
(define def2 expr2)
expr3
expr4
(define def5 expr5)
...

the evaluation of each expr is implicitly wrapped by the default
prompt as if it were evaluated using:

procedure instantiate a module M:
for each module-level expression/definition EXPR do:
  with DEFAULT_PROMPT:
  result = eval(EXPR)
  ...

The control operators (e.g. call/cc and abort) by default looks for
the default prompt. This effectively delimits the control effect of
each module-level expression and therefore the call/cc in your code
will not capture the println portion.

This behavior is described in the reference about how the body of a
module is evaluated, starting from the paragraph ``A module body is
executed only when the module is explicitly instantiated ...''
https://docs.racket-lang.org/reference/module.html

On Thu, Oct 25, 2018 at 2:00 PM  wrote:
>
>
> About (+ 1 (prompt (* 2 (call/cc (lambda (k) (set! x k) 2)
>
> 1. "prompt" form does not exist in racket.
> It is a procedure application or special form ?
>
> 2. assume this procedure or special form exists to construct a prompt.
>
> (+ 1 (prompt (* 2 (call/cc (lambda (k) (set! x k) 2)
>
> After the (lambda (k) ...) evaluated, value of k is (* 2 []), then I call
> (k 0) ; < When does the control flow come back here?
>
> (k 0) ---jump to--> (* 2 []), and then?
>
> What happens after (*2 0) ? What is the continuation of (*2 0)?
>
>
> On Friday, October 26, 2018 at 2:11:43 AM UTC+8, Joao Pedro Abreu De Souza 
> wrote:
>>
>> well, let's go by parts :
>>
>> 1) call/cc in principle will capture the complete continuation of a 
>> expression, right? Delimited continuation will capture ... welll, delimited 
>> continuations. But delimited by what? By a prompt.
>> In a delimited contninuation style(not really, but I dont want to mix 
>> functions), we will do something like (+ 1 (prompt (* 2 (call/cc (lambda (k) 
>> (set! x k) 2)
>>
>> this will set x with (* 2 []), because delimited capture from prompt.  
>> call/cc in racket works in a similar way, if around the most extern s-exp 
>> exists a prompt.
>>
>> 2) well, when I dont know exactly why begin dont work and lambda do, but I 
>> have a clue : if you do (syntax->datum (expand '(begin (print "hi") (print 
>> "hello" in drracket, you will receive
>> '(begin (#%app print '"hi") (#%app print '"hello"))
>>
>> and if you do (syntax->datum (expand '((lambda () (print "hi") (print 
>> "hello") you will receive '(#%app (lambda () (#%app print '"hi") (#%app 
>> print '"hello")))
>>
>> Probably, as begin dont is involved in a #%app, he dont is invoked with a 
>> prompt. But this is just speculation of my part.
>>
>> Em qui, 25 de out de 2018 às 14:13,  escreveu:
>>>
>>> Thank Joao
>>>
>>> I change my code like this:
>>>
>>> #lang racket
>>>
>>>
>>> ((lambda ()
>>>
>>>   (define saved-k #f)
>>>
>>>   (println (+ 1 (call/cc
>>> (lambda (k) ; k is the captured continuation
>>>   (set! saved-k k)
>>>   0
>>>
>>>   (println 'hello)
>>>
>>>   (saved-k 100)
>>>
>>>
>>>   ))
>>>
>>> Now it works as I expected.
>>>
>>> But why?
>>>
>>> "begin" special form packages a sequence of expressions into a single 
>>> expression. After compiled, it should be continuation-passing style.
>>>
>>> "lambda" 

Re: [racket-users] confusion about call-with-current-continuation

2018-10-25 Thread serioadamo97

About (+ 1 (prompt (* 2 (call/cc (lambda (k) (set! x k) 2)

1. "prompt" form does not exist in racket. 
It is a procedure application or special form ?

2. assume this procedure or special form exists to construct a prompt.

(+ 1 (prompt (* 2 (call/cc (lambda (k) (set! x k) 2)

After the (lambda (k) ...) evaluated, value of k is (* 2 []), then I call
(k 0) ; < When does the control flow come back here?

(k 0) ---jump to--> (* 2 []), and then?

What happens after (*2 0) ? What is the continuation of (*2 0)?


On Friday, October 26, 2018 at 2:11:43 AM UTC+8, Joao Pedro Abreu De Souza 
wrote:
>
> well, let's go by parts :
>
> 1) call/cc in principle will capture the complete continuation of a 
> expression, right? Delimited continuation will capture ... welll, delimited 
> continuations. But delimited by what? By a prompt.
> In a delimited contninuation style(not really, but I dont want to mix 
> functions), we will do something like (+ 1 (prompt (* 2 (call/cc (lambda 
> (k) (set! x k) 2)
>
> this will set x with (* 2 []), because delimited capture from prompt.  
> call/cc in racket works in a similar way, if around the most extern s-exp 
> exists a prompt.
>
> 2) well, when I dont know exactly why begin dont work and lambda do, but I 
> have a clue : if you do (syntax->datum (expand '(begin (print "hi") (print 
> "hello" in drracket, you will receive
> '(begin (#%app print '"hi") (#%app print '"hello"))
>
> and if you do (syntax->datum (expand '((lambda () (print "hi") (print 
> "hello") you will receive '(#%app (lambda () (#%app print '"hi") (#%app 
> print '"hello")))
>
> Probably, as begin dont is involved in a #%app, he dont is invoked with a 
> prompt. But this is just speculation of my part.
>
> Em qui, 25 de out de 2018 às 14:13, > 
> escreveu:
>
>> Thank Joao
>>
>> I change my code like this:
>>
>> #lang racket
>>
>>
>> ((lambda ()
>>
>>   (define saved-k #f)
>>
>>   (println (+ 1 (call/cc
>> (lambda (k) ; k is the captured continuation
>>   (set! saved-k k)
>>   0
>>
>>   (println 'hello)
>>
>>   (saved-k 100)
>>
>>
>>   ))
>>
>> Now it works as I expected.
>>
>> But why?
>>
>> "begin" special form packages a sequence of expressions into a single 
>> expression. After compiled, it should be continuation-passing style.
>>
>> "lambda" special form also have a sequence of expressions in it.
>>
>> They are essentially the same, except the lambda can be applied.
>>
>> What is prompt you mentioned?
>>
>> I have not heard of this concept before.
>>
>> Can you tell me something about the prompt?
>>
>> Thanks.
>>
>> On Friday, October 26, 2018 at 12:40:37 AM UTC+8, Joao Pedro Abreu De 
>> Souza wrote:
>>>
>>> Well, call/cc is like (in racket) delimited continuation, and have a 
>>> implicit prompt around a s-exp, so, as begin is a macro, he don't create a 
>>> prompt. The continuation captured is (+ 1 []) in your example.
>>>
>>> If you change the begin to a let, this works, because let expand to a 
>>> application of a lambda, so create prompt as you expected
>>>
>>> Em quinta-feira, 25 de outubro de 2018,  escreveu:
>>>
 Dear all,

 I am learning call/cc in racket, so I wrote some experiment code:

 #lang racket

 (begin

   (define saved-k #f)

   (+ 1 (call/cc
 (lambda (k) ; k is the captured continuation
   (set! saved-k k)
   0)))

   (println 'hello)

   (saved-k 100)

   ;; why 'hello not print more than once??
  
   )

 The execution result of this codeis:

 1
 hello
 101


 It does not meet my expectation, my expectation is :


 1
 hello
 101
 hello
 101
 loop


 About continuation, for example:

 assume  exp1 is:
 (+ 1 (call/cc
 (lambda (k) ; k is the captured continuation
   (set! saved-k k)
   0)))

 exp2 is:
 (println 'hello)

 Whether exp2 is the continuation of exp1

 Are these two expressions equivalent?

 (begin
   (exp1)
   (exp2))


 (exp1 (lambda (v)
 (exp2 continuation-of-exp2)))


 So exp2 is exp1's continuation?

 It makes me confused.

 Excuse me, this question may be stupid, forgive my ignorance...

 Thanks.

 Br,
 serioadamo97

 -- 
 You received this message because you are subscribed to the Google 
 Groups "Racket Users" group.
 To unsubscribe from this group and stop receiving emails from it, send 
 an email to racket-users...@googlegroups.com.
 For more options, visit https://groups.google.com/d/optout.

>>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to racket-users...@googlegroups.com .

Re: [racket-users] confusion about call-with-current-continuation

2018-10-25 Thread Joao Pedro Abreu De Souza
well, let's go by parts :

1) call/cc in principle will capture the complete continuation of a
expression, right? Delimited continuation will capture ... welll, delimited
continuations. But delimited by what? By a prompt.
In a delimited contninuation style(not really, but I dont want to mix
functions), we will do something like (+ 1 (prompt (* 2 (call/cc (lambda
(k) (set! x k) 2)

this will set x with (* 2 []), because delimited capture from prompt.
call/cc in racket works in a similar way, if around the most extern s-exp
exists a prompt.

2) well, when I dont know exactly why begin dont work and lambda do, but I
have a clue : if you do (syntax->datum (expand '(begin (print "hi") (print
"hello" in drracket, you will receive
'(begin (#%app print '"hi") (#%app print '"hello"))

and if you do (syntax->datum (expand '((lambda () (print "hi") (print
"hello") you will receive '(#%app (lambda () (#%app print '"hi") (#%app
print '"hello")))

Probably, as begin dont is involved in a #%app, he dont is invoked with a
prompt. But this is just speculation of my part.

Em qui, 25 de out de 2018 às 14:13,  escreveu:

> Thank Joao
>
> I change my code like this:
>
> #lang racket
>
>
> ((lambda ()
>
>   (define saved-k #f)
>
>   (println (+ 1 (call/cc
> (lambda (k) ; k is the captured continuation
>   (set! saved-k k)
>   0
>
>   (println 'hello)
>
>   (saved-k 100)
>
>
>   ))
>
> Now it works as I expected.
>
> But why?
>
> "begin" special form packages a sequence of expressions into a single
> expression. After compiled, it should be continuation-passing style.
>
> "lambda" special form also have a sequence of expressions in it.
>
> They are essentially the same, except the lambda can be applied.
>
> What is prompt you mentioned?
>
> I have not heard of this concept before.
>
> Can you tell me something about the prompt?
>
> Thanks.
>
> On Friday, October 26, 2018 at 12:40:37 AM UTC+8, Joao Pedro Abreu De
> Souza wrote:
>>
>> Well, call/cc is like (in racket) delimited continuation, and have a
>> implicit prompt around a s-exp, so, as begin is a macro, he don't create a
>> prompt. The continuation captured is (+ 1 []) in your example.
>>
>> If you change the begin to a let, this works, because let expand to a
>> application of a lambda, so create prompt as you expected
>>
>> Em quinta-feira, 25 de outubro de 2018,  escreveu:
>>
>>> Dear all,
>>>
>>> I am learning call/cc in racket, so I wrote some experiment code:
>>>
>>> #lang racket
>>>
>>> (begin
>>>
>>>   (define saved-k #f)
>>>
>>>   (+ 1 (call/cc
>>> (lambda (k) ; k is the captured continuation
>>>   (set! saved-k k)
>>>   0)))
>>>
>>>   (println 'hello)
>>>
>>>   (saved-k 100)
>>>
>>>   ;; why 'hello not print more than once??
>>>
>>>   )
>>>
>>> The execution result of this codeis:
>>>
>>> 1
>>> hello
>>> 101
>>>
>>>
>>> It does not meet my expectation, my expectation is :
>>>
>>>
>>> 1
>>> hello
>>> 101
>>> hello
>>> 101
>>> loop
>>>
>>>
>>> About continuation, for example:
>>>
>>> assume  exp1 is:
>>> (+ 1 (call/cc
>>> (lambda (k) ; k is the captured continuation
>>>   (set! saved-k k)
>>>   0)))
>>>
>>> exp2 is:
>>> (println 'hello)
>>>
>>> Whether exp2 is the continuation of exp1
>>>
>>> Are these two expressions equivalent?
>>>
>>> (begin
>>>   (exp1)
>>>   (exp2))
>>>
>>>
>>> (exp1 (lambda (v)
>>> (exp2 continuation-of-exp2)))
>>>
>>>
>>> So exp2 is exp1's continuation?
>>>
>>> It makes me confused.
>>>
>>> Excuse me, this question may be stupid, forgive my ignorance...
>>>
>>> Thanks.
>>>
>>> Br,
>>> serioadamo97
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Racket Users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to racket-users...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] confusion about call-with-current-continuation

2018-10-25 Thread serioadamo97
Thank Joao

I change my code like this:

#lang racket


((lambda ()
   
  (define saved-k #f)

  (println (+ 1 (call/cc
(lambda (k) ; k is the captured continuation
  (set! saved-k k)
  0

  (println 'hello)

  (saved-k 100)


  ))

Now it works as I expected.

But why?

"begin" special form packages a sequence of expressions into a single 
expression. After compiled, it should be continuation-passing style.

"lambda" special form also have a sequence of expressions in it.

They are essentially the same, except the lambda can be applied.

What is prompt you mentioned?

I have not heard of this concept before.

Can you tell me something about the prompt?

Thanks.

On Friday, October 26, 2018 at 12:40:37 AM UTC+8, Joao Pedro Abreu De Souza 
wrote:
>
> Well, call/cc is like (in racket) delimited continuation, and have a 
> implicit prompt around a s-exp, so, as begin is a macro, he don't create a 
> prompt. The continuation captured is (+ 1 []) in your example.
>
> If you change the begin to a let, this works, because let expand to a 
> application of a lambda, so create prompt as you expected
>
> Em quinta-feira, 25 de outubro de 2018, > 
> escreveu:
>
>> Dear all,
>>
>> I am learning call/cc in racket, so I wrote some experiment code:
>>
>> #lang racket
>>
>> (begin
>>
>>   (define saved-k #f)
>>
>>   (+ 1 (call/cc
>> (lambda (k) ; k is the captured continuation
>>   (set! saved-k k)
>>   0)))
>>
>>   (println 'hello)
>>
>>   (saved-k 100)
>>
>>   ;; why 'hello not print more than once??
>>  
>>   )
>>
>> The execution result of this codeis:
>>
>> 1
>> hello
>> 101
>>
>>
>> It does not meet my expectation, my expectation is :
>>
>>
>> 1
>> hello
>> 101
>> hello
>> 101
>> loop
>>
>>
>> About continuation, for example:
>>
>> assume  exp1 is:
>> (+ 1 (call/cc
>> (lambda (k) ; k is the captured continuation
>>   (set! saved-k k)
>>   0)))
>>
>> exp2 is:
>> (println 'hello)
>>
>> Whether exp2 is the continuation of exp1
>>
>> Are these two expressions equivalent?
>>
>> (begin
>>   (exp1)
>>   (exp2))
>>
>>
>> (exp1 (lambda (v)
>> (exp2 continuation-of-exp2)))
>>
>>
>> So exp2 is exp1's continuation?
>>
>> It makes me confused.
>>
>> Excuse me, this question may be stupid, forgive my ignorance...
>>
>> Thanks.
>>
>> Br,
>> serioadamo97
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to racket-users...@googlegroups.com .
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] confusion about call-with-current-continuation

2018-10-25 Thread Joao Pedro Abreu De Souza
Well, call/cc is like (in racket) delimited continuation, and have a
implicit prompt around a s-exp, so, as begin is a macro, he don't create a
prompt. The continuation captured is (+ 1 []) in your example.

If you change the begin to a let, this works, because let expand to a
application of a lambda, so create prompt as you expected

Em quinta-feira, 25 de outubro de 2018,  escreveu:

> Dear all,
>
> I am learning call/cc in racket, so I wrote some experiment code:
>
> #lang racket
>
> (begin
>
>   (define saved-k #f)
>
>   (+ 1 (call/cc
> (lambda (k) ; k is the captured continuation
>   (set! saved-k k)
>   0)))
>
>   (println 'hello)
>
>   (saved-k 100)
>
>   ;; why 'hello not print more than once??
>
>   )
>
> The execution result of this codeis:
>
> 1
> hello
> 101
>
>
> It does not meet my expectation, my expectation is :
>
>
> 1
> hello
> 101
> hello
> 101
> loop
>
>
> About continuation, for example:
>
> assume  exp1 is:
> (+ 1 (call/cc
> (lambda (k) ; k is the captured continuation
>   (set! saved-k k)
>   0)))
>
> exp2 is:
> (println 'hello)
>
> Whether exp2 is the continuation of exp1
>
> Are these two expressions equivalent?
>
> (begin
>   (exp1)
>   (exp2))
>
>
> (exp1 (lambda (v)
> (exp2 continuation-of-exp2)))
>
>
> So exp2 is exp1's continuation?
>
> It makes me confused.
>
> Excuse me, this question may be stupid, forgive my ignorance...
>
> Thanks.
>
> Br,
> serioadamo97
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] confusion about call-with-current-continuation

2018-10-25 Thread serioadamo97
Dear all,

I am learning call/cc in racket, so I wrote some experiment code:

#lang racket

(begin

  (define saved-k #f)

  (+ 1 (call/cc
(lambda (k) ; k is the captured continuation
  (set! saved-k k)
  0)))

  (println 'hello)

  (saved-k 100)

  ;; why 'hello not print more than once??
 
  )

The execution result of this codeis:

1
hello
101


It does not meet my expectation, my expectation is :


1
hello
101
hello
101
loop


About continuation, for example:

assume  exp1 is:
(+ 1 (call/cc
(lambda (k) ; k is the captured continuation
  (set! saved-k k)
  0)))

exp2 is:
(println 'hello)

Whether exp2 is the continuation of exp1

Are these two expressions equivalent?

(begin
  (exp1)
  (exp2))


(exp1 (lambda (v)
(exp2 continuation-of-exp2)))


So exp2 is exp1's continuation?

It makes me confused.

Excuse me, this question may be stupid, forgive my ignorance...

Thanks.

Br,
serioadamo97

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.