-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsolutions.rkt
67 lines (59 loc) · 1.26 KB
/
solutions.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
63
64
65
66
;#lang racket
; 1. length
(define (length* lst)
(if (null? lst)
0
(+ 1
(length* (cdr lst)))))
; 2. sum
(define (sum lst)
(if (null? lst)
0
(+ (car lst)
(sum (cdr lst)))))
; 3. last
(define (last* lst)
(if (null? (cdr lst))
(car lst)
(last (cdr lst))))
; 4. nth
(define (nth n lst)
(define (help i lst)
(cond [(null? lst) 'not-found]
[(= i n) (car lst)]
[else (help (+ 1 i) (cdr lst))]))
(help 0))
; 5. concat
(define (concat lst1 lst2)
(if (null? lst1)
lst2
(cons
(car lst1)
(concat (cdr lst1) lst2))))
; 6. map
(define (map* f lst)
(if (null? lst)
lst
(cons
(f (car lst))
(map* f (cdr lst)))))
; 7. filter
(define (filter* p lst)
(cond [(null? lst) lst]
[(p (car lst))
(cons
(car lst)
(filter* p (cdr lst)))]
[else (filter* p (cdr lst))]))
; 8. partition
(define (partition* p lst)
(define (help truthy falsy lst)
(cond [(null? lst) (cons truthy (list falsy))]
[(p (car lst))
(help (cons (car lst) truthy)
falsy
(cdr lst))]
[else (help truthy
(cons (car lst) falsy)
(cdr lst))]))
(help '() '() lst))