Re: [racket-users] Button click callback syntax

2020-05-13 Thread Philip Benade
Hi David

Thank you for replying to my question. 

I have a much better understanding of what is happening now, especially 
about passing a function instead the result of a function. Thank you.

Regards
Philip

On Tuesday, 12 May 2020 20:08:58 UTC+2, David Storrs wrote:
>
> Hi Philip, 
>
> The essence here is when evaluation happens: 
>
> ;  All of these are equivalent: 
> (define foo (lambda () 7) 
> (define foo (thunk 7))  ; 'thunk' is a term of art meaning "function 
> of 0 arguments" 
> (define (foo) 7) 
>
> Try running the following code in the repl: 
> ; start code 
> (define (general-callback msg-name msg-text message-box-text) ; 
> function of 3 arguments 
>   'result-of-general-callback) 
> general-callback 
> (general-callback "name" "text" "msg box text") 
> ; end code 
>
> You'll see that 'general-callback' returns: 
> #, which is the actual function 
> and (general-callback ...) returns 'result-of-general-callback, which 
> is the result of invoking the function. 
>
>
> In your GUI code, you want to be passing the function itself, not the 
> result of invoking the function. 
>
> Does that make sense? 
>
>
> On Tue, May 12, 2020 at 12:34 PM Philip Benade  > wrote: 
> > 
> > Hi All 
> > 
> > I am new to Racket and I have found a situation where I'm not certain 
> why Racket works the way it does. Unfortunately I am not sure of the 
> vocabulary so I will describe the situation with some code: 
> > 
> > 
> > ; A button made this way works as I would expect, it waits for 30 
> minutes and then shows a message-box and plays a sound. 
> > (new button% [parent long-break-panel] 
> >  [label "Long Break"] 
> >  ; Callback procedure for a button click: 
> >  [callback (lambda (button event) 
> >  (send long-break-msg set-label "Long break 
> timer started") 
> >  (general-timer 30 long-break-callback))]) 
> > 
> > ; A button made this way immediately shows the message box and plays the 
> sound. 
> > (new button% [parent long-break-panel] 
> >  [label "Long Break"] 
> >  ; Callback procedure for a button click: 
> >  [callback (lambda (button event) 
> >  (send long-break-msg set-label "Long break 
> timer started") 
> >  (general-timer 30 (general-callback 
> long-break-msg "Not on long break" "30 Minutes have passed, back to 
> work.")))]) 
> > 
> > ; A button made this way also immediately shows the message box and 
> plays the sound. Why is it different when the function name is surrounded 
> by parentheses? 
> > (new button% [parent long-break-panel] 
> >  [label "Long Break"] 
> >  ; Callback procedure for a button click: 
> >  [callback (lambda (button event) 
> >  (send long-break-msg set-label "Long break 
> timer started") 
> >  (general-timer 30 (long-break-callback)))]) 
> > 
> > ; The documentation for buttons say they require a callback. I 
> interpreted this to mean a function that will execute when the button is 
> clicked. 
> > (define general-callback 
> >   (lambda (msg-name msg-text message-box-text) 
> > (time-expired-sound) 
> > (send msg-name set-label msg-text) 
> > (message-box "Timer expired" message-box-text frame))) 
> > 
> > ; Why does wrapping the function so that it takes no parameters change 
> how it gets executed? 
> > (define long-break-callback 
> >   (lambda () 
> > (general-callback long-break-msg "Not on long break" "30 Minutes 
> have passed, back to work."))) 
> > 
> > 
> > Is anyone able to explain why I am getting these different behaviors 
> from these buttons? 
> > 
> > Regards 
> > Philip 
> > 
> > -- 
> > 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...@googlegroups.com . 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/3a6effce-915e-42c8-aff7-56fc23524e4c%40googlegroups.com.
>  
>
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/56a40f46-d3da-41c5-b737-2a67f7421bfe%40googlegroups.com.


Re: [racket-users] Button click callback syntax

2020-05-12 Thread David Storrs
Hi Philip,

The essence here is when evaluation happens:

;  All of these are equivalent:
(define foo (lambda () 7)
(define foo (thunk 7))  ; 'thunk' is a term of art meaning "function
of 0 arguments"
(define (foo) 7)

Try running the following code in the repl:
; start code
(define (general-callback msg-name msg-text message-box-text) ;
function of 3 arguments
  'result-of-general-callback)
general-callback
(general-callback "name" "text" "msg box text")
; end code

You'll see that 'general-callback' returns:
#, which is the actual function
and (general-callback ...) returns 'result-of-general-callback, which
is the result of invoking the function.


In your GUI code, you want to be passing the function itself, not the
result of invoking the function.

Does that make sense?


On Tue, May 12, 2020 at 12:34 PM Philip Benade  wrote:
>
> Hi All
>
> I am new to Racket and I have found a situation where I'm not certain why 
> Racket works the way it does. Unfortunately I am not sure of the vocabulary 
> so I will describe the situation with some code:
>
>
> ; A button made this way works as I would expect, it waits for 30 minutes and 
> then shows a message-box and plays a sound.
> (new button% [parent long-break-panel]
>  [label "Long Break"]
>  ; Callback procedure for a button click:
>  [callback (lambda (button event)
>  (send long-break-msg set-label "Long break timer 
> started")
>  (general-timer 30 long-break-callback))])
>
> ; A button made this way immediately shows the message box and plays the 
> sound.
> (new button% [parent long-break-panel]
>  [label "Long Break"]
>  ; Callback procedure for a button click:
>  [callback (lambda (button event)
>  (send long-break-msg set-label "Long break timer 
> started")
>  (general-timer 30 (general-callback long-break-msg 
> "Not on long break" "30 Minutes have passed, back to work.")))])
>
> ; A button made this way also immediately shows the message box and plays the 
> sound. Why is it different when the function name is surrounded by 
> parentheses?
> (new button% [parent long-break-panel]
>  [label "Long Break"]
>  ; Callback procedure for a button click:
>  [callback (lambda (button event)
>  (send long-break-msg set-label "Long break timer 
> started")
>  (general-timer 30 (long-break-callback)))])
>
> ; The documentation for buttons say they require a callback. I interpreted 
> this to mean a function that will execute when the button is clicked.
> (define general-callback
>   (lambda (msg-name msg-text message-box-text)
> (time-expired-sound)
> (send msg-name set-label msg-text)
> (message-box "Timer expired" message-box-text frame)))
>
> ; Why does wrapping the function so that it takes no parameters change how it 
> gets executed?
> (define long-break-callback
>   (lambda ()
> (general-callback long-break-msg "Not on long break" "30 Minutes have 
> passed, back to work.")))
>
>
> Is anyone able to explain why I am getting these different behaviors from 
> these buttons?
>
> Regards
> Philip
>
> --
> 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.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/3a6effce-915e-42c8-aff7-56fc23524e4c%40googlegroups.com.

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAE8gKodGj1dFEcjW-ED9GdXsy37F0gCsxfHbhPuFxOCXwWgToQ%40mail.gmail.com.