Re: [racket-users] Moving an image with the keyboard

2015-06-27 Thread Matthias Felleisen

Patrick, I get the sense that this is your first experience with programming 
and programming languages. 

If so, Realm is not the best way to get started. As it says in the Preface, 
it's for people with a first course on programming under their belt (either How 
to Design Programs /HtDPor some standard Java course). The Bootstrap curriculum 
is an adaptation of the HtDP ideas, which isn't about programming but designing 
programs systematically. It lays out general principles of doing that, starting 
with simple programs and slowly moving up the chain to complex forms of data 
and programs. At each stage the design principles are expanded to cope with a 
large space of design alternatives. One step in this design process is to 
devise a data representation (name a set of values) and state signatures (say 
in terms of these data representations what functions take in and return). BUT, 
the languages we use at this stage do NOT check whether what you state is 
correct. This is by design -- because if we do, we just increase the number of 
error messages beginners get and because many college students now go out on 
internships and program in such languages (JavaScript, Python, Ruby, etc). 

Bootstrap makes sure that all of this relates back to Mathematics as it is 
taught at around the middle school level. That was the original idea behind 
Racket and our educational dimension. 

Big-bang describes a world with plain mathematical terms: the states it keeps 
track of, how to map a state to another one when something happens (a clock 
tick, a key press) and how to render the state as an image (which is a value 
just like a number).  There is another clause you can add to big-bang programs 
(click on big-bang to open its documentation) that describes what kind of 
values big-bang is supposed to track: 

 (big-bang (ufo 70 60)
   (on-key move-UFO)
   (to-draw render-ufo)
   (check-with ufo?))

If you add this, run and press - the program will signal an error that tells 
you what went wrong. Try it out, and otherwise follow Jordan's advice or switch 
back to HtDP/2e: 

 file:///Users/matthias/0Unison/0Web/HtDP2e/Draft/index.html


-- Matthias




