-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.scm
96 lines (72 loc) · 2.17 KB
/
common.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
; Library of common procedures used in exercises which do not particularly participate
; in solution design but are used only as helpers and can be shared among.
(define true #t)
(define false #f)
(define (square x)
(* x x))
(define (cube x)
(* x x x))
(define (double x)
(* x 2))
(define (halve x)
(/ x 2))
(define (even? n)
(= (remainder n 2) 0))
(define (div a b)
(floor (/ a b)))
(define (divides? a b)
(= (remainder b a) 0))
(define (identity x) x)
(define close-enough?
(lambda (a b delta) (< (abs (- a b)) delta)))
; average of 2 values
(define (average x y)
(/ (+ x y) 2.0))
; avergae of 3.0
(define (average-of-3 x y z)
(/ (+ x y z) 3.0))
(define (inc x)
(+ x 1))
(define (gcd a b)
(if (= b 0)
a
(gcd b (remainder a b))))
; define nil as empty list.
(define nil '())
;;;; standard sequence procedures
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
(define (filter predicate sequence)
(cond ((null? sequence) nil)
((predicate (car sequence))
(cons (car sequence) (filter predicate (cdr sequence))))
(else (filter predicate (cdr sequence)))))
(define (enumerate-leaves tree)
(cond ((null? tree) nil)
((not (pair? tree)) (list tree))
(else (append (enumerate-leaves (car tree))
(enumerate-leaves (cdr tree))))))
(define (enumerate-interval low high)
(if (> low high)
nil
(cons low (enumerate-interval (+ low 1) high))))
(define (accumulate-n op init seqs)
(if (null? (car seqs))
nil
(cons (accumulate op init (map car seqs)) ; get cars of every list inside
(accumulate-n op init (map cdr seqs))))) ; proceed with cadrs in next recursion
; Folding to left and right are standard operations
(define (fold-left op initial sequence)
(define (iter result rest)
(if (null? rest)
result
(iter (op result (car rest))
(cdr rest))))
(iter initial sequence))
; folding right is the standard accumulation
(define fold-right accumulate)
(define (reverse sequence)
(fold-right (lambda (x y) (append y (list x))) nil sequence))