-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.scm
48 lines (37 loc) · 1.06 KB
/
lib.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
(define else #t)
(define number? integer?)
(define (> x y) (< y x))
(define (<= x y) (not (< y x)))
(define (>= x y) (not (< x y)))
(define (abs x) (if (< x 0) (- 0 x) x))
(define (not obj) (if obj #f #t))
(define (boolean? obj) (if obj (eq? obj #t) #t))
(define (null? obj) (eq? obj '()))
(define (length lst)
(define (loop lst n)
(if (null? lst) n (loop (cdr lst) (+ n 1))))
(loop lst 0))
(define (reverse lst)
(define (loop lst acc)
(if (null? lst)
acc
(loop (cdr lst) (cons (car lst) acc))))
(loop lst '()))
(define (memq obj lst)
(cond ((null? lst) #f)
((eq? (car lst) obj) lst)
(else (memq obj (cdr lst)))))
(define (assq obj lst)
(cond ((null? lst) #f)
((eq? (car (car lst)) obj) (car lst))
(else (assq obj (cdr lst)))))
(define (map proc lst)
(define (loop lst acc)
(if (null? lst)
(reverse acc)
(loop (cdr lst) (cons (proc (car lst)) acc))))
(loop lst '()))
(define (for-each proc lst)
(cond ((not (null? lst))
(proc (car lst))
(for-each proc (cdr lst)))))