-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path2.20.scm
executable file
·66 lines (56 loc) · 1.86 KB
/
2.20.scm
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
(load "myhelpers.scm")
;Exercise 2.20
(define (same-parity n . l)
(define (test num)
(if (even? num)
even?
odd?))
(define (recur f l)
(cond ((null? l) '())
(else (if (f (car l))
(cons (car l) (recur f (cdr l)))
(recur f (cdr l))))))
(recur (test n) (cons n l))) ;(cons n l) to add initial number to front
;test
(assert (same-parity 1 2 3 4 5 6) (list 1 3 5))
(assert (same-parity 2 3 4 5 6 7) (list 2 4 6))
;Another recursive implementation
(define (same-parity n . l)
(define (filter f lst)
(cond ((null? lst) '())
((f (car lst)) (cons (car lst) (filter f (cdr lst))))
(else (filter f (cdr lst)))))
(if (even? n)
(cons n (filter even? (cdr l)))
(cons n (filter odd? (cdr l)))))
;(if (even? n)
(assert (same-parity 1 2 3 4 5 6) (list 1 3 5))
(assert (same-parity 2 3 4 5 6 7) (list 2 4 6))
;Iteriverly
(define (same-parity-iter n . l)
(define (test n)
(if (even? n)
even?
odd?))
(define (iter f newlist oldlist)
(if (null? oldlist)
(cons n newlist)
(let ((item (car oldlist)))
(if (f item)
(iter f (append newlist (list item)) (cdr oldlist))
(iter f newlist (cdr oldlist))))))
(iter (test n) '() l))
;Test
(assert (same-parity-iter 1 2 3 4 5 6) (list 1 3 5))
(assert (same-parity-iter 2 3 4 5 6 7) (list 2 4 6))
;Another iterative implementation
(define (same-parity-iter n . l)
(define (iter f oldlist newlist)
(cond ((null? oldlist) (cons n newlist))
((f (car oldlist)) (iter f (cdr oldlist) (append newlist (list (car oldlist)))))
(else (iter f (cdr oldlist) newlist))))
(let ((t? (if (even? n) even? odd?)))
(iter t? l '())))
;Test
(assert (same-parity-iter 1 2 3 4 5 6) (list 1 3 5))
(assert (same-parity-iter 2 3 4 5 6 7) (list 2 4 6))