Skip to content

Latest commit

 

History

History
532 lines (374 loc) · 7.36 KB

htdp.org

File metadata and controls

532 lines (374 loc) · 7.36 KB

How to Design Programs

Exercise

Part 1. Fixed-Size Data

1

(define x 5)
(define y 12)

(sqrt (+ (* x x) (* y y)))

2

(define prefix "hello")
(define suffix "world")
(string-append prefix "_" suffix)

3

(define str "helloworld")
(define i 5)

(string-append (substring str 0 i)
               "_"
               (substring str i))

4

(define str "helloworld")
(define ind "0123456789")
(define i 5)

(string-append (substring str 0 i)
               (substring str (+ i 1)))

[0, 9]

5

#lang htdp/bsl

(require 2htdp/image)

(define COLOR "aquamarine")
(define HEIGHT 60)
(define WIDTH 300)

(define h HEIGHT)
(define w (/ WIDTH 3))

(beside
 (triangle/sas h 90 w "solid" COLOR)
 (rectangle    w h "solid" COLOR)
 (triangle/ass 90 h w "solid" COLOR))

6

(require 2htdp/image)

(define cat (bitmap/url "https://htdp.org/2019-02-24/cat1.png"))

(* (image-width cat) (image-height cat))

7

(define sunny #true)
(define friday #false)

(or (not sunny) friday)

4

8

(require 2htdp/image)

(define cat (bitmap/url "https://htdp.org/2019-02-24/cat1.png"))

(if (>= (image-height cat) (image-width cat))
    "tall"
    "wide")
(require 2htdp/image)

;;                     宽 高
(define pic (rectangle 100 50 "solid" "orange"))

(if (>= (image-height pic) (image-width pic))
    "tall"
    "wide")
(require 2htdp/image)

;; (define image (circle 100 "solid" "green"))
;; (define image (rectangle 100 50 "solid" "green"))
(define image (rectangle 50 100 "solid" "green"))

(define width  (image-width image))
(define height (image-height image))

(if (> width height)
    "wide"
    (if (< width height)
        "tall"
        "square"))

9

(require 2htdp/image)

;; (define in "hello")
;; (define in (circle 5 "solid" "red"))
;; (define in 0)
(define in #false)

(if (string? in)
    (string-length in)
    (if (image? in)
        (* (image-width in) (image-height in))
        (if (number? in)
            (if (<= in 0)
                in
                (- in 1))
            (if in
                10
                20))))

10

11

(define (distance x y)
  (sqrt (+ (* x x) (* y y))))

(distance 3 4)
(distance 5 12)
(distance 5 0)
(distance 1 1)

12

(define (cvolume a)
  (* a a a))

(define (csurface a)
  (* 6 (* a a)))

(cvolume 1)
(csurface 1)

13

(define (string-first s) (substring s 0 1))

(string-first "hello")

14

(define (string-last s)
  (substring
   s
   (- (string-length s) 1)
   (string-length s)))

(string-last "hello")

15

(define (==> sunny friday)
  (or (not sunny) friday))

(==> #false #true)

16

(require 2htdp/image)

(define (image-area image)
  (* (image-width image) (image-height image)))

(image-area (rectangle 10 20 "solid" "pink"))

17

(require 2htdp/image)

(define (image-classify image)
  (if (> (image-width image) (image-height image))
      "wide"
      (if (< (image-width image) (image-height image))
          "tall"
          "square")))

(image-classify (circle 10 "solid" "blue"))
(image-classify (rectangle 10 20 "solid" "red"))
(image-classify (rectangle 20 10 "solid" "green"))

18

(define (string-join s1 s2)
  (string-append s1 "_" s2))

(string-join "hello" "world")

19

(define (string-insert str i)
  (string-append (substring str 0 i)
                 "_"
                 (substring str i (string-length str))))

(string-insert "helloworld" 5)
(string-insert "" 0)

20

(define (string-delete str i)
  (string-append (substring str 0 i)
                 (substring str (+ i 1))))

(string-delete "hello" 4)
;; can't deal with empty string
;; (string-delete "" 0)

21

(+ (ff 1) (ff 1)) 中,DrRacket 的 stepper 没有重用 (ff 1) 的值。

22

23

24

25

26

27

(define base-attendees 120)
(define base-price 5.0)
(define attendees-change-per-cent 15)

(define (attendees ticket-price)
  (- base-attendees
     (* (/ (- ticket-price base-price) 0.1)
        attendees-change-per-cent)))

(define (revenue ticket-price)
  (* ticket-price (attendees ticket-price)))

(define fix-cost 0)
(define cost-per-attendee 1.5)

(define (cost ticket-price)
  (+ fix-cost (* cost-per-attendee (attendees ticket-price))))

(define (profit ticket-price)
  (- (revenue ticket-price)
     (cost ticket-price)))

(profit 1)
(profit 2)
(profit 3)
(profit 4)
(profit 5)

28

29

30

(define price-sensitivity (/ 15 0.1))

31

32

声音、摄像头、键盘、鼠标、触摸板、温度、亮度、手势、触摸屏、蓝牙、wifi

33

34

;; String -> String
;; extracts the first character from a non-empty string
;; given: "hello", expect: "h"
(define (string-first s)
  (substring s 0 1))

(string-first "hello")
(string-first "world")

35

;; String -> String
;; extracts the last character from a non-empty string
;; given: "hello", expect "o"
(define (string-last s)
  (substring s (sub1 (string-length s))))

(string-last "hello")
(string-last "world")

36

;; Image -> Number
;; Counts the number of pixels of img
;; given: (square 10 "solid" "red"), expect 100
;; given: (rectangle 5 10 "solid" "red"), expect 50
(define (image-area img)
  (* (image-width img) (image-height img)))

(require 2htdp/image)

(image-area (square 10 "solid" "red"))
(image-area (rectangle 5 10 "solid" "red"))

37

;; String -> String
;; return a string like s with the first char removed
;; given: "hello", expect "ello"
(define (string-rest s)
  (substring s 1))

(string-rest "hello")
(string-rest "world")

38

;; String -> String
;; return a string like s with the last char removed
;; given: "hello", expect "hell"
(define (string-remove-last s)
  (substring s 0 (sub1 (string-length s))))

(string-remove-last "hello")
(string-remove-last "world")

(check-duplicates)

39

40

41

42

43

时间 t,速度 sin(t),距离是多少?

x*sin(x) 的积分?

44

45

46

47

48

49

50

51

52

4

53