-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.scm
139 lines (106 loc) · 2.35 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
; lib.scm
;
; Builtin scheme librairie implemented in scheme.
(define (map f l)
(if (pair? l)
(cons (f (car l)) (map f (cdr l)))
(list)))
(define (append l1 l2)
(if (null? l1)
l2
(cons
(car l1)
(append (cdr l1) l2))))
(define (_length l n)
(if (pair? l)
(_length (cdr l) (+ n 1))
n))
(define (length l)
(_length l 0))
(define (reverse l)
(if (null? l)
l
(append
(reverse (cdr l))
(cons (car l) (list)))))
(define (char-whitespace? c)
(or (eqv? c #\newline)
(eqv? c #\space)
(eqv? c #\tab)))
(define (list . x)
x)
(define (char-numeric? c)
(let ((code (char->integer c)))
(and (>= code 48) (<= code 57))))
(define (_string->list str n i)
(if (= n i)
(list)
(cons
(string-ref str i)
(_string->list str n (+ i 1)))))
(define (string->list s)
(_string->list s (string-length s) 0))
(define (_string->number strl n)
(if (null? strl)
n
(let ((c (car strl)))
(if (char-numeric? c)
(_string->number
(cdr strl)
(+ (* n 10) (- (char->integer c) 48)))
n))))
(define (string->number s)
(_string->number (string->list s) 0))
(define (string) "")
(define (string-append . x)
(_string-append x))
(define (_string-append x)
(if (and (pair? x) (pair? (cdr x)))
(string-append-2 (car x) (_string-append (cdr x)))
(if (and (pair? x) (null? (cdr x)))
(car x)
(string))))
(define (caar x)
(car (car x)))
(define (caaaar x)
(car (car (car (car x)))))
(define (cddr x)
(cdr (cdr x)))
(define (cdddr x)
(cdr (cddr x)))
(define (cddddr x)
(cdr (cdddr x)))
(define (cadr x)
(car (cdr x)))
(define (caadr x)
(caar (cdr x)))
(define (caddr x)
(car (cddr x)))
(define (cdadr x)
(cdr (car (cdr x))))
(define (cadddr x)
(car (cdddr x)))
(define (cadar x)
(car (cdr (car x))))
(define (_table-find t n)
(if (pair? t)
(if (eqv? (caar t) n)
(car t)
(_table-find (cdr t) n))
#f))
(define (table-find t n)
(_table-find (cdr t) n))
(define (make-table)
(list (cons 'head 0)))
(define (table-set! t key x)
(let ((cel (table-find t key)))
(if cel
(set-cdr! cel x)
(set-cdr! t (cons (cons key x) (cdr t))))))
(define (table-ref t key b)
(let ((cel (table-find t key)))
(if cel
(cdr cel)
#f)))
(define (table-length t)
(length (cdr t)))