1801834720b932921fb65c809cb9107530efc420
[mes.git] / module / mes / scm.mes
1 ;;; -*-scheme-*-
2
3 ;;; Mes --- Maxwell Equations of Software
4 ;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;;
6 ;;; This file is part of Mes.
7 ;;;
8 ;;; Mes is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; Mes is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with Mes.  If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;;; scm.mes is loaded after base, quasiquote and let.  It provides
24 ;;; basic Scheme functions bringing Mes close to basic RRS Scheme (no
25 ;;; labels, processes, fluids or throw/catch).
26
27 ;;; Code:
28
29 (mes-use-module (mes let))
30
31 (define (cadddr x) (car (cdddr x)))
32
33 (define (list . rest) rest)
34
35 (define (list-head x n)
36   (if (= 0 n) '()
37       (cons (car x) (list-head (cdr x) (- n 1)))))
38
39 (define (list-tail x n)
40   (if (= 0 n) x
41       (list-tail (cdr x) (- n 1))))
42
43 (define (string-prefix? prefix string)
44   (and
45    (>= (string-length string) (string-length prefix))
46    (equal? (substring string 0 (string-length prefix)) prefix)))
47
48 (define (symbol-prefix? prefix symbol)
49   (string-prefix? (symbol->string prefix) (symbol->string symbol)))
50
51 (define (symbol-append . rest)
52   (string->symbol (apply string-append (map symbol->string rest))))
53
54 (define-macro (case val . args)
55   (if (null? args) #f
56       (let ((clause (car args)))
57         (let ((pred (car clause)))
58           (let ((body (cdr clause)))
59            (if (pair? pred) `(if ,(if (null? (cdr pred))
60                                       `(eq? ,val ',(car pred))
61                                       `(member ,val ',pred))
62                                  (begin ,@body)
63                                  (case ,val ,@(cdr args)))
64                `(begin ,@body)))))))
65
66 (define-macro (when expr . body)
67   `(if ,expr
68        ((lambda () ,@body))))
69
70 (define-macro (unless expr . body)
71   `(if (not ,expr)
72        ((lambda () ,@body))))
73
74 (define-macro (do init test . body)
75   `(let loop ((,(caar init) ,(cadar init)))
76      (when (not ,@test)
77        ,@body
78        (loop ,@(cddar init)))))
79
80 (define integer? number?)
81
82 (define (make-list n . x)
83   (let ((fill (if (pair? x) (car x) *unspecified*)))
84     (let loop ((n n))
85       (if (= 0 n) '()
86           (cons fill (loop (- n 1)))))))
87
88 (define (string->list s)
89   (let ((n (string-length s)))
90     (let loop ((i 0))
91       (if (= i n) '()
92           (cons (string-ref s i) (loop (+ i 1)))))))
93
94 (define (string->number s . radix)
95   (if (and (pair? radix) (not (= (car radix) 10))) '*STRING->NUMBER:RADIX-NOT-SUPPORTED
96       (let* ((lst (string->list s))
97              (sign (if (char=? (car lst) #\-) -1 1))
98              (lst (if (= sign -1) (cdr lst) lst)))
99         (let loop ((lst lst) (n 0))
100           (if (null? lst) (* sign n)
101               (loop (cdr lst) (+ (* n 10) (- (char->integer (car lst)) (char->integer #\0)))))))))
102
103 (define (char<? a b) (< (char->integer a) (char->integer b)))
104 (define (char>? a b) (> (char->integer a) (char->integer b)))
105 (define (char<=? a b) (<= (char->integer a) (char->integer b)))
106 (define (char>=? a b) (>= (char->integer a) (char->integer b)))
107
108 (define (vector . rest) (list->vector rest))
109 (define c:make-vector make-vector)
110 (define (make-vector n . x)
111   (if (null? x) (c:make-vector n)
112       (list->vector (apply make-list (cons n x)))))
113
114 (define (acons key value alist)
115   (cons (cons key value) alist))
116
117 (define (assq-set! alist key val)
118   (let ((entry (assq key alist)))
119     (cond (entry (set-cdr! entry val)
120                  alist)
121           (#t (cons (cons key val) alist)))))
122
123 (define (assq-ref alist key)
124   (let ((entry (assq key alist)))
125     (if entry (cdr entry)
126         #f)))
127
128 (define assv assq)
129 (define assv-ref assq-ref)
130
131 (define (assoc key alist)
132   (if (null? alist) #f ;; IF
133       (if (equal? key (caar alist)) (car alist)
134           (assoc key (cdr alist)))))
135
136 (define (assoc-ref alist key)
137   (let ((entry (assoc key alist)))
138     (if entry (cdr entry)
139         #f)))
140
141 (define (memq x lst)
142   (if (null? lst) #f ;; IF
143       (if (eq? x (car lst)) lst
144           (memq x (cdr lst)))))
145 (define memv memq)
146
147 (define (member x lst)
148   (if (null? lst) #f ;; IF
149       (if (equal? x (car lst)) lst
150           (member x (cdr lst)))))
151
152 (define (for-each f l . r)
153   (if (pair? l) (if (null? r) (begin (f (car l)) (for-each f (cdr l)))
154                     (if (null? (cdr r)) (begin (f (car l) (caar r)) (for-each f (cdr l) (cdar r)))))))
155
156 (define (<= . rest)
157   (or (apply < rest)
158       (apply = rest)))
159
160 (define (>= . rest)
161   (or (apply > rest)
162       (apply = rest)))
163
164 ;; (define (>= . rest)
165 ;;   (if (apply > rest) #t
166 ;;       (if (apply = rest) #t
167 ;;           #f)))
168
169 (define (remainder x y)
170   (- x (* (quotient x y) y)))
171
172 (define (even? x)
173   (= 0 (remainder x 2)))
174
175 (define (odd? x)
176   (= 1 (remainder x 2)))
177
178 (define (negative? x)
179   (< x 0))
180
181 (define (positive? x)
182   (> x 0))
183
184 (define (zero? x)
185   (= x 0))
186
187 (define (1+ x)
188   (+ x 1))
189
190 (define (1- x)
191   (- x 1))
192
193 (define (abs x)
194   (if (>= x 0) x (- x)))
195
196 (define (expt x y)
197   (let loop ((s 1) (count y))
198     (if (= 0 count) s
199         (loop (* s x) (- count 1)))))
200
201 (define (max x . rest)
202   (if (null? rest) x
203       (let ((y (car rest)))
204         (let ((z (if (> x y) x y)))
205           (apply max (cons z (cdr rest)))))))
206
207 (define (min x . rest)
208   (if (null? rest) x
209       (let ((y (car rest)))
210         (let ((z (if (< x y) x y)))
211           (apply min (cons z (cdr rest)))))))
212
213 (define gensym
214   (let ((counter 0))
215     (lambda (. rest)
216       (let ((value (number->string counter)))
217         (set! counter (+ counter 1))
218         (string->symbol (string-append "g" value))))))
219
220 (define else #t)
221
222 (define (error who . rest)
223   (display "error:" (current-error-port))
224   (display who (current-error-port))
225   (display ":" (current-error-port))
226   (display rest (current-error-port))
227   (newline (current-error-port))
228   (display "exiting...\n" (current-error-port))
229   (exit 1))
230
231 (define (syntax-error message . rest)
232   (display "syntax-error:" (current-error-port))
233   (display message (current-error-port))
234   (display ":" (current-error-port))
235   (display rest (current-error-port))
236   (newline (current-error-port)))
237
238 (define (list-ref lst k)
239   (let loop ((lst lst) (k k))
240     (if (= 0 k) (car lst)
241         (loop (cdr lst) (- k 1)))))
242
243 (define (iota n)
244   (if (<= n 0) '()
245       (append2 (iota (- n 1)) (list (- n 1)))))
246
247 ;; srfi-1
248 (define (last-pair lst)
249   (let loop ((lst lst))
250     (if (or (null? lst) (null? (cdr lst))) lst
251         (loop (cdr lst)))))
252
253 (define (reverse lst)
254   (if (null? lst) '()
255       (append (reverse (cdr lst)) (cons (car lst) '()))))
256
257 (define (filter pred lst)
258   (let loop ((lst lst))
259     (if (null? lst) '()
260         (if (pred (car lst))
261             (cons (car lst) (loop (cdr lst)))
262             (loop (cdr lst))))))
263
264 (define (delete x lst)
265   (filter (lambda (e) (not (equal? e x))) lst))
266
267 (define (delq x lst)
268   (filter (lambda (e) (not (eq? e x))) lst))
269
270 (define (vector-copy x)
271   (list->vector (vector->list x)))
272
273 (define (eof-object? x)
274   (or (and (number? x) (= x -1))
275       (and (char? x) (eof-object? (char->integer x)))))
276
277 (define (char=? x y)
278   (and (char? x) (char? y)
279        (eq? x y)))
280
281 (define (char-alphabetic? x)
282   (and (char? x)
283        (let ((i (char->integer x)))
284         (or (and (>= i (char->integer #\A)) (<= i (char->integer #\Z)))
285             (and (>= i (char->integer #\a)) (<= i (char->integer #\z)))))))
286
287 (define (char-numeric? x)
288   (and (char? x)
289        (let ((i (char->integer x)))
290          (and (>= i (char->integer #\0)) (<= i (char->integer #\9))))))