|
1 | 1 | (define eq? =)
|
2 | 2 |
|
3 |
| -(define (abs x) |
4 |
| - (if (< x 0) (- 0 x) x)) |
| 3 | +(define (abs x) (if (< x 0) (- 0 x) x)) |
5 | 4 |
|
6 | 5 | (define (foldl proc init list)
|
7 |
| - (if list |
8 |
| - (foldl proc (proc init (car list)) (cdr list)) |
| 6 | + (if list |
| 7 | + (foldl proc |
| 8 | + (proc init (car list)) |
| 9 | + (cdr list)) |
9 | 10 | init))
|
10 | 11 |
|
11 | 12 | (define (foldr proc init list)
|
12 |
| - (if list |
13 |
| - (proc (car list) (foldr proc init (cdr list))) |
| 13 | + (if list |
| 14 | + (proc (car list) |
| 15 | + (foldr proc init (cdr list))) |
14 | 16 | init))
|
15 | 17 |
|
16 |
| -(define (list . items) (foldr cons nil items)) |
| 18 | +(define (list . items) |
| 19 | + (foldr cons nil items)) |
17 | 20 |
|
18 | 21 | (define (reverse list)
|
19 |
| - (foldl |
20 |
| - (lambda (a x) (cons x a)) |
21 |
| - nil |
22 |
| - list)) |
| 22 | + (foldl (lambda (a x) (cons x a )) nil list)) |
23 | 23 |
|
24 | 24 | (define (unary-map proc list)
|
25 |
| - (foldr |
26 |
| - (lambda (x rest) (cons (proc x) rest)) |
27 |
| - nil |
28 |
| - list)) |
| 25 | + (foldr (lambda (x rest) (cons (proc x) rest)) |
| 26 | + nil |
| 27 | + list)) |
29 | 28 |
|
30 | 29 | (define (map proc . arg-lists)
|
31 |
| - (if (car arg-lists) |
32 |
| - (cons |
33 |
| - (apply proc (unary-map car arg-lists)) |
34 |
| - (apply (quote map) (cons proc (unary-map cdr arg-lists)))) |
| 30 | + (if (car arg-lists) |
| 31 | + (cons (apply proc (unary-map car arg-lists)) |
| 32 | + (apply 'map (cons proc |
| 33 | + (unary-map cdr arg-lists)))) |
35 | 34 | nil))
|
36 | 35 |
|
37 | 36 | (define (append a b) (foldr cons b a))
|
|
41 | 40 | (define (cadr x) (car (cdr x)))
|
42 | 41 |
|
43 | 42 | (defmacro (quasiquote x)
|
44 |
| - (if (pair? x) |
45 |
| - (if (eq? (car x) (quote unquote)) |
46 |
| - (cadr x) |
47 |
| - (if (eq? (caar x) (quote unquote-splicing)) |
48 |
| - (list |
49 |
| - (quote append) |
50 |
| - (cadr (car x)) |
51 |
| - (list (quote quasiquote) (cdr x))) |
52 |
| - (list |
53 |
| - (quote cons) |
54 |
| - (list (quote quasiquote) (car x)) |
55 |
| - (list (quote quasiquote) (cdr x))))) |
56 |
| - (list (quote quote) x))) |
| 43 | + (if (pair? x) |
| 44 | + (if (eq? (car x) 'unquote) |
| 45 | + (cadr x) |
| 46 | + (if (eq? (caar x) 'unquote-splicing) |
| 47 | + (list 'append |
| 48 | + (cadr (car x)) |
| 49 | + (list 'quasiquote (cdr x))) |
| 50 | + (list 'cons |
| 51 | + (list 'quasiquote (car x)) |
| 52 | + (list 'quasiquote (cdr x))))) |
| 53 | + (list 'quote x))) |
57 | 54 |
|
58 | 55 | (defmacro (let defs . body)
|
59 |
| - (quasiquote |
60 |
| - ((lambda (unquote (map car defs)) (unquote-splicing body)) |
61 |
| - (unquote-splicing (map cadr defs))))) |
| 56 | + `((lambda ,(map car defs) ,@body) |
| 57 | + ,@(map cadr defs))) |
62 | 58 |
|
63 | 59 | (define +
|
64 |
| - (let |
65 |
| - ((old+ +)) |
66 |
| - (lambda xs (foldl old+ 0 xs)))) |
| 60 | + (let ((old+ +)) |
| 61 | + (lambda xs (foldl old+ 0 xs)))) |
67 | 62 |
|
68 | 63 | (define -
|
69 |
| - (let |
70 |
| - ((old- -)) |
71 |
| - (lambda xs (foldl old- (car xs) (cdr xs))))) |
| 64 | + (let ((old- -)) |
| 65 | + (lambda xs (foldl old- (car xs) (cdr xs))))) |
72 | 66 |
|
73 | 67 | (define *
|
74 |
| - (let |
75 |
| - ((old* *)) |
76 |
| - (lambda xs (foldl old* (car xs) (cdr xs))))) |
| 68 | + (let ((old* *)) |
| 69 | + (lambda xs (foldl old* (car xs) (cdr xs))))) |
77 | 70 |
|
78 | 71 | (define /
|
79 |
| - (let |
80 |
| - ((old/ /)) |
81 |
| - (lambda xs (foldl old/ (car xs) (cdr xs))))) |
| 72 | + (let ((old/ /)) |
| 73 | + (lambda xs (foldl old/ (car xs) (cdr xs))))) |
| 74 | + |
82 | 75 |
|
83 | 76 | (define (last x)
|
84 |
| - (if (pair? x) |
| 77 | + (if (pair? x) |
85 | 78 | (if (pair? (cdr x))
|
86 |
| - (last (cdr x)) |
87 |
| - (if (= nil (cdr x)) |
88 |
| - (car x) |
89 |
| - (cdr x))) |
| 79 | + (last (cdr x)) |
| 80 | + (if (= nil (cdr x)) |
| 81 | + (car x) |
| 82 | + (cdr x))) |
90 | 83 | (x)))
|
91 | 84 |
|
92 | 85 | (define (list-length x)
|
93 |
| - (if (pair? x) |
| 86 | + (if (pair? x) |
94 | 87 | (+ 1 (list-length (cdr x)))
|
95 |
| - (if (= nil x) 0 1))) |
| 88 | + (if (= nil x) |
| 89 | + 0 |
| 90 | + 1))) |
96 | 91 |
|
97 | 92 | (define (length x)
|
98 |
| - (if (pair? x) |
| 93 | + (if (pair? x) |
99 | 94 | (list-length x)
|
100 |
| - (if (string? x) (string-length x) nil))) |
101 |
| - |
| 95 | + (if (string? x) |
| 96 | + (string-length x) |
| 97 | + nil))) |
0 commit comments