-
Notifications
You must be signed in to change notification settings - Fork 1
/
stdlib.scm
62 lines (51 loc) · 1.36 KB
/
stdlib.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
;;
;; Here we define procedures that eliminate the need for providing
;; certain primitives.
;;
(define (map f lst)
(if (null? lst) '()
(cons (f (car lst)) (map f (cdr lst)))))
(define (assoc symbol alist)
(cond ((null? alist) false)
((equal? (car (car alist)) symbol) (cdr (car alist)))
(else (assoc symbol (cdr alist)))))
(define (fold f z lst)
(if (null? lst) z
(fold f (f (car lst) z) (cdr lst))))
(define (foldr f z lst)
(if (null? lst) z
(f (car lst) (foldr f z (cdr lst)))))
(define (length lst)
(if (not (list? lst))
(error "Argument to length is not a list:" lst)
(fold (lambda (x accum) (+ 1 accum)) 0 lst)))
(define (list . a)
(foldr cons '() a))
(define (list? a)
(cond ((null? a) true)
((not (pair? a)) false)
(else (list? (cdr a)))))
(define (append a b)
(foldr cons b a))
(define (null? x)
(eq? x '()))
(define (>= a b . c)
(define (helper x rest)
(if (null? rest)
true
(let ((y (car rest)))
(and (or (> x y)
(= x y))
(helper y (cdr rest))))))
(and (or (> a b)
(= a b))
(helper b c)))
(define (< a b . c)
(define (helper x rest)
(if (null? rest)
true
(let ((y (car rest)))
(and (not (>= x y))
(helper y (cdr rest))))))
(and (not (>= a b))
(helper b c)))