aboutsummaryrefslogtreecommitdiff
path: root/HW01.rkt
blob: 4885c3383a690edbef0dbc54e12cc162b7015610 (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#lang scheme

;; circlearea
;;
;; find the area of a circle with radius r
(define
  (circlearea r)
  (* 3.142 (expt r 2))
)

;; between?
;;
;; check whether a is between x and y
(define
  (between? a x y)
  (if
   (or
    (and (< y a) (> x a)) ;; y < a < x
    (and (> y a) (< x a)) ;; x < a < y
   )
   #t
   #f
  )
)

;; shorter
;;
;; returns the list with fewer elements
(define
  (shorter a b)
  (if
   (< (length a) (length b))
   a
   b
 )
)

;; righttri?
;;
;; checks whether 3 integers can be the side lengths
;; of a right triangle.
(define
  (righttri? a b c)
  (cond
    ;; a is largest (b^2 + c^2 = a^2)
    [ (and (> a b) (> a c)) (= (+ (expt b 2) (expt c 2)) (expt a 2)) ]
    
    ;; b is largest (a^2 + c^2 = b^2)
    [ (and (> b a) (> b c)) (= (+ (expt a 2) (expt c 2)) (expt b 2)) ]
    
    ;; c is largest (a^2 + b^2 = c^2)
    [ (and (> c a) (> c b)) (= (+ (expt a 2) (expt b 2)) (expt c 2)) ]
  )
)