-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.scm
42 lines (35 loc) · 1.01 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
(define (prime? n)
(if (= (remainder n 2) 0) #f #t))
(define (map proc items)
(if (null? items)
'()
(cons (proc (car items))
(map proc (cdr items)))))
(define (filter predicate seq)
(cond ((null? seq) '())
((predicate (car seq))
(cons (car seq)
(filter predicate (cdr seq))))
(else (filter predicate (cdr seq)))))
(define (accumulate op initial seq)
(if (null? seq) initial
(op (car seq)
(accumulate op initial (cdr seq)))))
(define (accumulate-n op init seqs)
(if (null? (car seqs))
'()
(cons (accumulate op init (map car seqs))
(accumulate-n op init (map cdr seqs)))))
(define (fold-left op initial sequence)
(define (iter result rest)
(if (null? rest)
result
(iter (op result (car rest))
(cdr rest))))
(iter initial sequence))
(define (enumerate-interval i j)
(if (> i j)
'()
(cons i (enumerate-interval (+ 1 i) j))))
(define (flatmap proc seq)
(accumulate append '() (map proc seq)))