init.scm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. ; Initialization file for TinySCHEME 1.39
  2. ; Per R5RS, up to four deep compositions should be defined
  3. (define (caar x) (car (car x)))
  4. (define (cadr x) (car (cdr x)))
  5. (define (cdar x) (cdr (car x)))
  6. (define (cddr x) (cdr (cdr x)))
  7. (define (caaar x) (car (car (car x))))
  8. (define (caadr x) (car (car (cdr x))))
  9. (define (cadar x) (car (cdr (car x))))
  10. (define (caddr x) (car (cdr (cdr x))))
  11. (define (cdaar x) (cdr (car (car x))))
  12. (define (cdadr x) (cdr (car (cdr x))))
  13. (define (cddar x) (cdr (cdr (car x))))
  14. (define (cdddr x) (cdr (cdr (cdr x))))
  15. (define (caaaar x) (car (car (car (car x)))))
  16. (define (caaadr x) (car (car (car (cdr x)))))
  17. (define (caadar x) (car (car (cdr (car x)))))
  18. (define (caaddr x) (car (car (cdr (cdr x)))))
  19. (define (cadaar x) (car (cdr (car (car x)))))
  20. (define (cadadr x) (car (cdr (car (cdr x)))))
  21. (define (caddar x) (car (cdr (cdr (car x)))))
  22. (define (cadddr x) (car (cdr (cdr (cdr x)))))
  23. (define (cdaaar x) (cdr (car (car (car x)))))
  24. (define (cdaadr x) (cdr (car (car (cdr x)))))
  25. (define (cdadar x) (cdr (car (cdr (car x)))))
  26. (define (cdaddr x) (cdr (car (cdr (cdr x)))))
  27. (define (cddaar x) (cdr (cdr (car (car x)))))
  28. (define (cddadr x) (cdr (cdr (car (cdr x)))))
  29. (define (cdddar x) (cdr (cdr (cdr (car x)))))
  30. (define (cddddr x) (cdr (cdr (cdr (cdr x)))))
  31. (macro (unless form)
  32. `(if (not ,(cadr form)) (begin ,@(cddr form))))
  33. (macro (when form)
  34. `(if ,(cadr form) (begin ,@(cddr form))))
  35. ; DEFINE-MACRO Contributed by Andy Gaynor
  36. (macro (define-macro dform)
  37. (if (symbol? (cadr dform))
  38. `(macro ,@(cdr dform))
  39. (let ((form (gensym)))
  40. `(macro (,(caadr dform) ,form)
  41. (apply (lambda ,(cdadr dform) ,@(cddr dform)) (cdr ,form))))))
  42. ; Utilities for math. Notice that inexact->exact is primitive,
  43. ; but exact->inexact is not.
  44. (define exact? integer?)
  45. (define (inexact? x) (and (real? x) (not (integer? x))))
  46. (define (even? n) (= (remainder n 2) 0))
  47. (define (odd? n) (not (= (remainder n 2) 0)))
  48. (define (zero? n) (= n 0))
  49. (define (positive? n) (> n 0))
  50. (define (negative? n) (< n 0))
  51. (define complex? number?)
  52. (define rational? real?)
  53. (define (abs n) (if (>= n 0) n (- n)))
  54. (define (exact->inexact n) (* n 1.0))
  55. (define (<> n1 n2) (not (= n1 n2)))
  56. (define (max . lst)
  57. (foldr (lambda (a b) (if (> a b) a b)) (car lst) (cdr lst)))
  58. (define (min . lst)
  59. (foldr (lambda (a b) (if (< a b) a b)) (car lst) (cdr lst)))
  60. (define (succ x) (+ x 1))
  61. (define (pred x) (- x 1))
  62. (define (gcd a b)
  63. (let ((aa (abs a))
  64. (bb (abs b)))
  65. (if (= bb 0)
  66. aa
  67. (gcd bb (remainder aa bb)))))
  68. (define (lcm a b)
  69. (if (or (= a 0) (= b 0))
  70. 0
  71. (abs (* (quotient a (gcd a b)) b))))
  72. (define call/cc call-with-current-continuation)
  73. (define (string . charlist)
  74. (list->string charlist))
  75. (define (list->string charlist)
  76. (let* ((len (length charlist))
  77. (newstr (make-string len))
  78. (fill-string!
  79. (lambda (str i len charlist)
  80. (if (= i len)
  81. str
  82. (begin (string-set! str i (car charlist))
  83. (fill-string! str (+ i 1) len (cdr charlist)))))))
  84. (fill-string! newstr 0 len charlist)))
  85. (define (string-fill! s e)
  86. (let ((n (string-length s)))
  87. (let loop ((i 0))
  88. (if (= i n)
  89. s
  90. (begin (string-set! s i e) (loop (succ i)))))))
  91. (define (string->list s)
  92. (let loop ((n (pred (string-length s))) (l '()))
  93. (if (= n -1)
  94. l
  95. (loop (pred n) (cons (string-ref s n) l)))))
  96. (define (string-copy str)
  97. (string-append str))
  98. (define (string->anyatom str pred)
  99. (let* ((a (string->atom str)))
  100. (if (pred a) a
  101. (error "string->xxx: not a xxx" a))))
  102. (define (string->number str) (string->anyatom str number?))
  103. (define (anyatom->string n pred)
  104. (if (pred n)
  105. (atom->string n)
  106. (error "xxx->string: not a xxx" n)))
  107. (define (number->string n) (anyatom->string n number?))
  108. (define (char-cmp? cmp a b)
  109. (cmp (char->integer a) (char->integer b)))
  110. (define (char-ci-cmp? cmp a b)
  111. (cmp (char->integer (char-downcase a)) (char->integer (char-downcase b))))
  112. (define (char=? a b) (char-cmp? = a b))
  113. (define (char<? a b) (char-cmp? < a b))
  114. (define (char>? a b) (char-cmp? > a b))
  115. (define (char<=? a b) (char-cmp? <= a b))
  116. (define (char>=? a b) (char-cmp? >= a b))
  117. (define (char-ci=? a b) (char-ci-cmp? = a b))
  118. (define (char-ci<? a b) (char-ci-cmp? < a b))
  119. (define (char-ci>? a b) (char-ci-cmp? > a b))
  120. (define (char-ci<=? a b) (char-ci-cmp? <= a b))
  121. (define (char-ci>=? a b) (char-ci-cmp? >= a b))
  122. ; Note the trick of returning (cmp x y)
  123. (define (string-cmp? chcmp cmp a b)
  124. (let ((na (string-length a)) (nb (string-length b)))
  125. (let loop ((i 0))
  126. (cond
  127. ((= i na)
  128. (if (= i nb) (cmp 0 0) (cmp 0 1)))
  129. ((= i nb)
  130. (cmp 1 0))
  131. ((chcmp = (string-ref a i) (string-ref b i))
  132. (loop (succ i)))
  133. (else
  134. (chcmp cmp (string-ref a i) (string-ref b i)))))))
  135. (define (string=? a b) (string-cmp? char-cmp? = a b))
  136. (define (string<? a b) (string-cmp? char-cmp? < a b))
  137. (define (string>? a b) (string-cmp? char-cmp? > a b))
  138. (define (string<=? a b) (string-cmp? char-cmp? <= a b))
  139. (define (string>=? a b) (string-cmp? char-cmp? >= a b))
  140. (define (string-ci=? a b) (string-cmp? char-ci-cmp? = a b))
  141. (define (string-ci<? a b) (string-cmp? char-ci-cmp? < a b))
  142. (define (string-ci>? a b) (string-cmp? char-ci-cmp? > a b))
  143. (define (string-ci<=? a b) (string-cmp? char-ci-cmp? <= a b))
  144. (define (string-ci>=? a b) (string-cmp? char-ci-cmp? >= a b))
  145. (define (list . x) x)
  146. (define (foldr f x lst)
  147. (if (null? lst)
  148. x
  149. (foldr f (f x (car lst)) (cdr lst))))
  150. (define (unzip1-with-cdr . lists)
  151. (unzip1-with-cdr-iterative lists '() '()))
  152. (define (unzip1-with-cdr-iterative lists cars cdrs)
  153. (if (null? lists)
  154. (cons cars cdrs)
  155. (let ((car1 (caar lists))
  156. (cdr1 (cdar lists)))
  157. (unzip1-with-cdr-iterative
  158. (cdr lists)
  159. (append cars (list car1))
  160. (append cdrs (list cdr1))))))
  161. (define (map proc . lists)
  162. (if (null? lists)
  163. (apply proc)
  164. (if (null? (car lists))
  165. '()
  166. (let* ((unz (apply unzip1-with-cdr lists))
  167. (cars (car unz))
  168. (cdrs (cdr unz)))
  169. (cons (apply proc cars) (apply map (cons proc cdrs)))))))
  170. (define (for-each proc . lists)
  171. (if (null? lists)
  172. (apply proc)
  173. (if (null? (car lists))
  174. #t
  175. (let* ((unz (apply unzip1-with-cdr lists))
  176. (cars (car unz))
  177. (cdrs (cdr unz)))
  178. (apply proc cars) (apply map (cons proc cdrs))))))
  179. (define (list-tail x k)
  180. (if (zero? k)
  181. x
  182. (list-tail (cdr x) (- k 1))))
  183. (define (list-ref x k)
  184. (car (list-tail x k)))
  185. (define (last-pair x)
  186. (if (pair? (cdr x))
  187. (last-pair (cdr x))
  188. x))
  189. (define (head stream) (car stream))
  190. (define (tail stream) (force (cdr stream)))
  191. (define (vector-equal? x y)
  192. (and (vector? x) (vector? y) (= (vector-length x) (vector-length y))
  193. (let ((n (vector-length x)))
  194. (let loop ((i 0))
  195. (if (= i n)
  196. #t
  197. (and (equal? (vector-ref x i) (vector-ref y i))
  198. (loop (succ i))))))))
  199. (define (list->vector x)
  200. (apply vector x))
  201. (define (vector-fill! v e)
  202. (let ((n (vector-length v)))
  203. (let loop ((i 0))
  204. (if (= i n)
  205. v
  206. (begin (vector-set! v i e) (loop (succ i)))))))
  207. (define (vector->list v)
  208. (let loop ((n (pred (vector-length v))) (l '()))
  209. (if (= n -1)
  210. l
  211. (loop (pred n) (cons (vector-ref v n) l)))))
  212. ;; The following quasiquote macro is due to Eric S. Tiedemann.
  213. ;; Copyright 1988 by Eric S. Tiedemann; all rights reserved.
  214. ;;
  215. ;; Subsequently modified to handle vectors: D. Souflis
  216. (macro
  217. quasiquote
  218. (lambda (l)
  219. (define (mcons f l r)
  220. (if (and (pair? r)
  221. (eq? (car r) 'quote)
  222. (eq? (car (cdr r)) (cdr f))
  223. (pair? l)
  224. (eq? (car l) 'quote)
  225. (eq? (car (cdr l)) (car f)))
  226. (if (or (procedure? f) (number? f) (string? f))
  227. f
  228. (list 'quote f))
  229. (if (eqv? l vector)
  230. (apply l (eval r))
  231. (list 'cons l r)
  232. )))
  233. (define (mappend f l r)
  234. (if (or (null? (cdr f))
  235. (and (pair? r)
  236. (eq? (car r) 'quote)
  237. (eq? (car (cdr r)) '())))
  238. l
  239. (list 'append l r)))
  240. (define (foo level form)
  241. (cond ((not (pair? form))
  242. (if (or (procedure? form) (number? form) (string? form))
  243. form
  244. (list 'quote form))
  245. )
  246. ((eq? 'quasiquote (car form))
  247. (mcons form ''quasiquote (foo (+ level 1) (cdr form))))
  248. (#t (if (zero? level)
  249. (cond ((eq? (car form) 'unquote) (car (cdr form)))
  250. ((eq? (car form) 'unquote-splicing)
  251. (error "Unquote-splicing wasn't in a list:"
  252. form))
  253. ((and (pair? (car form))
  254. (eq? (car (car form)) 'unquote-splicing))
  255. (mappend form (car (cdr (car form)))
  256. (foo level (cdr form))))
  257. (#t (mcons form (foo level (car form))
  258. (foo level (cdr form)))))
  259. (cond ((eq? (car form) 'unquote)
  260. (mcons form ''unquote (foo (- level 1)
  261. (cdr form))))
  262. ((eq? (car form) 'unquote-splicing)
  263. (mcons form ''unquote-splicing
  264. (foo (- level 1) (cdr form))))
  265. (#t (mcons form (foo level (car form))
  266. (foo level (cdr form)))))))))
  267. (foo 0 (car (cdr l)))))
  268. ;;;;; atom? and equal? written by a.k
  269. ;;;; atom?
  270. (define (atom? x)
  271. (not (pair? x)))
  272. ;;;; equal?
  273. (define (equal? x y)
  274. (cond
  275. ((pair? x)
  276. (and (pair? y)
  277. (equal? (car x) (car y))
  278. (equal? (cdr x) (cdr y))))
  279. ((vector? x)
  280. (and (vector? y) (vector-equal? x y)))
  281. ((string? x)
  282. (and (string? y) (string=? x y)))
  283. (else (eqv? x y))))
  284. ;;;; (do ((var init inc) ...) (endtest result ...) body ...)
  285. ;;
  286. (macro do
  287. (lambda (do-macro)
  288. (apply (lambda (do vars endtest . body)
  289. (let ((do-loop (gensym)))
  290. `(letrec ((,do-loop
  291. (lambda ,(map (lambda (x)
  292. (if (pair? x) (car x) x))
  293. `,vars)
  294. (if ,(car endtest)
  295. (begin ,@(cdr endtest))
  296. (begin
  297. ,@body
  298. (,do-loop
  299. ,@(map (lambda (x)
  300. (cond
  301. ((not (pair? x)) x)
  302. ((< (length x) 3) (car x))
  303. (else (car (cdr (cdr x))))))
  304. `,vars)))))))
  305. (,do-loop
  306. ,@(map (lambda (x)
  307. (if (and (pair? x) (cdr x))
  308. (car (cdr x))
  309. '()))
  310. `,vars)))))
  311. do-macro)))
  312. ;;;; generic-member
  313. (define (generic-member cmp obj lst)
  314. (cond
  315. ((null? lst) #f)
  316. ((cmp obj (car lst)) lst)
  317. (else (generic-member cmp obj (cdr lst)))))
  318. (define (memq obj lst)
  319. (generic-member eq? obj lst))
  320. (define (memv obj lst)
  321. (generic-member eqv? obj lst))
  322. (define (member obj lst)
  323. (generic-member equal? obj lst))
  324. ;;;; generic-assoc
  325. (define (generic-assoc cmp obj alst)
  326. (cond
  327. ((null? alst) #f)
  328. ((cmp obj (caar alst)) (car alst))
  329. (else (generic-assoc cmp obj (cdr alst)))))
  330. (define (assq obj alst)
  331. (generic-assoc eq? obj alst))
  332. (define (assv obj alst)
  333. (generic-assoc eqv? obj alst))
  334. (define (assoc obj alst)
  335. (generic-assoc equal? obj alst))
  336. (define (acons x y z) (cons (cons x y) z))
  337. ;;;; Utility to ease macro creation
  338. (define (macro-expand form)
  339. ((eval (get-closure-code (eval (car form)))) form))
  340. ;;;; Handy for imperative programs
  341. ;;;; Used as: (define-with-return (foo x y) .... (return z) ...)
  342. (macro (define-with-return form)
  343. `(define ,(cadr form)
  344. (call/cc (lambda (return) ,@(cddr form)))))
  345. ;;;; Simple exception handling
  346. ;
  347. ; Exceptions are caught as follows:
  348. ;
  349. ; (catch (do-something to-recover and-return meaningful-value)
  350. ; (if-something goes-wrong)
  351. ; (with-these calls))
  352. ;
  353. ; "Catch" establishes a scope spanning multiple call-frames
  354. ; until another "catch" is encountered.
  355. ;
  356. ; Exceptions are thrown with:
  357. ;
  358. ; (throw "message")
  359. ;
  360. ; If used outside a (catch ...), reverts to (error "message)
  361. (define *handlers* (list))
  362. (define (push-handler proc)
  363. (set! *handlers* (cons proc *handlers*)))
  364. (define (pop-handler)
  365. (let ((h (car *handlers*)))
  366. (set! *handlers* (cdr *handlers*))
  367. h))
  368. (define (more-handlers?)
  369. (pair? *handlers*))
  370. (define (throw . x)
  371. (if (more-handlers?)
  372. (apply (pop-handler))
  373. (apply error x)))
  374. (macro (catch form)
  375. (let ((label (gensym)))
  376. `(call/cc (lambda (exit)
  377. (push-handler (lambda () (exit ,(cadr form))))
  378. (let ((,label (begin ,@(cddr form))))
  379. (pop-handler)
  380. ,label)))))
  381. (define *error-hook* throw)
  382. ;;;;; Definition of MAKE-ENVIRONMENT, to be used with two-argument EVAL
  383. (macro (make-environment form)
  384. `(apply (lambda ()
  385. ,@(cdr form)
  386. (current-environment))))
  387. (define-macro (eval-polymorphic x . envl)
  388. (display envl)
  389. (let* ((env (if (null? envl) (current-environment) (eval (car envl))))
  390. (xval (eval x env)))
  391. (if (closure? xval)
  392. (make-closure (get-closure-code xval) env)
  393. xval)))
  394. ; Redefine this if you install another package infrastructure
  395. ; Also redefine 'package'
  396. (define *colon-hook* eval)
  397. ;;;;; I/O
  398. (define (input-output-port? p)
  399. (and (input-port? p) (output-port? p)))
  400. (define (close-port p)
  401. (cond
  402. ((input-output-port? p) (close-input-port (close-output-port p)))
  403. ((input-port? p) (close-input-port p))
  404. ((output-port? p) (close-output-port p))
  405. (else (throw "Not a port" p))))
  406. (define (call-with-input-file s p)
  407. (let ((inport (open-input-file s)))
  408. (if (eq? inport #f)
  409. #f
  410. (let ((res (p inport)))
  411. (close-input-port inport)
  412. res))))
  413. (define (call-with-output-file s p)
  414. (let ((outport (open-output-file s)))
  415. (if (eq? outport #f)
  416. #f
  417. (let ((res (p outport)))
  418. (close-output-port outport)
  419. res))))
  420. (define (with-input-from-file s p)
  421. (let ((inport (open-input-file s)))
  422. (if (eq? inport #f)
  423. #f
  424. (let ((prev-inport (current-input-port)))
  425. (set-input-port inport)
  426. (let ((res (p)))
  427. (close-input-port inport)
  428. (set-input-port prev-inport)
  429. res)))))
  430. (define (with-output-to-file s p)
  431. (let ((outport (open-output-file s)))
  432. (if (eq? outport #f)
  433. #f
  434. (let ((prev-outport (current-output-port)))
  435. (set-output-port outport)
  436. (let ((res (p)))
  437. (close-output-port outport)
  438. (set-output-port prev-outport)
  439. res)))))
  440. (define (with-input-output-from-to-files si so p)
  441. (let ((inport (open-input-file si))
  442. (outport (open-input-file so)))
  443. (if (not (and inport outport))
  444. (begin
  445. (close-input-port inport)
  446. (close-output-port outport)
  447. #f)
  448. (let ((prev-inport (current-input-port))
  449. (prev-outport (current-output-port)))
  450. (set-input-port inport)
  451. (set-output-port outport)
  452. (let ((res (p)))
  453. (close-input-port inport)
  454. (close-output-port outport)
  455. (set-input-port prev-inport)
  456. (set-output-port prev-outport)
  457. res)))))
  458. ; Random number generator (maximum cycle)
  459. (define *seed* 1)
  460. (define (random-next)
  461. (let* ((a 16807) (m 2147483647) (q (quotient m a)) (r (modulo m a)))
  462. (set! *seed*
  463. (- (* a (- *seed*
  464. (* (quotient *seed* q) q)))
  465. (* (quotient *seed* q) r)))
  466. (if (< *seed* 0) (set! *seed* (+ *seed* m)))
  467. *seed*))
  468. ;; SRFI-0
  469. ;; COND-EXPAND
  470. ;; Implemented as a macro
  471. (define *features* '(srfi-0))
  472. (define-macro (cond-expand . cond-action-list)
  473. (cond-expand-runtime cond-action-list))
  474. (define (cond-expand-runtime cond-action-list)
  475. (if (null? cond-action-list)
  476. #t
  477. (if (cond-eval (caar cond-action-list))
  478. `(begin ,@(cdar cond-action-list))
  479. (cond-expand-runtime (cdr cond-action-list)))))
  480. (define (cond-eval-and cond-list)
  481. (foldr (lambda (x y) (and (cond-eval x) (cond-eval y))) #t cond-list))
  482. (define (cond-eval-or cond-list)
  483. (foldr (lambda (x y) (or (cond-eval x) (cond-eval y))) #f cond-list))
  484. (define (cond-eval condition)
  485. (cond ((symbol? condition)
  486. (if (member condition *features*) #t #f))
  487. ((eq? condition #t) #t)
  488. ((eq? condition #f) #f)
  489. (else (case (car condition)
  490. ((and) (cond-eval-and (cdr condition)))
  491. ((or) (cond-eval-or (cdr condition)))
  492. ((not) (if (not (null? (cddr condition)))
  493. (error "cond-expand : 'not' takes 1 argument")
  494. (not (cond-eval (cadr condition)))))
  495. (else (error "cond-expand : unknown operator" (car condition)))))))
  496. (gc-verbose #f)