On Jun 27, 2015, at 8:50 AM, Patrick Ester wrote:

 Jordan,
 
 Thanks for the suggestion. Quick question, why aren't type signatures 
 required? I've dabbled with a language called Elm (elm-lang.org) in which 
 type signatures are mandatory, the code won't compile without them. Not that 
 you control this, but Realm of Racket doesn't say anything about type 
 signatures. I know that the Bootstrap curriculum, however, stresses 
 contracts. Coincidentally, I'll be attending a Bootstrap conference next 
 month. Anyways, everything works now, and I appreciate your help.
 
 
 On Friday, June 26, 2015 at 8:10:08 PM UTC-5, Jordan Johnson wrote:
 Hi Patrick,
 
 
 The problem you’re up against is that in the code you’ve attached, we don’t 
 have a clear picture of what the signatures for your functions are, nor of 
 the “world” upon which big-bang operates. Standard practice in HtDP-based 
 courses dictates that
 every big-bang program should have a data definition for the world in 
 comments, andevery function should have a signature and brief description of 
 its purpose in comments (as well as one or more examples/tests to illustrate 
 its use).
 
 
 Because you write (big-bang (ufo 70 60) ...), you clearly intend for your 
 “world” to be a ufo struct. So in order for your readers to not have to 
 guess at what you mean, you need a comment to accompany your struct 
 definition:
 
 
 ;; A World is a (ufo Number Number):
 (struct ufo (xpos ypos))
 ;; where xpos is ...
 ;;   and ypos is ...
 
 
 And say what xpos and ypos represent. Thereafter, when you write function 
 signatures, you can use World to describe the kind of data that’s passing in 
 or out, e.g.,
 
 
 ;; SIGNATURE: render-ufo : World - Image
 ;; PURPOSE: draws the UFO at the given location
 
 
 If you look at the Help Desk docs for 2htdp/universe, one fine thing it 
 gives you in the description of big-bang is the signature required for every 
 one of the handler functions. For example: big-bang’s first parameter must 
 be a World, and in
  [to-draw f]
 the signature of f must be
  ;; f : World - Image
 
 
 Once you’ve worked out what the signatures should be for all of your handler 
 functions, take a look at your code and start writing signatures for the 
 functions you’ve written. This will lead you to figure out what’s going 
 wrong.
 
 
 Cheers,
 jmj
 
 
 On Jun 26, 2015, at 4:42 PM, Patrick Ester patric...@gmail.com wrote:
 
 Dear All,
 
 I am not sure if this is the right place since I am working from the Realm 
 of Racket book. If not, please let me know where I can take my question. I 
 finished up chapter five, and wanted to modify the UFO example to move in 
 relation to the arrow keys. Here is the code that I wrote:
 
 #lang racket
 (require 2htdp/universe 2htdp/image)
 
 (define WIDTH 600)
 (define 

Re: [racket-users] Moving an image with the keyboard

2015-06-27 Thread Patrick Ester
Jordan,

Thanks for the suggestion. Quick question, why aren't type signatures required? 
I've dabbled with a language called Elm (elm-lang.org) in which type signatures 
are mandatory, the code won't compile without them. Not that you control this, 
but Realm of Racket doesn't say anything about type signatures. I know that the 
Bootstrap curriculum, however, stresses contracts. Coincidentally, I'll be 
attending a Bootstrap conference next month. Anyways, everything works now, and 
I appreciate your help.


On Friday, June 26, 2015 at 8:10:08 PM UTC-5, Jordan Johnson wrote:
 Hi Patrick,
 
 
 The problem you’re up against is that in the code you’ve attached, we don’t 
 have a clear picture of what the signatures for your functions are, nor of 
 the “world” upon which big-bang operates. Standard practice in HtDP-based 
 courses dictates that
 every big-bang program should have a data definition for the world in 
 comments, andevery function should have a signature and brief description of 
 its purpose in comments (as well as one or more examples/tests to illustrate 
 its use).
 
 
 Because you write (big-bang (ufo 70 60) ...), you clearly intend for your 
 “world” to be a ufo struct. So in order for your readers to not have to guess 
 at what you mean, you need a comment to accompany your struct definition:
 
 
 ;; A World is a (ufo Number Number):
 (struct ufo (xpos ypos))
 ;; where xpos is ...
 ;;   and ypos is ...
 
 
 And say what xpos and ypos represent. Thereafter, when you write function 
 signatures, you can use World to describe the kind of data that’s passing in 
 or out, e.g.,
 
 
 ;; SIGNATURE: render-ufo : World - Image
 ;; PURPOSE: draws the UFO at the given location
 
 
 If you look at the Help Desk docs for 2htdp/universe, one fine thing it gives 
 you in the description of big-bang is the signature required for every one of 
 the handler functions. For example: big-bang’s first parameter must be a 
 World, and in
  [to-draw f]
 the signature of f must be
  ;; f : World - Image
 
 
 Once you’ve worked out what the signatures should be for all of your handler 
 functions, take a look at your code and start writing signatures for the 
 functions you’ve written. This will lead you to figure out what’s going wrong.
 
 
 Cheers,
 jmj
 
 
 On Jun 26, 2015, at 4:42 PM, Patrick Ester patric...@gmail.com wrote:
 
 Dear All,
 
 I am not sure if this is the right place since I am working from the Realm of 
 Racket book. If not, please let me know where I can take my question. I 
 finished up chapter five, and wanted to modify the UFO example to move in 
 relation to the arrow keys. Here is the code that I wrote:
 
 #lang racket
 (require 2htdp/universe 2htdp/image)
 
 (define WIDTH 600)
 (define HEIGHT 600)
 ;;The image won't copy and paste, but you get the idea.
 (define IMAGE-of-ufo .)
 
 (struct ufo (xpos ypos))
 
 (define (move-up w)
   (add1 (ufo-xpos w)))
 
 (define (move-down w)
   (sub1 (ufo-xpos w)))
 
 (define (move-right w)
   (add1 (ufo-ypos w)))
 
 (define (move-left w)
   (sub1 (ufo-ypos w)))
 
 (define (render-ufo w)
   (place-image
    (scale 0.25 IMAGE-of-ufo) (ufo-xpos w) (ufo-ypos w)
    (empty-scene WIDTH HEIGHT)))
 
 (define (move-UFO w key)
   (cond [(key=? key up) (move-up w)]
     [(key=? key down) (move-down w)]
     [(key=? key left) (move-left w)]
     [(key=? key right) (move-right w)]
     [else w]))
 
 (big-bang (ufo 70 60)
   (on-key move-UFO)
   (to-draw render-ufo))
 
 When I click the Run button, the output initially looks good. However, when I 
 press an arrow key, I get an error. Attached is a screenshot with what I see 
 in the definitions pane.
 
 The error message in the interactions pane is:
 
 ufo-xpos: contract violation
   expected: ufo?
   given: 61
 
 I do not get why the contract is broken. The function place-image takes an 
 image, number, number, and a scene. What mistake am I making? Thanks in 
 advance.
 
 Patrick
 
 -- 
 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.
 Screen Shot 2015-06-26 at 6.26.21 PM.png

-- 
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] Moving an image with the keyboard

2015-06-26 Thread Jordan Johnson
Hi Patrick,

The problem you’re up against is that in the code you’ve attached, we don’t 
have a clear picture of what the signatures for your functions are, nor of the 
“world” upon which big-bang operates. Standard practice in HtDP-based courses 
dictates that
every big-bang program should have a data definition for the world in comments, 
and
every function should have a signature and brief description of its purpose in 
comments (as well as one or more examples/tests to illustrate its use).

