-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathex063.rkt
37 lines (32 loc) · 1.43 KB
/
ex063.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname ex063) (read-case-sensitive #t) (teachpacks ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp") (lib "batch-io.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp") (lib "batch-io.rkt" "teachpack" "2htdp")) #f)))
; computes the distance of ap to the origin
(define (distance-to-0 ap)
(sqrt
(+ (sqr (posn-x ap))
(sqr (posn-y ap)))))
(check-expect (distance-to-0 (make-posn 3 4)) 5)
(check-expect (distance-to-0 (make-posn 8 6)) 10)
(check-expect (distance-to-0 (make-posn 5 12)) 13)
(distance-to-0 (make-posn 3 4))
;(sqrt (+ (sqr (posn-x (make-posn 3 4))) (sqr (posn-y (make-posn 3 4)))))
;(sqrt (+ (sqr 3) (sqr 4)))
;(sqrt (+ 9 16))
;(sqrt 25)
;5
(distance-to-0 (make-posn 6 (* 2 4)))
;(distance-to-0 (make-posn 6 8)
;(sqrt (+ (sqr (posn-x (make-posn 6 8))) (sqr (posn-y (make-posn 6 8)))))
;(sqrt (+ (sqr 6) (sqr 8)))
;(sqrt (+ 36 64))
;(sqrt 100)
;10
(+ (distance-to-0 (make-posn 12 5)) 10)
;(+ (sqrt (+ (sqr (posn-x (make-posn 12 5))) (sqr (posn-y (make-posn 12 5)))))
; 10)
;(+ (sqrt (+ (sqr 12) (sqr 5))) 10)
;(+ (sqrt (+ 144 25)) 10)
;(+ (sqrt 169) 10)
;(+ 13 10)
;23