-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprelude.scm
50 lines (41 loc) · 920 Bytes
/
prelude.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
(define (abs n)
((if (> n 0) + -) n))
(define (inc n) (+ n 1))
(define (dec n) (- n 1))
(define (empty? lst)
(= lst None))
(define (map f l)
(if (empty? l)
None
(cons (f (car l))
(map f (cdr l)))))
(define (filter f lst)
(if (empty? lst)
None
(if (f (car lst))
(cons (car lst)
(filter f (cdr lst)))
(filter f (cdr lst)))))
(define (length lst)
(if (empty? lst)
0
(+ 1
(length (cdr lst)))))
(define (reverse lst)
(if (= 1 (length lst))
(list (car lst))
(append (reverse (cdr lst))
(list (car lst)))))
; reduce in python style with init (leftmost) value
(define (reduce f init lst)
(if (empty? lst)
init
(reduce f
(f init (car lst))
(cdr lst))))
; (range a b) -> list from a to b inclusively
(define (range a b)
(if (= a b)
(list a)
(cons a
(range (inc a) b))))