Added patch by Siddharth Heroor
[Klink.git] / init.scm
blob3cdf9cafa04b3502140a7ea3b84fe27989d0d0f6
1 ;    Initialization file for TinySCHEME 1.39\r
2 \r
3 ; Per R5RS, up to four deep compositions should be defined\r
4 (define (caar x) (car (car x)))\r
5 (define (cadr x) (car (cdr x)))\r
6 (define (cdar x) (cdr (car x)))\r
7 (define (cddr x) (cdr (cdr x)))\r
8 (define (caaar x) (car (car (car x))))\r
9 (define (caadr x) (car (car (cdr x))))\r
10 (define (cadar x) (car (cdr (car x))))\r
11 (define (caddr x) (car (cdr (cdr x))))\r
12 (define (cdaar x) (cdr (car (car x))))\r
13 (define (cdadr x) (cdr (car (cdr x))))\r
14 (define (cddar x) (cdr (cdr (car x))))\r
15 (define (cdddr x) (cdr (cdr (cdr x))))\r
16 (define (caaaar x) (car (car (car (car x)))))\r
17 (define (caaadr x) (car (car (car (cdr x)))))\r
18 (define (caadar x) (car (car (cdr (car x)))))\r
19 (define (caaddr x) (car (car (cdr (cdr x)))))\r
20 (define (cadaar x) (car (cdr (car (car x)))))\r
21 (define (cadadr x) (car (cdr (car (cdr x)))))\r
22 (define (caddar x) (car (cdr (cdr (car x)))))\r
23 (define (cadddr x) (car (cdr (cdr (cdr x)))))\r
24 (define (cdaaar x) (cdr (car (car (car x)))))\r
25 (define (cdaadr x) (cdr (car (car (cdr x)))))\r
26 (define (cdadar x) (cdr (car (cdr (car x)))))\r
27 (define (cdaddr x) (cdr (car (cdr (cdr x)))))\r
28 (define (cddaar x) (cdr (cdr (car (car x)))))\r
29 (define (cddadr x) (cdr (cdr (car (cdr x)))))\r
30 (define (cdddar x) (cdr (cdr (cdr (car x)))))\r
31 (define (cddddr x) (cdr (cdr (cdr (cdr x)))))\r
33 ;;;; Utility to ease macro creation\r
34 (define (macro-expand form)\r
35      ((eval (get-closure-code (eval (car form)))) form))\r
37 (define (macro-expand-all form)\r
38    (if (macro? form)\r
39       (macro-expand-all (macro-expand form))\r
40       form))\r
42 (define *compile-hook* macro-expand-all)\r
45 (macro (unless form)\r
46      `(if (not ,(cadr form)) (begin ,@(cddr form))))\r
48 (macro (when form)\r
49      `(if ,(cadr form) (begin ,@(cddr form))))\r
51 ; DEFINE-MACRO Contributed by Andy Gaynor\r
52 (macro (define-macro dform)\r
53   (if (symbol? (cadr dform))\r
54     `(macro ,@(cdr dform))\r
55     (let ((form (gensym)))\r
56       `(macro (,(caadr dform) ,form)\r
57          (apply (lambda ,(cdadr dform) ,@(cddr dform)) (cdr ,form))))))\r
59 ; Utilities for math. Notice that inexact->exact is primitive,\r
60 ; but exact->inexact is not.\r
61 (define exact? integer?)\r
62 (define (inexact? x) (and (real? x) (not (integer? x))))\r
63 (define (even? n) (= (remainder n 2) 0))\r
64 (define (odd? n) (not (= (remainder n 2) 0)))\r
65 (define (zero? n) (= n 0))\r
66 (define (positive? n) (> n 0))\r
67 (define (negative? n) (< n 0))\r
68 (define complex? number?)\r
69 (define rational? real?)\r
70 (define (abs n) (if (>= n 0) n (- n)))\r
71 (define (exact->inexact n) (* n 1.0))\r
72 (define (<> n1 n2) (not (= n1 n2)))\r
73 (define (max . lst)\r
74      (foldr (lambda (a b) (if (> a b) a b)) (car lst) (cdr lst)))\r
75 (define (min . lst)\r
76      (foldr (lambda (a b) (if (< a b) a b)) (car lst) (cdr lst)))\r
77 (define (succ x) (+ x 1))\r
78 (define (pred x) (- x 1))\r
79 (define gcd\r
80   (lambda a\r
81     (if (null? a)\r
82       0\r
83       (let ((aa (abs (car a)))\r
84             (bb (abs (cadr a))))\r
85          (if (= bb 0)\r
86               aa\r
87               (gcd bb (remainder aa bb)))))))\r
88 (define lcm\r
89   (lambda a\r
90     (if (null? a)\r
91       1\r
92       (let ((aa (abs (car a)))\r
93             (bb (abs (cadr a))))\r
94          (if (or (= aa 0) (= bb 0))\r
95              0\r
96              (abs (* (quotient aa (gcd aa bb)) bb)))))))\r
99 (define (string . charlist)\r
100      (list->string charlist))\r
102 (define (list->string charlist)\r
103      (let* ((len (length charlist))\r
104             (newstr (make-string len))\r
105             (fill-string!\r
106                (lambda (str i len charlist)\r
107                     (if (= i len)\r
108                          str\r
109                          (begin (string-set! str i (car charlist))\r
110                          (fill-string! str (+ i 1) len (cdr charlist)))))))\r
111           (fill-string! newstr 0 len charlist)))\r
113 (define (string-fill! s e)\r
114      (let ((n (string-length s)))\r
115           (let loop ((i 0))\r
116                (if (= i n)\r
117                     s\r
118                     (begin (string-set! s i e) (loop (succ i)))))))\r
120 (define (string->list s)\r
121      (let loop ((n (pred (string-length s))) (l '()))\r
122           (if (= n -1)\r
123                l\r
124                (loop (pred n) (cons (string-ref s n) l)))))\r
126 (define (string-copy str)\r
127      (string-append str))\r
129 (define (string->anyatom str pred)\r
130      (let* ((a (string->atom str)))\r
131        (if (pred a) a\r
132      (error "string->xxx: not a xxx" a))))\r
134 (define (string->number str) (string->anyatom str number?))\r
136 (define (anyatom->string n pred)\r
137   (if (pred n)\r
138       (atom->string n)\r
139       (error "xxx->string: not a xxx" n)))\r
142 (define (number->string n) (anyatom->string n number?))\r
144 (define (char-cmp? cmp a b)\r
145      (cmp (char->integer a) (char->integer b)))\r
146 (define (char-ci-cmp? cmp a b)\r
147      (cmp (char->integer (char-downcase a)) (char->integer (char-downcase b))))\r
149 (define (char=? a b) (char-cmp? = a b))\r
150 (define (char<? a b) (char-cmp? < a b))\r
151 (define (char>? a b) (char-cmp? > a b))\r
152 (define (char<=? a b) (char-cmp? <= a b))\r
153 (define (char>=? a b) (char-cmp? >= a b))\r
155 (define (char-ci=? a b) (char-ci-cmp? = a b))\r
156 (define (char-ci<? a b) (char-ci-cmp? < a b))\r
157 (define (char-ci>? a b) (char-ci-cmp? > a b))\r
158 (define (char-ci<=? a b) (char-ci-cmp? <= a b))\r
159 (define (char-ci>=? a b) (char-ci-cmp? >= a b))\r
161 ; Note the trick of returning (cmp x y)\r
162 (define (string-cmp? chcmp cmp a b)\r
163      (let ((na (string-length a)) (nb (string-length b)))\r
164           (let loop ((i 0))\r
165                (cond\r
166                     ((= i na)\r
167                          (if (= i nb) (cmp 0 0) (cmp 0 1)))\r
168                     ((= i nb)\r
169                          (cmp 1 0))\r
170                     ((chcmp = (string-ref a i) (string-ref b i))\r
171                          (loop (succ i)))\r
172                     (else\r
173                          (chcmp cmp (string-ref a i) (string-ref b i)))))))\r
176 (define (string=? a b) (string-cmp? char-cmp? = a b))\r
177 (define (string<? a b) (string-cmp? char-cmp? < a b))\r
178 (define (string>? a b) (string-cmp? char-cmp? > a b))\r
179 (define (string<=? a b) (string-cmp? char-cmp? <= a b))\r
180 (define (string>=? a b) (string-cmp? char-cmp? >= a b))\r
182 (define (string-ci=? a b) (string-cmp? char-ci-cmp? = a b))\r
183 (define (string-ci<? a b) (string-cmp? char-ci-cmp? < a b))\r
184 (define (string-ci>? a b) (string-cmp? char-ci-cmp? > a b))\r
185 (define (string-ci<=? a b) (string-cmp? char-ci-cmp? <= a b))\r
186 (define (string-ci>=? a b) (string-cmp? char-ci-cmp? >= a b))\r
188 (define (list . x) x)\r
190 (define (foldr f x lst)\r
191      (if (null? lst)\r
192           x\r
193           (foldr f (f x (car lst)) (cdr lst))))\r
195 (define (unzip1-with-cdr . lists)\r
196   (unzip1-with-cdr-iterative lists '() '()))\r
198 (define (unzip1-with-cdr-iterative lists cars cdrs)\r
199   (if (null? lists)\r
200       (cons cars cdrs)\r
201       (let ((car1 (caar lists))\r
202       (cdr1 (cdar lists)))\r
203   (unzip1-with-cdr-iterative\r
204    (cdr lists)\r
205    (append cars (list car1))\r
206    (append cdrs (list cdr1))))))\r
208 (define (map proc . lists)\r
209   (if (null? lists)\r
210       (apply proc)\r
211       (if (null? (car lists))\r
212     '()\r
213     (let* ((unz (apply unzip1-with-cdr lists))\r
214      (cars (car unz))\r
215      (cdrs (cdr unz)))\r
216       (cons (apply proc cars) (apply map (cons proc cdrs)))))))\r
218 (define (for-each proc . lists)\r
219   (if (null? lists)\r
220       (apply proc)\r
221       (if (null? (car lists))\r
222     #t\r
223     (let* ((unz (apply unzip1-with-cdr lists))\r
224      (cars (car unz))\r
225      (cdrs (cdr unz)))\r
226       (apply proc cars) (apply map (cons proc cdrs))))))\r
228 (define (list-tail x k)\r
229     (if (zero? k)\r
230         x\r
231         (list-tail (cdr x) (- k 1))))\r
233 (define (list-ref x k)\r
234     (car (list-tail x k)))\r
236 (define (last-pair x)\r
237     (if (pair? (cdr x))\r
238         (last-pair (cdr x))\r
239         x))\r
241 (define (head stream) (car stream))\r
243 (define (tail stream) (force (cdr stream)))\r
245 (define (vector-equal? x y)\r
246      (and (vector? x) (vector? y) (= (vector-length x) (vector-length y))\r
247           (let ((n (vector-length x)))\r
248                (let loop ((i 0))\r
249                     (if (= i n)\r
250                          #t\r
251                          (and (equal? (vector-ref x i) (vector-ref y i))\r
252                               (loop (succ i))))))))\r
254 (define (list->vector x)\r
255      (apply vector x))\r
257 (define (vector-fill! v e)\r
258      (let ((n (vector-length v)))\r
259           (let loop ((i 0))\r
260                (if (= i n)\r
261                     v\r
262                     (begin (vector-set! v i e) (loop (succ i)))))))\r
264 (define (vector->list v)\r
265      (let loop ((n (pred (vector-length v))) (l '()))\r
266           (if (= n -1)\r
267                l\r
268                (loop (pred n) (cons (vector-ref v n) l)))))\r
270 ;; The following quasiquote macro is due to Eric S. Tiedemann.\r
271 ;;   Copyright 1988 by Eric S. Tiedemann; all rights reserved.\r
272 ;;\r
273 ;; Subsequently modified to handle vectors: D. Souflis\r
275 (macro\r
276  quasiquote\r
277  (lambda (l)\r
278    (define (mcons f l r)\r
279      (if (and (pair? r)\r
280               (eq? (car r) 'quote)\r
281               (eq? (car (cdr r)) (cdr f))\r
282               (pair? l)\r
283               (eq? (car l) 'quote)\r
284               (eq? (car (cdr l)) (car f)))\r
285          (if (or (procedure? f) (number? f) (string? f))\r
286                f\r
287                (list 'quote f))\r
288          (if (eqv? l vector)\r
289                (apply l (eval r))\r
290                (list 'cons l r)\r
291                )))\r
292    (define (mappend f l r)\r
293      (if (or (null? (cdr f))\r
294              (and (pair? r)\r
295                   (eq? (car r) 'quote)\r
296                   (eq? (car (cdr r)) '())))\r
297          l\r
298          (list 'append l r)))\r
299    (define (foo level form)\r
300      (cond ((not (pair? form))\r
301                (if (or (procedure? form) (number? form) (string? form))\r
302                     form\r
303                     (list 'quote form))\r
304                )\r
305            ((eq? 'quasiquote (car form))\r
306             (mcons form ''quasiquote (foo (+ level 1) (cdr form))))\r
307            (#t (if (zero? level)\r
308                    (cond ((eq? (car form) 'unquote) (car (cdr form)))\r
309                          ((eq? (car form) 'unquote-splicing)\r
310                           (error "Unquote-splicing wasn't in a list:"\r
311                                  form))\r
312                          ((and (pair? (car form))\r
313                                (eq? (car (car form)) 'unquote-splicing))\r
314                           (mappend form (car (cdr (car form)))\r
315                                    (foo level (cdr form))))\r
316                          (#t (mcons form (foo level (car form))\r
317                                          (foo level (cdr form)))))\r
318                    (cond ((eq? (car form) 'unquote)\r
319                           (mcons form ''unquote (foo (- level 1)\r
320                                                      (cdr form))))\r
321                          ((eq? (car form) 'unquote-splicing)\r
322                           (mcons form ''unquote-splicing\r
323                                       (foo (- level 1) (cdr form))))\r
324                          (#t (mcons form (foo level (car form))\r
325                                          (foo level (cdr form)))))))))\r
326    (foo 0 (car (cdr l)))))\r
328 ;;;;;Helper for the dynamic-wind definition.  By Tom Breton (Tehom)\r
329 (define (shared-tail x y)\r
330    (let (  (len-x (length x)) \r
331            (len-y (length y)))\r
332       (define (shared-tail-helper x y)\r
333          (if\r
334             (eq? x y) \r
335             x\r
336             (shared-tail-helper (cdr x) (cdr y))))\r
337       (cond\r
338          ((> len-x len-y) \r
339             (shared-tail-helper\r
340                (list-tail x (- len-x len-y))\r
341                y))\r
342          ((< len-x len-y) \r
343             (shared-tail-helper\r
344                x\r
345                (list-tail y (- len-y len-x))))\r
346          (#t (shared-tail-helper x y)))))\r
348 ;;;;;Dynamic-wind by Tom Breton (Tehom)\r
350 ;;Guarded because we must only eval this once, because doing so\r
351 ;;redefines call/cc in terms of old call/cc\r
352 (unless (defined? 'dynamic-wind)\r
353    (let\r
354       ;;These functions are defined in the context of a private list of\r
355       ;;pairs of before/after procs.\r
356       (  (*active-windings* '())\r
357          ;;We'll define some functions into the larger environment, so\r
358          ;;we need to know it.\r
359          (outer-env (current-environment)))\r
361       ;;Poor-man's structure operations\r
362       (define before-func car)\r
363       (define after-func  cdr)\r
364       (define make-winding cons)\r
366       ;;Manage active windings\r
367       (define (activate-winding! new) \r
368          ((before-func new))\r
369          (set! *active-windings* (cons new *active-windings*)))\r
370       (define (deactivate-top-winding!)\r
371          (let ((old-top (car *active-windings*)))\r
372             ;;Remove it from the list first so it's not active during its\r
373             ;;own exit.\r
374             (set! *active-windings* (cdr *active-windings*))\r
375             ((after-func old-top))))\r
377       (define (set-active-windings! new-ws)\r
378          (unless (eq? new-ws *active-windings*)\r
379             (let ((shared (shared-tail new-ws *active-windings*)))\r
381                ;;Define the looping functions.\r
382                ;;Exit the old list.  Do deeper ones last.  Don't do\r
383                ;;any shared ones.\r
384                (define (pop-many)\r
385                   (unless (eq? *active-windings* shared)\r
386                      (deactivate-top-winding!)\r
387                      (pop-many)))\r
388                ;;Enter the new list.  Do deeper ones first so that the\r
389                ;;deeper windings will already be active.  Don't do any\r
390                ;;shared ones.\r
391                (define (push-many new-ws)\r
392                   (unless (eq? new-ws shared)\r
393                      (push-many (cdr new-ws))\r
394                      (activate-winding! (car new-ws))))\r
396                ;;Do it.\r
397                (pop-many)\r
398                (push-many new-ws))))\r
400       ;;The definitions themselves.\r
401       (eval\r
402          `(define call-with-current-continuation \r
403              ;;It internally uses the built-in call/cc, so capture it.\r
404              ,(let ((old-c/cc call-with-current-continuation))\r
405                  (lambda (func)\r
406                     ;;Use old call/cc to get the continuation.\r
407                     (old-c/cc \r
408                        (lambda (continuation)\r
409                           ;;Call func with not the continuation itself\r
410                           ;;but a procedure that adjusts the active\r
411                           ;;windings to what they were when we made\r
412                           ;;this, and only then calls the\r
413                           ;;continuation.\r
414                           (func \r
415                              (let ((current-ws *active-windings*))\r
416                                 (lambda (x)\r
417                                    (set-active-windings! current-ws)\r
418                                    (continuation x)))))))))\r
419          outer-env)\r
420       ;;We can't just say "define (dynamic-wind before thunk after)"\r
421       ;;because the lambda it's defined to lives in this environment,\r
422       ;;not in the global environment.\r
423       (eval \r
424          `(define dynamic-wind\r
425              ,(lambda (before thunk after)\r
426                  ;;Make a new winding\r
427                  (activate-winding! (make-winding before after))\r
428                  (let ((result (thunk)))\r
429                     ;;Get rid of the new winding.\r
430                     (deactivate-top-winding!)\r
431                     ;;The return value is that of thunk.\r
432                     result)))\r
433          outer-env)))\r
435 (define call/cc call-with-current-continuation)\r
438 ;;;;; atom? and equal? written by a.k\r
440 ;;;; atom?\r
441 (define (atom? x)\r
442   (not (pair? x)))\r
444 ;;;;    equal?\r
445 (define (equal? x y)\r
446      (cond\r
447           ((pair? x)\r
448                (and (pair? y)\r
449                     (equal? (car x) (car y))\r
450                     (equal? (cdr x) (cdr y))))\r
451           ((vector? x)\r
452                (and (vector? y) (vector-equal? x y)))\r
453           ((string? x)\r
454                (and (string? y) (string=? x y)))\r
455           (else (eqv? x y))))\r
457 ;;;; (do ((var init inc) ...) (endtest result ...) body ...)\r
458 ;;\r
459 (macro do\r
460   (lambda (do-macro)\r
461     (apply (lambda (do vars endtest . body)\r
462              (let ((do-loop (gensym)))\r
463                `(letrec ((,do-loop\r
464                            (lambda ,(map (lambda (x)\r
465                                            (if (pair? x) (car x) x))\r
466                                       `,vars)\r
467                              (if ,(car endtest)\r
468                                (begin ,@(cdr endtest))\r
469                                (begin\r
470                                  ,@body\r
471                                  (,do-loop\r
472                                    ,@(map (lambda (x)\r
473                                             (cond\r
474                                               ((not (pair? x)) x)\r
475                                               ((< (length x) 3) (car x))\r
476                                               (else (car (cdr (cdr x))))))\r
477                                        `,vars)))))))\r
478                   (,do-loop\r
479                     ,@(map (lambda (x)\r
480                              (if (and (pair? x) (cdr x))\r
481                                (car (cdr x))\r
482                                '()))\r
483                         `,vars)))))\r
484       do-macro)))\r
486 ;;;; generic-member\r
487 (define (generic-member cmp obj lst)\r
488   (cond\r
489     ((null? lst) #f)\r
490     ((cmp obj (car lst)) lst)\r
491     (else (generic-member cmp obj (cdr lst)))))\r
493 (define (memq obj lst)\r
494      (generic-member eq? obj lst))\r
495 (define (memv obj lst)\r
496      (generic-member eqv? obj lst))\r
497 (define (member obj lst)\r
498      (generic-member equal? obj lst))\r
500 ;;;; generic-assoc\r
501 (define (generic-assoc cmp obj alst)\r
502      (cond\r
503           ((null? alst) #f)\r
504           ((cmp obj (caar alst)) (car alst))\r
505           (else (generic-assoc cmp obj (cdr alst)))))\r
507 (define (assq obj alst)\r
508      (generic-assoc eq? obj alst))\r
509 (define (assv obj alst)\r
510      (generic-assoc eqv? obj alst))\r
511 (define (assoc obj alst)\r
512      (generic-assoc equal? obj alst))\r
514 (define (acons x y z) (cons (cons x y) z))\r
516 ;;;; Handy for imperative programs\r
517 ;;;; Used as: (define-with-return (foo x y) .... (return z) ...)\r
518 (macro (define-with-return form)\r
519      `(define ,(cadr form)\r
520           (call/cc (lambda (return) ,@(cddr form)))))\r
522 ;;;; Simple exception handling\r
524 ;    Exceptions are caught as follows:\r
526 ;         (catch (do-something to-recover and-return meaningful-value)\r
527 ;              (if-something goes-wrong)\r
528 ;              (with-these calls))\r
530 ;    "Catch" establishes a scope spanning multiple call-frames\r
531 ;    until another "catch" is encountered.\r
533 ;    Exceptions are thrown with:\r
535 ;         (throw "message")\r
537 ;    If used outside a (catch ...), reverts to (error "message)\r
539 (define *handlers* (list))\r
541 (define (push-handler proc)\r
542      (set! *handlers* (cons proc *handlers*)))\r
544 (define (pop-handler)\r
545      (let ((h (car *handlers*)))\r
546           (set! *handlers* (cdr *handlers*))\r
547           h))\r
549 (define (more-handlers?)\r
550      (pair? *handlers*))\r
552 (define (throw . x)\r
553      (if (more-handlers?)\r
554           (apply (pop-handler))\r
555           (apply error x)))\r
557 (macro (catch form)\r
558      (let ((label (gensym)))\r
559           `(call/cc (lambda (exit)\r
560                (push-handler (lambda () (exit ,(cadr form))))\r
561                (let ((,label (begin ,@(cddr form))))\r
562                     (pop-handler)\r
563                     ,label)))))\r
565 (define *error-hook* throw)\r
568 ;;;;; Definition of MAKE-ENVIRONMENT, to be used with two-argument EVAL\r
570 (macro (make-environment form)\r
571      `(apply (lambda ()\r
572                ,@(cdr form)\r
573                (current-environment))))\r
575 (define-macro (eval-polymorphic x . envl)\r
576   (display envl)\r
577   (let* ((env (if (null? envl) (current-environment) (eval (car envl))))\r
578          (xval (eval x env)))\r
579     (if (closure? xval)\r
580   (make-closure (get-closure-code xval) env)\r
581   xval)))\r
583 ; Redefine this if you install another package infrastructure\r
584 ; Also redefine 'package'\r
585 (define *colon-hook* eval)\r
587 ;;;;; I/O\r
589 (define (input-output-port? p)\r
590      (and (input-port? p) (output-port? p)))\r
592 (define (close-port p)\r
593      (cond\r
594           ((input-output-port? p) (close-input-port (close-output-port p)))\r
595           ((input-port? p) (close-input-port p))\r
596           ((output-port? p) (close-output-port p))\r
597           (else (throw "Not a port" p))))\r
599 (define (call-with-input-file s p)\r
600      (let ((inport (open-input-file s)))\r
601           (if (eq? inport #f)\r
602                #f\r
603                (let ((res (p inport)))\r
604                     (close-input-port inport)\r
605                     res))))\r
607 (define (call-with-output-file s p)\r
608      (let ((outport (open-output-file s)))\r
609           (if (eq? outport #f)\r
610                #f\r
611                (let ((res (p outport)))\r
612                     (close-output-port outport)\r
613                     res))))\r
615 (define (with-input-from-file s p)\r
616      (let ((inport (open-input-file s)))\r
617           (if (eq? inport #f)\r
618                #f\r
619                (let ((prev-inport (current-input-port)))\r
620                     (set-input-port inport)\r
621                     (let ((res (p)))\r
622                          (close-input-port inport)\r
623                          (set-input-port prev-inport)\r
624                          res)))))\r
626 (define (with-output-to-file s p)\r
627      (let ((outport (open-output-file s)))\r
628           (if (eq? outport #f)\r
629                #f\r
630                (let ((prev-outport (current-output-port)))\r
631                     (set-output-port outport)\r
632                     (let ((res (p)))\r
633                          (close-output-port outport)\r
634                          (set-output-port prev-outport)\r
635                          res)))))\r
637 (define (with-input-output-from-to-files si so p)\r
638      (let ((inport (open-input-file si))\r
639            (outport (open-input-file so)))\r
640           (if (not (and inport outport))\r
641                (begin\r
642                     (close-input-port inport)\r
643                     (close-output-port outport)\r
644                     #f)\r
645                (let ((prev-inport (current-input-port))\r
646                      (prev-outport (current-output-port)))\r
647                     (set-input-port inport)\r
648                     (set-output-port outport)\r
649                     (let ((res (p)))\r
650                          (close-input-port inport)\r
651                          (close-output-port outport)\r
652                          (set-input-port prev-inport)\r
653                          (set-output-port prev-outport)\r
654                          res)))))\r
656 ; Random number generator (maximum cycle)\r
657 (define *seed* 1)\r
658 (define (random-next)\r
659      (let* ((a 16807) (m 2147483647) (q (quotient m a)) (r (modulo m a)))\r
660           (set! *seed*\r
661                (-   (* a (- *seed*\r
662                          (* (quotient *seed* q) q)))\r
663                     (* (quotient *seed* q) r)))\r
664           (if (< *seed* 0) (set! *seed* (+ *seed* m)))\r
665           *seed*))\r
666 ;; SRFI-0\r
667 ;; COND-EXPAND\r
668 ;; Implemented as a macro\r
669 (define *features* '(srfi-0))\r
671 (define-macro (cond-expand . cond-action-list)\r
672   (cond-expand-runtime cond-action-list))\r
674 (define (cond-expand-runtime cond-action-list)\r
675   (if (null? cond-action-list)\r
676       #t\r
677       (if (cond-eval (caar cond-action-list))\r
678           `(begin ,@(cdar cond-action-list))\r
679           (cond-expand-runtime (cdr cond-action-list)))))\r
681 (define (cond-eval-and cond-list)\r
682   (foldr (lambda (x y) (and (cond-eval x) (cond-eval y))) #t cond-list))\r
684 (define (cond-eval-or cond-list)\r
685   (foldr (lambda (x y) (or (cond-eval x) (cond-eval y))) #f cond-list))\r
687 (define (cond-eval condition)\r
688   (cond ((symbol? condition)\r
689    (if (member condition *features*) #t #f))\r
690   ((eq? condition #t) #t)\r
691   ((eq? condition #f) #f)\r
692   (else (case (car condition)\r
693     ((and) (cond-eval-and (cdr condition)))\r
694     ((or) (cond-eval-or (cdr condition)))\r
695     ((not) (if (not (null? (cddr condition)))\r
696          (error "cond-expand : 'not' takes 1 argument")\r
697          (not (cond-eval (cadr condition)))))\r
698     (else (error "cond-expand : unknown operator" (car condition)))))))\r
700 (gc-verbose #f)\r