Skip to content

Instantly share code, notes, and snippets.

@vdeemann
Last active April 6, 2024 20:53
Show Gist options
  • Save vdeemann/d5dda656bc578eb49a77ef77e7d2a625 to your computer and use it in GitHub Desktop.
Save vdeemann/d5dda656bc578eb49a77ef77e7d2a625 to your computer and use it in GitHub Desktop.
how-to-design-programs
(+ 1 2)
(+ 1 (+ 1 (+ 1 1) 2) 3 4 5)
(+ 3 4)
;; Play with your computer’s mouse to find the menu that changes the
;; fraction into decimal expansion.
;; https://stackoverflow.com/a/76037629/8706936
;; Language > Choose Languages > "Beginning Student" > "Show Details"
;; > "Mixed fractions" or "Repeating decimals"
(/ 4 6)
;; Exercise 1. Add the following definitions for x and y to
;; DrRacket’s definitions area:
(define x 12)
(define y 5)
(sqrt (+ (expt x 2) (expt y 2)))
;;
;; Exercise 2. Add the following two lines to the definitions area:
(define prefix "hello")
(define suffix "world")
(string-append prefix "_" suffix)
;;
(+ (string-length "hello world") 20)
(+ (string-length (number->string 42)) 2)
; string-length: expects a string, given 42
;(+ (string-length 42) 1)
;; Exercise 3. Add the following two lines to the definitions area:
(define str "helloworld")
(define i 5)
(string-append (substring str 0 i) "_"
(substring str i (string-length str)))
;;
#|
(define str "helloworld")
(define ind "0123456789")
(define i 5)
|#
;;
;; Exercise 4. Use the same setup as in exercise 3 to create an
;; expression that deletes the ith position from str. Clearly this
;; expression creates a shorter string than the given one. Which values
;; for i are legitimate?
;;
;; answer: I'm not sure how to answer which values for i are legitimate
;; but I think i in (substring str 0 (- i 1)) is legitimate
(string-append (substring str 0 (- i 1))
(substring str i (string-length str)))
;;
(require 2htdp/image)
;; Explain how DrRacket determines the value of this expression:
;;
;; image-width of the circle is the width of an image in pixels which
;; is 20
;; +
;; image-height of the rectangle is the height which is 20
(+ (image-width (circle 10 "solid" "red"))
(image-height (rectangle 10 20 "solid" "blue")))
;; scene+line consumes a scene, four numbers, and a color to draw a
;; line into the given image. Experiment with it to see how it works.
(scene+line (rectangle 40 40 "solid" "gray")
20 5 20 100 "maroon")
;; Exercise 5. Use the 2htdp/image library to create the image of a
;; simple boat or tree. Make sure you can easily change the scale of
;; the entire image.
(define base 50)
(define height 24)
(place-image
(star 12 "solid" "green")
10 6
(place-image
(rectangle 5 20 "solid" "brown")
10 20
(rectangle base height "solid" "goldenrod")))
;;
#|
;; Exercise 6. Add the following line to the definitions area:
;; Create an expression that counts the number of pixels in the image.
(define cat .)
(* (image-width cat) (image-height cat))
;;
|#
;; Exercise 7. Boolean expressions can express some everyday problems.
;; Suppose you want to decide whether today is an appropriate day to go
;; to the mall. You go to the mall either if it is not sunny or if
;; today is Friday (because that is when stores post new sales items).
(define sunny #true)
(define friday #false)
;; Now create an expression that computes whether sunny is false or
;; friday is true. So in this particular case, the answer is #false.
;; (Why?)
;;
;; The answer is false because we have not'd the sunny value to false
;; otherwise if sunny remained true the or operation would take either
;; boolean value as one or all statements are true.
(or (not sunny) friday)
;; How many combinations of Booleans can you associate with sunny and
;; friday?
;;
;; 4
;;
#|
(define x 0)
(define inverse-of-x (/ 1 x))
; /: division by zero
(if (= x 0) 0 (/ 1 x))
|#
;; Explain what the following four comparison primitives determine
;; about numbers: <, <=, >, >=.
;;
;; <, takes two arguments and compares the first argument if its less
;; than the second
;; <=, takes two arguments and compares the first argument if its less
;; than or equal to the second
;; >, takes two arguments and compares the first argument if its
;; greater than the second
;; >=, takes two arguments and compares the first argument if its
;; greater than or equal to the second
;; look up documentation on string<=? and string>=? through HelpDesk
(define current-color "red")
(define next-color
(if (string=? "green" current-color) "yellow" "red"))
;; Exercise 8. Add the following line to the definitions area:
(define cat .)
(define (is-image-tall-or-wide img)
(if (>= (image-height img) (image-width img)) "tall" "wide"))
(is-image-tall-or-wide cat)
(is-image-tall-or-wide (rectangle 50 10 "solid" "gray"))
;; Create an expression that com- putes whether a picture is "tall",
;; "wide", or "square".
(define (is-image-tall-or-wide-or-square img)
(if (= (image-height img) (image-width img)) "square"
(if (> (image-height img) (image-width img)) "tall" "wide")))
(is-image-tall-or-wide-or-square (rectangle 10 10 "solid" "gray"))
(is-image-tall-or-wide-or-square (rectangle 100 10 "solid" "gray"))
(is-image-tall-or-wide-or-square (rectangle 10 100 "solid" "gray"))
;;
;(* (+ (string-length 42) 1) pi)
;; Exercise 9. Add the following line to the definitions area of
;; DrRacket:
(define (in value)
(if (string? value) (string-length value)
(if (image? value) (* (image-height value) (image-width value))
(if (number? value) (sub1 value)
(if value 10 20)))))
(+ 1 1)
(+ 2 2)
(* 3 3)
(- 4 2)
(/ 6 2)
(string-append "hello" " " "world")
(>= 10 10)
(<= -1 10)
(string=? "design" "tinker")
(and (or (= (string-length "hello world")
(string->number "11"))
(string=? "hello world" "good morning"))
(>= (+ (string-length "hello world") 60) 80))
(require 2htdp/image)
(* (image-width .) (image-height .))
(define (y x) (* x x))
(y 1)
(y 2)
(y 3)
(y 4)
(y 5)
; the missing entry for 10
(y 10)
#|
(define (picture-of-rocket height)
(place-image . 50 height (empty-scene 100 60)))
(picture-of-rocket 0)
(picture-of-rocket 10)
(picture-of-rocket 20)
(picture-of-rocket 30)
|#
(require 2htdp/universe)
(define (picture-of-rocket.v2 height)
(cond
[(<= height 60)
(place-image . 50 height
(empty-scene 100 60))]
[(> height 60)
(place-image . 50 60
(empty-scene 100 60))]))
(- 60 (/ (image-height .) 2))
(place-image . 50 (- 60 (image-height .))
(empty-scene 100 60))
(define (picture-of-rocket.v3 height)
(cond
[(<= height (- 60 (/ (image-height .) 2)))
(place-image . 50 height
(empty-scene 100 60))]
[(> height (- 60 (/ (image-height .) 2)))
(place-image . 50 (- 60 (/ (image-height .) 2))
(empty-scene 100 60))]))
#|
(define (picture-of-rocket.v4 h)
(cond
[(<= h ROCKET-CENTER-TO-TOP)
(place-image ROCKET 50 h (empty-scene WIDTH HEIGHT))]
[(> h ROCKET-CENTER-TO-TOP)
(place-image ROCKET
50 ROCKET-CENTER-TO-TOP
(empty-scene WIDTH HEIGHT))]))
;; How would you change the program to create a 200-by-400 scene?
(define WIDTH 200)
(define HEIGHT 400)
(define ROCKET .)
(define ROCKET-CENTER-TO-TOP
(- HEIGHT (/ (image-height ROCKET) 2)))
; run this animate command in the interactions pane
; (animate picture-of-rocket.v4)
|#
#|
(define CENTER 100)
(define HEIGHT (* 2 CENTER))
|#
#|
;; How would you change the program so that it depicts the landing of
;; a green UFO (unidentified flying object)?
(define (picture-of-green-ufo h)
(cond
[(<= h GREEN-UFO-CENTER-TO-TOP)
(place-image GREEN-UFO 50 h (empty-scene WIDTH HEIGHT))]
[(> h GREEN-UFO-CENTER-TO-TOP)
(place-image GREEN-UFO
50 GREEN-UFO-CENTER-TO-TOP
(empty-scene WIDTH HEIGHT))]))
(define GREEN-UFO
(overlay (circle 10 "solid" "green")
(rectangle 40 4 "solid" "green")))
(define GREEN-UFO-CENTER-TO-TOP
(- HEIGHT (/ (image-height GREEN-UFO) 2)))
; run this animate command in the interactions pane
; (animate picture-of-green-ufo)
|#
; constants
(define WIDTH 200)
(define HEIGHT 400)
;; How would you change the program so that the background is
;; al- ways blue?
(define MTSCN (overlay/align "right" "bottom"
(rectangle WIDTH 10 "solid" "gray")
(rectangle WIDTH HEIGHT "solid" "blue")))
(define ROCKET .)
;; How would you change the program so that the rocket lands on a flat
;; rock bed that is 10 pixels higher than the bottom of the scene?
;; Don’t forget to change the scenery, too.
(define ROCKET-CENTER-TO-TOP
(- (- HEIGHT 10) (/ (image-height ROCKET) 2)))
(define X-COORDINATE 50)
; functions
(define (picture-of-rocket.v5 h)
(cond
[(<= h ROCKET-CENTER-TO-TOP)
(place-image ROCKET X-COORDINATE h MTSCN)]
[(> h ROCKET-CENTER-TO-TOP)
(place-image ROCKET X-COORDINATE ROCKET-CENTER-TO-TOP MTSCN)]))
; run this animate command in the interactions pane
; (animate picture-of-rocket.v5)
#|
(define (picture-of-rocket t)
(cond
[(<= t ROCKET-CENTER-TO-TOP)
(place-image ROCKET 50 t MTSCN)]
[(> t ROCKET-CENTER-TO-TOP)
(place-image ROCKET
50 ROCKET-CENTER-TO-TOP
MTSCN)]))
(define V 3)
(define (distance t)
(* V t))
|#
#|
; properties of the "world" and the descending rocket
(define WIDTH 100)
(define HEIGHT 60)
(define V 3)
(define X 50)
; graphical constants
(define MTSCN (empty-scene WIDTH HEIGHT))
(define ROCKET .)
(define ROCKET-CENTER-TO-TOP
(- HEIGHT (/ (image-height ROCKET) 2)))
; functions
(define (picture-of-rocket.v6 t)
(cond
[(<= (distance t) ROCKET-CENTER-TO-TOP)
(place-image ROCKET X (distance t) MTSCN)]
[(> (distance t) ROCKET-CENTER-TO-TOP)
(place-image ROCKET X ROCKET-CENTER-TO-TOP MTSCN)]))
(define (distance t)
(* V t))
; run this animate command in the interactions pane
; (animate picture-of-rocket.v6)
|#
(define (sign x)
(cond
[(> x 0) 1]
[(= x 0) 0]
[(< x 0) -1]))
(sign -5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment