-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdayfour.rkt
64 lines (49 loc) · 1.39 KB
/
dayfour.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#lang racket
(define (square x)
(* x x))
;; more obfuscated functions, please
(define (head x) (car x))
(define (tail x) (cdr x))
;; foldl --> (foldl - '(1 2 3) 9)
;; = (((9 - 1) - 2) - 3) = 3
(define (foldl-helper op lst total)
(if (null? lst)
total
(foldl-helper op (tail lst) (op total (head lst)))))
(display "should be 9 ==> ")
(foldl-helper - '(1 2 3) 9)
;; foldr --> (foldr - '(1 2 3) 9) = -7
(define (foldr-helper op lst start)
(if (null? lst)
start
(op (head lst) (foldr-helper op (tail lst) start))))
(display "should be -7 ==> ")
(foldr-helper - '(1 2 3) 9)
;; gets the length of a list
(define (lst-length lst)
(foldl-helper (lambda (x y) (+ x 1)) lst 0))
(display "should be 5 ==> ")
(lst-length (list 1 3 2 4 3 8)) ; ==> 5
;; gets the lesser of two numbers (or either if the same)
(define (min-of-two x y)
(if (< x y)
x
y))
;; gets the least number of a list of numbers
(define (min-lst lst)
(foldl-helper min-of-two lst +inf.0))
(display "should be 1 ==> ")
(min-lst (list 7 2 8 3 1 4)) ; ==> 1
;; gets the greater of two numbers (or either if the same)
(define (max-of-two x y)
(if (> x y)
x
y))
;; gets the greatest number of a list of numbers
(define (max-lst lst)
(foldl-helper max-of-two lst -inf.0))
(display "should be 8 ==> ")
(max-lst (list 7 2 8 3 1 4)) ; ==> 8
(define (id lst)
(foldl-helper (lambda (arr elem) (cons elem arr)) lst '()))
(id (list 1 2 3))