-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdlib.scm
More file actions
168 lines (148 loc) · 3.35 KB
/
Copy pathstdlib.scm
File metadata and controls
168 lines (148 loc) · 3.35 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
;-----------------------------------------------
; Skeme standard library functions
;
; Copyright (C) 2026 by Mark Seminatore
;-----------------------------------------------
(load "term_colors.scm")
;----------------------
; define assertion test
;----------------------
(define (assert condition message)
(if (not condition)
(error "Assertion failed:" message)
condition)
)
;----------------------
; define cadr and cddr
;----------------------
(define (cadr x) (car (cdr x)))
(define (cddr x) (cdr (cdr x)))
;----------------------
; define apply
;----------------------
(define (apply fn lst)
(cond
((null? lst) '())
(else (cons (fn (car lst)) (apply fn (cdr lst))))
)
)
;----------------------
; define filter
;----------------------
(define (filter fn lst)
(cond
((null? lst) '())
((fn (car lst))
(cons (car lst) (filter fn (cdr lst))))
(else (filter fn (cdr lst)))
)
)
;----------------------
; define even?
;----------------------
(define (even? n)
(= (modulo n 2) 0)
)
;----------------------
; define odd?
;----------------------
(define (odd? n)
(not (even? n))
)
;----------------------
; define min/max
;----------------------
(define (min a b)
(if (< a b) a b)
)
(define (max a b)
(if (> a b) a b)
)
;----------------------
; define append
;----------------------
(define (append lst1 lst2)
(if (null? lst1)
lst2
(cons (car lst1) (append (cdr lst1) lst2))
)
)
;----------------------
; define map
; map function over a list
;----------------------
;(define (map fn lst)
; (if (null? lst)
; '()
; (cons (fn (car lst)) (map fn (cdr lst)))
; )
;)
;----------------------
; list-ref: return nth element of a list
;----------------------
(define (list-ref lst n)
(if (= n 0)
(car lst)
(list-ref (cdr lst) (- n 1))))
;----------------------
; list-tail: return the sublist after dropping n elements
;----------------------
(define (list-tail lst n)
(if (= n 0)
lst
(list-tail (cdr lst) (- n 1))))
;----------------------
; member: find first element equal? to x
;----------------------
(define (member x lst)
(cond
((null? lst) #f)
((equal? x (car lst)) lst)
(else (member x (cdr lst)))))
;----------------------
; memq: find first element eq? to x
;----------------------
(define (memq x lst)
(cond
((null? lst) #f)
((eq? x (car lst)) lst)
(else (memq x (cdr lst)))))
;----------------------
; assoc: find pair by key using equal?
;----------------------
(define (assoc key alist)
(cond
((null? alist) #f)
((equal? key (car (car alist))) (car alist))
(else (assoc key (cdr alist)))))
;----------------------
; assq: find pair by key using eq?
;----------------------
(define (assq key alist)
(cond
((null? alist) #f)
((eq? key (car (car alist))) (car alist))
(else (assq key (cdr alist)))))
;----------------------
; for-each: apply fn to each element for side effects
;----------------------
(define (for-each fn lst)
(if (null? lst)
'()
(begin
(fn (car lst))
(for-each fn (cdr lst)))))
;----------------------
; gcd: greatest common divisor
;----------------------
(define (gcd a b)
(if (= b 0)
(abs a)
(gcd b (remainder a b))))
;----------------------
; lcm: least common multiple
;----------------------
(define (lcm a b)
(if (= a 0)
0
(abs (quotient (* a b) (gcd a b)))))