Because you write (big-bang (ufo 70 60) ...), you clearly intend for your 
“world” to be a ufo struct. So in order for your readers to not have to guess 
at what you mean, you need a comment to accompany your struct definition:

;; A World is a (ufo Number Number):
(struct ufo (xpos ypos))
;; where xpos is ...
;;   and ypos is ...

And say what xpos and ypos represent. Thereafter, when you write function 
signatures, you can use World to describe the kind of data that’s passing in or 
out, e.g.,

;; SIGNATURE: render-ufo : World - Image
;; PURPOSE: draws the UFO at the given location

If you look at the Help Desk docs for 2htdp/universe, one fine thing it gives 
you in the description of big-bang is the signature required for every one of 
the handler functions. For example: big-bang’s first parameter must be a World, 
and in
 [to-draw f]
the signature of f must be
 ;; f : World - Image

Once you’ve worked out what the signatures should be for all of your handler 
functions, take a look at your code and start writing signatures for the 
functions you’ve written. This will lead you to figure out what’s going wrong.

Cheers,
jmj

On Jun 26, 2015, at 4:42 PM, Patrick Ester patrick.es...@gmail.com wrote:
 
 Dear All,
 
 I am not sure if this is the right place since I am working from the Realm of 
 Racket book. If not, please let me know where I can take my question. I 
 finished up chapter five, and wanted to modify the UFO example to move in 
 relation to the arrow keys. Here is the code that I wrote:
 
 #lang racket
 (require 2htdp/universe 2htdp/image)
 
 (define WIDTH 600)
 (define HEIGHT 600)
 ;;The image won't copy and paste, but you get the idea.
 (define IMAGE-of-ufo .)
 
 (struct ufo (xpos ypos))
 
 (define (move-up w)
  (add1 (ufo-xpos w)))
 
 (define (move-down w)
  (sub1 (ufo-xpos w)))
 
 (define (move-right w)
  (add1 (ufo-ypos w)))
 
 (define (move-left w)
  (sub1 (ufo-ypos w)))
 
 (define (render-ufo w)
  (place-image
   (scale 0.25 IMAGE-of-ufo) (ufo-xpos w) (ufo-ypos w)
   (empty-scene WIDTH HEIGHT)))
 
 (define (move-UFO w key)
  (cond [(key=? key up) (move-up w)]
[(key=? key down) (move-down w)]
[(key=? key left) (move-left w)]
[(key=? key right) (move-right w)]
[else w]))
 
 (big-bang (ufo 70 60)
  (on-key move-UFO)
  (to-draw render-ufo))
 
 When I click the Run button, the output initially looks good. However, when I 
 press an arrow key, I get an error. Attached is a screenshot with what I see 
 in the definitions pane.
 
 The error message in the interactions pane is:
 
 ufo-xpos: contract violation
  expected: ufo?
  given: 61
 
 I do not get why the contract is broken. The function place-image takes an 
 image, number, number, and a scene. What mistake am I making? Thanks in 
 advance.
 
 Patrick
 
 -- 
 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.
 Screen Shot 2015-06-26 at 6.26.21 PM.png

-- 
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] Moving an image with the keyboard

2015-06-26 Thread Patrick Ester
Dear All,

I am not sure if this is the right place since I am working from the Realm of 
Racket book. If not, please let me know where I can take my question. I 
finished up chapter five, and wanted to modify the UFO example to move in 
relation to the arrow keys. Here is the code that I wrote:

#lang racket
(require 2htdp/universe 2htdp/image)
 
(define WIDTH 600)
(define HEIGHT 600)
;;The image won't copy and paste, but you get the idea.
(define IMAGE-of-ufo .)
 
(struct ufo (xpos ypos))
 
(define (move-up w)
  (add1 (ufo-xpos w)))
 
(define (move-down w)
  (sub1 (ufo-xpos w)))
 
(define (move-right w)
  (add1 (ufo-ypos w)))
 
(define (move-left w)
  (sub1 (ufo-ypos w)))
 
(define (render-ufo w)
  (place-image
   (scale 0.25 IMAGE-of-ufo) (ufo-xpos w) (ufo-ypos w)
   (empty-scene WIDTH HEIGHT)))
 
(define (move-UFO w key)
  (cond [(key=? key up) (move-up w)]
[(key=? key down) (move-down w)]
[(key=? key left) (move-left w)]
[(key=? key right) (move-right w)]
[else w]))
 
(big-bang (ufo 70 60)
  (on-key move-UFO)
  (to-draw render-ufo))

When I click the Run button, the output initially looks good. However, when I 
press an arrow key, I get an error. Attached is a screenshot with what I see in 
the definitions pane.

The error message in the interactions pane is:

ufo-xpos: contract violation
  expected: ufo?
  given: 61

I do not get why the contract is broken. The function place-image takes an 
image, number, number, and a scene. What mistake am I making? Thanks in advance.

Patrick

-- 
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.