[racket] could somebody tell me how this code works?

4 views
Skip to first unread message

김태윤

unread,
Oct 5, 2010, 7:30:34 AM10/5/10
to us...@racket-lang.org
hello 
I am a scheme beginner
I am learning big-bang function with the book, how to design programs 
but I still have no idea how big-bang function exactly work.

#lang racket
(require 2htdp/image)
(require 2htdp/universe)
(define (handle-draw model) (text model 18 "blue"))
(define (handle-key old-model key) key)
(big-bang ""                           ; initial model is empty string
         (check-with string?)         ; model is a string
         (on-draw handle-draw 400 50) ; display text in 150x50 pixel window
         (on-key handle-key))

could somebody please tell me how this code exactly work?
my guessing is this 

there is only one checking function. 
if it is true, keep big-bang going. 
if it is false, it stops.
the first argument of big-bang "" is going to be sent to handle-draw by on-draw
if key pressed, that handle-draw's return value is going to be sent to handle-key as second argument by on-key
big-bang's first argument "" is changed to the handle-key's return value.
and big-bang start again from the beginning with that value as first argument

I still have no idea about the old-model argument.
is check-with function must be there?

thanks in advanced

Matthias Felleisen

unread,
Oct 5, 2010, 9:03:00 AM10/5/10
to 김태윤, us...@racket-lang.org

See
http://www.ccs.neu.edu/home/matthias/HtDP2e/htdp2e-part1.html#(part._.D.K._design-world)
and for something like 'check-with' see
http://docs.racket-lang.org/teachpack/2htdpuniverse.html#(form._((lib._2htdp/universe..rkt)._check-with))

> _________________________________________________
> For list-related administrative tasks:
> http://lists.racket-lang.org/listinfo/users

_________________________________________________
For list-related administrative tasks:
http://lists.racket-lang.org/listinfo/users

Stephen Bloch

unread,
Oct 5, 2010, 9:20:41 AM10/5/10
to 김태윤, us...@racket-lang.org

On Oct 5, 2010, at 7:30 AM, 김태윤 wrote:

> #lang racket
> (require 2htdp/image)
> (require 2htdp/universe)
> (define (handle-draw model) (text model 18 "blue"))
> (define (handle-key old-model key) key)
> (big-bang "" ; initial model is empty string
> (check-with string?) ; model is a string
> (on-draw handle-draw 400 50) ; display text in 150x50 pixel window
> (on-key handle-key))
>
> could somebody please tell me how this code exactly work?
> my guessing is this
>
> there is only one checking function.
> if it is true, keep big-bang going.
> if it is false, it stops.

Not exactly: the "check-with" is just to catch programming errors, and to document that the model is supposed to be a string. If I made a mistake, and my key handler returned something other than a string, it would catch that mistake.

> the first argument of big-bang "" is going to be sent to handle-draw by on-draw
> if key pressed, that handle-draw's return value is going to be sent to handle-key as second argument by on-key
> big-bang's first argument "" is changed to the handle-key's return value.
> and big-bang start again from the beginning with that value as first argument
>
> I still have no idea about the old-model argument.

That's sort of right, but a confusing way to look at it. Here's a simpler way.

At all times, there is a "model", which is initially the first argument of big-bang.
Every time a key is pressed, the key handler is called with the current model and the key; whatever the key handler returns becomes the new model.
Every time the model changes (for example, after the key handler returns), the draw handler is called with the current model; whatever the draw handler returns is shown on the screen.

> is check-with function must be there?

No, this animation would still work without the (check-with string?) .

Matthias pointed you to one place to read about this. Another is at http://www.picturingprograms.com/pdf/chap06.pdf .


Stephen Bloch
sbl...@adelphi.edu

Reply all
Reply to author
Forward
0 new messages