fork download
  1. ;;; simply.scm version 3.13 (8/11/98)
  2.  
  3. ;;; This file uses Scheme features we don't talk about in _Simply_Scheme_.
  4. ;;; Read at your own risk.
  5.  
  6. (if (equal? 'foo (symbol->string 'foo))
  7. (error "Simply.scm already loaded!!")
  8. #f)
  9.  
  10. ;; Make number->string remove leading "+" if necessary
  11.  
  12. (if (char=? #\+ (string-ref (number->string 1.0) 0))
  13. (let ((old-ns number->string) (char=? char=?) (string-ref string-ref)
  14. (substring substring) (string-length string-length))
  15. (set! number->string
  16. (lambda args
  17. (let ((result (apply old-ns args)))
  18. (if (char=? #\+ (string-ref result 0))
  19. (substring result 1 (string-length result))
  20. result)))))
  21. 'no-problem)
  22.  
  23. (define number->string
  24. (let ((old-ns number->string) (string? string?))
  25. (lambda args
  26. (if (string? (car args))
  27. (car args)
  28. (apply old-ns args)))))
  29.  
  30. ;; Get strings in error messages to print nicely (especially "")
  31.  
  32. (define whoops
  33. (let ((string? string?) (string-append string-append) (error error)
  34. (cons cons) (map map) (apply apply))
  35. (define (error-printform x)
  36. (if (string? x)
  37. (string-append "\"" x "\"")
  38. x))
  39. (lambda (string . args)
  40. (apply error (cons string (map error-printform args))))))
  41.  
  42.  
  43. ;; ROUND returns an inexact integer if its argument is inexact,
  44. ;; but we think it should always return an exact integer.
  45. ;; (It matters because some Schemes print inexact integers as "+1.0".)
  46. ;; The (exact 1) test is for PC Scheme, in which nothing is exact.
  47. (if (and (inexact? (round (sqrt 2))) (exact? 1))
  48. (let ((old-round round) (inexact->exact inexact->exact))
  49. (set! round
  50. (lambda (number)
  51. (inexact->exact (old-round number)))))
  52. 'no-problem)
  53.  
  54. ;; Remainder and quotient blow up if their argument isn't an integer.
  55. ;; Unfortunately, in SCM, (* 365.25 24 60 60) *isn't* an integer.
  56.  
  57. (if (inexact? (* .25 4))
  58. (let ((rem remainder) (quo quotient) (inexact->exact inexact->exact)
  59. (integer? integer?))
  60. (set! remainder
  61. (lambda (x y)
  62. (rem (if (integer? x) (inexact->exact x) x)
  63. (if (integer? y) (inexact->exact y) y))))
  64. (set! quotient
  65. (lambda (x y)
  66. (quo (if (integer? x) (inexact->exact x) x)
  67. (if (integer? y) (inexact->exact y) y)))))
  68. 'done)
  69.  
  70.  
  71. ;; Random
  72. ;; If your version of Scheme has RANDOM, you should take this out.
  73. ;; (It gives the same sequence of random numbers every time.)
  74.  
  75. (define random
  76. (let ((*seed* 1) (quotient quotient) (modulo modulo) (+ +) (- -) (* *) (> >))
  77. (lambda (x)
  78. (let* ((hi (quotient *seed* 127773))
  79. (low (modulo *seed* 127773))
  80. (test (- (* 16807 low) (* 2836 hi))))
  81. (if (> test 0)
  82. (set! *seed* test)
  83. (set! *seed* (+ test 2147483647))))
  84. (modulo *seed* x))))
  85.  
  86.  
  87. ;;; Logo-style word/sentence implementation
  88.  
  89. (define word?
  90. (let ((number? number?) (symbol? symbol?) (string? string?))
  91. (lambda (x)
  92. (or (symbol? x) (number? x) (string? x)))))
  93.  
  94. (define sentence?
  95. (let ((null? null?) (pair? pair?) (word? word?) (car car) (cdr cdr))
  96. (define (list-of-words? l)
  97. (cond ((null? l) #t)
  98. ((pair? l)
  99. (and (word? (car l)) (list-of-words? (cdr l))))
  100. (else #f)))
  101. list-of-words?))
  102.  
  103. (define empty?
  104. (let ((null? null?) (string? string?) (string=? string=?))
  105. (lambda (x)
  106. (or (null? x)
  107. (and (string? x) (string=? x ""))))))
  108.  
  109.  
  110. (define char-rank
  111. ;; 0 Letter in good case or special initial
  112. ;; 1 ., + or -
  113. ;; 2 Digit
  114. ;; 3 Letter in bad case or weird character
  115. (let ((*the-char-ranks* (make-vector 256 3))
  116. (= =) (+ +) (string-ref string-ref) (string-length string-length)
  117. (vector-set! vector-set!) (char->integer char->integer)
  118. (symbol->string symbol->string) (vector-ref vector-ref))
  119. (define (rank-string str rank)
  120. (define (helper i len)
  121. (if (= i len)
  122. 'done
  123. (begin (vector-set! *the-char-ranks*
  124. (char->integer (string-ref str i))
  125. rank)
  126. (helper (+ i 1) len))))
  127. (helper 0 (string-length str)))
  128. (rank-string (symbol->string 'abcdefghijklmnopqrstuvwxyz) 0)
  129. (rank-string "!$%&*/:<=>?~_^" 0)
  130. (rank-string "+-." 1)
  131. (rank-string "0123456789" 2)
  132. (lambda (char) ;; value of char-rank
  133. (vector-ref *the-char-ranks* (char->integer char)))))
  134.  
  135. (define string->word
  136. (let ((= =) (<= <=) (+ +) (- -) (char-rank char-rank) (string-ref string-ref)
  137. (string-length string-length) (string=? string=?) (not not)
  138. (char=? char=?) (string->number string->number)
  139. (string->symbol string->symbol))
  140. (lambda (string)
  141. (define (subsequents? string i length)
  142. (cond ((= i length) #t)
  143. ((<= (char-rank (string-ref string i)) 2)
  144. (subsequents? string (+ i 1) length))
  145. (else #f)))
  146. (define (special-id? string)
  147. (or (string=? string "+")
  148. (string=? string "-")
  149. (string=? string "...")))
  150. (define (ok-symbol? string)
  151. (if (string=? string "")
  152. #f
  153. (let ((rank1 (char-rank (string-ref string 0))))
  154. (cond ((= rank1 0) (subsequents? string 1 (string-length string)))
  155. ((= rank1 1) (special-id? string))
  156. (else #f)))))
  157.  
  158. (define (nn-helper string i len seen-point?)
  159. (cond ((= i len)
  160. (if seen-point?
  161. (not (char=? (string-ref string (- len 1)) #\0))
  162. #t))
  163. ((char=? #\. (string-ref string i))
  164. (cond (seen-point? #f)
  165. ((= (+ i 2) len) #t) ; Accepts "23.0"
  166. (else (nn-helper string (+ i 1) len #t))))
  167. ((= 2 (char-rank (string-ref string i)))
  168. (nn-helper string (+ i 1) len seen-point?))
  169. (else #f)))
  170. (define (narrow-number? string)
  171. (if (string=? string "")
  172. #f
  173. (let* ((c0 (string-ref string 0))
  174. (start 0)
  175. (len (string-length string))
  176. (cn (string-ref string (- len 1))))
  177. (if (and (char=? c0 #\-) (not (= len 1)))
  178. (begin
  179. (set! start 1)
  180. (set! c0 (string-ref string 1)))
  181. #f)
  182. (cond ((not (= (char-rank cn) 2)) #f) ; Rejects "-" among others
  183. ((char=? c0 #\.) #f)
  184. ((char=? c0 #\0)
  185. (cond ((= len 1) #t) ; Accepts "0" but not "-0"
  186. ((= len 2) #f) ; Rejects "-0" and "03"
  187. ((char=? (string-ref string (+ start 1)) #\.)
  188. (nn-helper string (+ start 2) len #t))
  189. (else #f)))
  190. (else (nn-helper string start len #f))))))
  191.  
  192. ;; The body of string->word:
  193. (cond ((narrow-number? string) (string->number string))
  194. ((ok-symbol? string) (string->symbol string))
  195. (else string)))))
  196.  
  197. (define char->word
  198. (let ((= =) (char-rank char-rank) (make-string make-string) (char=? char=?)
  199. (string->symbol string->symbol) (string->number string->number))
  200. (lambda (char)
  201. (let ((rank (char-rank char))
  202. (string (make-string 1 char)))
  203. (cond ((= rank 0) (string->symbol string))
  204. ((= rank 2) (string->number string))
  205. ((char=? char #\+) '+)
  206. ((char=? char #\-) '-)
  207. (else string))))))
  208.  
  209. (define word->string
  210. (let ((number? number?) (string? string?) (number->string number->string)
  211. (symbol->string symbol->string))
  212. (lambda (wd)
  213. (cond ((string? wd) wd)
  214. ((number? wd) (number->string wd))
  215. (else (symbol->string wd))))))
  216.  
  217. (define count
  218. (let ((word? word?) (string-length string-length)
  219. (word->string word->string) (length length))
  220. (lambda (stuff)
  221. (if (word? stuff)
  222. (string-length (word->string stuff))
  223. (length stuff)))))
  224.  
  225. (define word
  226. (let ((string->word string->word) (apply apply) (string-append string-append)
  227. (map map) (word? word?) (word->string word->string) (whoops whoops))
  228. (lambda x
  229. (string->word
  230. (apply string-append
  231. (map (lambda (arg)
  232. (if (word? arg)
  233. (word->string arg)
  234. (whoops "Invalid argument to WORD: " arg)))
  235. x))))))
  236.  
  237. (define se
  238. (let ((pair? pair?) (null? null?) (word? word?) (car car) (cons cons)
  239. (cdr cdr) (whoops whoops))
  240. (define (paranoid-append a original-a b)
  241. (cond ((null? a) b)
  242. ((word? (car a))
  243. (cons (car a) (paranoid-append (cdr a) original-a b)))
  244. (else (whoops "Argument to SENTENCE not a word or sentence"
  245. original-a ))))
  246. (define (combine-two a b) ;; Note: b is always a list
  247. (cond ((pair? a) (paranoid-append a a b))
  248. ((null? a) b)
  249. ((word? a) (cons a b))
  250. (else (whoops "Argument to SENTENCE not a word or sentence:" a))))
  251. ;; Helper function so recursive calls don't show up in TRACE
  252. (define (real-se args)
  253. (if (null? args)
  254. '()
  255. (combine-two (car args) (real-se (cdr args)))))
  256. (lambda args
  257. (real-se args))))
  258.  
  259. (define sentence se)
  260.  
  261. (define first
  262. (let ((pair? pair?) (char->word char->word) (string-ref string-ref)
  263. (word->string word->string) (car car) (empty? empty?)
  264. (whoops whoops) (word? word?))
  265. (define (word-first wd)
  266. (char->word (string-ref (word->string wd) 0)))
  267. (lambda (x)
  268. (cond ((pair? x) (car x))
  269. ((empty? x) (whoops "Invalid argument to FIRST: " x))
  270. ((word? x) (word-first x))
  271. (else (whoops "Invalid argument to FIRST: " x))))))
  272.  
  273. (define last
  274. (let ((pair? pair?) (- -) (word->string word->string) (char->word char->word)
  275. (string-ref string-ref) (string-length string-length) (empty? empty?)
  276. (cdr cdr) (car car) (whoops whoops) (word? word?))
  277. (define (word-last wd)
  278. (let ((s (word->string wd)))
  279. (char->word (string-ref s (- (string-length s) 1)))))
  280. (define (list-last lst)
  281. (if (empty? (cdr lst))
  282. (car lst)
  283. (list-last (cdr lst))))
  284. (lambda (x)
  285. (cond ((pair? x) (list-last x))
  286. ((empty? x) (whoops "Invalid argument to LAST: " x))
  287. ((word? x) (word-last x))
  288. (else (whoops "Invalid argument to LAST: " x))))))
  289.  
  290. (define bf
  291. (let ((pair? pair?) (substring substring) (string-length string-length)
  292. (string->word string->word) (word->string word->string) (cdr cdr)
  293. (empty? empty?) (whoops whoops) (word? word?))
  294. (define string-bf
  295. (lambda (s)
  296. (substring s 1 (string-length s))))
  297. (define (word-bf wd)
  298. (string->word (string-bf (word->string wd))))
  299. (lambda (x)
  300. (cond ((pair? x) (cdr x))
  301. ((empty? x) (whoops "Invalid argument to BUTFIRST: " x))
  302. ((word? x) (word-bf x))
  303. (else (whoops "Invalid argument to BUTFIRST: " x))))))
  304.  
  305. (define butfirst bf)
  306.  
  307. (define bl
  308. (let ((pair? pair?) (- -) (cdr cdr) (cons cons) (car car) (substring substring)
  309. (string-length string-length) (string->word string->word)
  310. (word->string word->string) (empty? empty?) (whoops whoops) (word? word?))
  311. (define (list-bl list)
  312. (if (null? (cdr list))
  313. '()
  314. (cons (car list) (list-bl (cdr list)))))
  315. (define (string-bl s)
  316. (substring s 0 (- (string-length s) 1)))
  317. (define (word-bl wd)
  318. (string->word (string-bl (word->string wd))))
  319. (lambda (x)
  320. (cond ((pair? x) (list-bl x))
  321. ((empty? x) (whoops "Invalid argument to BUTLAST: " x))
  322. ((word? x) (word-bl x))
  323. (else (whoops "Invalid argument to BUTLAST: " x))))))
  324.  
  325. (define butlast bl)
  326.  
  327. (define item
  328. (let ((> >) (- -) (< <) (integer? integer?) (list-ref list-ref)
  329. (char->word char->word) (string-ref string-ref)
  330. (word->string word->string) (not not) (whoops whoops)
  331. (count count) (word? word?) (list? list?))
  332. (define (word-item n wd)
  333. (char->word (string-ref (word->string wd) (- n 1))))
  334. (lambda (n stuff)
  335. (cond ((not (integer? n))
  336. (whoops "Invalid first argument to ITEM (must be an integer): "
  337. n))
  338. ((< n 1)
  339. (whoops "Invalid first argument to ITEM (must be positive): "
  340. n))
  341. ((> n (count stuff))
  342. (whoops "No such item: " n stuff))
  343. ((word? stuff) (word-item n stuff))
  344. ((list? stuff) (list-ref stuff (- n 1)))
  345. (else (whoops "Invalid second argument to ITEM: " stuff))))))
  346.  
  347. (define equal?
  348. ;; Note that EQUAL? assumes strings are numbers.
  349. ;; (strings-are-numbers #f) doesn't change this behavior.
  350. (let ((vector-length vector-length) (= =) (vector-ref vector-ref)
  351. (+ +) (string? string?) (symbol? symbol?) (null? null?) (pair? pair?)
  352. (car car) (cdr cdr) (eq? eq?) (string=? string=?)
  353. (symbol->string symbol->string) (number? number?)
  354. (string->word string->word) (vector? vector?) (eqv? eqv?))
  355. (define (vector-equal? v1 v2)
  356. (let ((len1 (vector-length v1))
  357. (len2 (vector-length v2)))
  358. (define (helper i)
  359. (if (= i len1)
  360. #t
  361. (and (equal? (vector-ref v1 i) (vector-ref v2 i))
  362. (helper (+ i 1)))))
  363. (if (= len1 len2)
  364. (helper 0)
  365. #f)))
  366. (lambda (x y)
  367. (cond ((null? x) (null? y))
  368. ((null? y) #f)
  369. ((pair? x)
  370. (and (pair? y)
  371. (equal? (car x) (car y))
  372. (equal? (cdr x) (cdr y))))
  373. ((pair? y) #f)
  374. ((symbol? x)
  375. (or (and (symbol? y) (eq? x y))
  376. (and (string? y) (string=? (symbol->string x) y))))
  377. ((symbol? y)
  378. (and (string? x) (string=? x (symbol->string y))))
  379. ((number? x)
  380. (or (and (number? y) (= x y))
  381. (and (string? y)
  382. (let ((possible-num (string->word y)))
  383. (and (number? possible-num)
  384. (= x possible-num))))))
  385. ((number? y)
  386. (and (string? x)
  387. (let ((possible-num (string->word x)))
  388. (and (number? possible-num)
  389. (= possible-num y)))))
  390. ((string? x) (and (string? y) (string=? x y)))
  391. ((string? y) #f)
  392. ((vector? x) (and (vector? y) (vector-equal? x y)))
  393. ((vector? y) #f)
  394. (else (eqv? x y))))))
  395.  
  396. (define member?
  397. (let ((> >) (- -) (< <) (null? null?) (symbol? symbol?) (eq? eq?) (car car)
  398. (not not) (symbol->string symbol->string) (string=? string=?)
  399. (cdr cdr) (equal? equal?) (word->string word->string)
  400. (string-length string-length) (whoops whoops) (string-ref string-ref)
  401. (char=? char=?) (list? list?) (number? number?) (empty? empty?)
  402. (word? word?) (string? string?))
  403. (define (symbol-in-list? symbol string lst)
  404. (cond ((null? lst) #f)
  405. ((and (symbol? (car lst))
  406. (eq? symbol (car lst))))
  407. ((string? (car lst))
  408. (cond ((not string)
  409. (symbol-in-list? symbol (symbol->string symbol) lst))
  410. ((string=? string (car lst)) #t)
  411. (else (symbol-in-list? symbol string (cdr lst)))))
  412. (else (symbol-in-list? symbol string (cdr lst)))))
  413. (define (word-in-list? wd lst)
  414. (cond ((null? lst) #f)
  415. ((equal? wd (car lst)) #t)
  416. (else (word-in-list? wd (cdr lst)))))
  417. (define (word-in-word? small big)
  418. (let ((one-letter-str (word->string small)))
  419. (if (> (string-length one-letter-str) 1)
  420. (whoops "Invalid arguments to MEMBER?: " small big)
  421. (let ((big-str (word->string big)))
  422. (char-in-string? (string-ref one-letter-str 0)
  423. big-str
  424. (- (string-length big-str) 1))))))
  425. (define (char-in-string? char string i)
  426. (cond ((< i 0) #f)
  427. ((char=? char (string-ref string i)) #t)
  428. (else (char-in-string? char string (- i 1)))))
  429. (lambda (x stuff)
  430. (cond ((empty? stuff) #f)
  431. ((word? stuff) (word-in-word? x stuff))
  432. ((not (list? stuff))
  433. (whoops "Invalid second argument to MEMBER?: " stuff))
  434. ((symbol? x) (symbol-in-list? x #f stuff))
  435. ((or (number? x) (string? x))
  436. (word-in-list? x stuff))
  437. (else (whoops "Invalid first argument to MEMBER?: " x))))))
  438.  
  439. (define before?
  440. (let ((not not) (word? word?) (whoops whoops) (string<? string<?)
  441. (word->string word->string))
  442. (lambda (wd1 wd2)
  443. (cond ((not (word? wd1))
  444. (whoops "Invalid first argument to BEFORE? (not a word): " wd1))
  445. ((not (word? wd2))
  446. (whoops "Invalid second argument to BEFORE? (not a word): " wd2))
  447. (else (string<? (word->string wd1) (word->string wd2)))))))
  448.  
  449.  
  450. ;;; Higher Order Functions
  451.  
  452. (define filter
  453. (let ((null? null?) (car car) (cons cons) (cdr cdr) (not not)
  454. (procedure? procedure?) (whoops whoops) (list? list?))
  455. (lambda (pred l)
  456. ;; Helper function so recursive calls don't show up in TRACE
  457. (define (real-filter l)
  458. (cond ((null? l) '())
  459. ((pred (car l))
  460. (cons (car l) (real-filter (cdr l))))
  461. (else (real-filter (cdr l)))))
  462. (cond ((not (procedure? pred))
  463. (whoops "Invalid first argument to FILTER (not a procedure): "
  464. pred))
  465. ((not (list? l))
  466. (whoops "Invalid second argument to FILTER (not a list): " l))
  467. (else (real-filter l))))))
  468.  
  469. (define keep
  470. (let ((+ +) (= =) (pair? pair?) (substring substring)
  471. (char->word char->word) (string-ref string-ref)
  472. (string-set! string-set!) (word->string word->string)
  473. (string-length string-length) (string->word string->word)
  474. (make-string make-string) (procedure? procedure?)
  475. (whoops whoops) (word? word?) (null? null?))
  476. (lambda (pred w-or-s)
  477. (define (keep-string in i out out-len len)
  478. (cond ((= i len) (substring out 0 out-len))
  479. ((pred (char->word (string-ref in i)))
  480. (string-set! out out-len (string-ref in i))
  481. (keep-string in (+ i 1) out (+ out-len 1) len))
  482. (else (keep-string in (+ i 1) out out-len len))))
  483. (define (keep-word wd)
  484. (let* ((string (word->string wd))
  485. (len (string-length string)))
  486. (string->word
  487. (keep-string string 0 (make-string len) 0 len))))
  488. (cond ((not (procedure? pred))
  489. (whoops "Invalid first argument to KEEP (not a procedure): "
  490. pred))
  491. ((pair? w-or-s) (filter pred w-or-s))
  492. ((word? w-or-s) (keep-word w-or-s))
  493. ((null? w-or-s) '())
  494. (else
  495. (whoops "Bad second argument to KEEP (not a word or sentence): "
  496. w-or-s))))))
  497.  
  498. (define appearances
  499. (let ((count count) (keep keep) (equal? equal?))
  500. (lambda (item aggregate)
  501. (count (keep (lambda (element) (equal? item element)) aggregate)))))
  502.  
  503. (define every
  504. (let ((= =) (+ +) (se se) (char->word char->word) (string-ref string-ref)
  505. (empty? empty?) (first first) (bf bf) (not not) (procedure? procedure?)
  506. (whoops whoops) (word? word?) (word->string word->string)
  507. (string-length string-length))
  508. (lambda (fn stuff)
  509. (define (string-every string i length)
  510. (if (= i length)
  511. '()
  512. (se (fn (char->word (string-ref string i)))
  513. (string-every string (+ i 1) length))))
  514. (define (sent-every sent)
  515. ;; This proc. can't be optimized or else it will break the
  516. ;; exercise where we ask them to reimplement sentences as
  517. ;; vectors and then see if every still works.
  518. (if (empty? sent)
  519. sent ; Can't be '() or exercise breaks.
  520. (se (fn (first sent))
  521. (sent-every (bf sent)))))
  522. (cond ((not (procedure? fn))
  523. (whoops "Invalid first argument to EVERY (not a procedure):"
  524. fn))
  525. ((word? stuff)
  526. (let ((string (word->string stuff)))
  527. (string-every string 0 (string-length string))))
  528. (else (sent-every stuff))))))
  529.  
  530. (define accumulate
  531. (let ((not not) (empty? empty?) (bf bf) (first first) (procedure? procedure?)
  532. (whoops whoops) (member member) (list list))
  533. (lambda (combiner stuff)
  534. (define (real-accumulate stuff)
  535. (if (empty? (bf stuff))
  536. (first stuff)
  537. (combiner (first stuff) (real-accumulate (bf stuff)))))
  538. (cond ((not (procedure? combiner))
  539. (whoops "Invalid first argument to ACCUMULATE (not a procedure):"
  540. combiner))
  541. ((not (empty? stuff)) (real-accumulate stuff))
  542. ((member combiner (list + * word se)) (combiner))
  543. (else
  544. (whoops "Can't accumulate empty input with that combiner"))))))
  545.  
  546. (define reduce
  547. (let ((null? null?) (cdr cdr) (car car) (not not) (procedure? procedure?)
  548. (whoops whoops) (member member) (list list))
  549. (lambda (combiner stuff)
  550. (define (real-reduce stuff)
  551. (if (null? (cdr stuff))
  552. (car stuff)
  553. (combiner (car stuff) (real-reduce (cdr stuff)))))
  554. (cond ((not (procedure? combiner))
  555. (whoops "Invalid first argument to REDUCE (not a procedure):"
  556. combiner))
  557. ((not (null? stuff)) (real-reduce stuff))
  558. ((member combiner (list + * word se append)) (combiner))
  559. (else (whoops "Can't reduce empty input with that combiner"))))))
  560.  
  561. (define repeated
  562. (let ((= =) (- -))
  563. (lambda (fn number)
  564. (if (= number 0)
  565. (lambda (x) x)
  566. (lambda (x)
  567. ((repeated fn (- number 1)) (fn x)))))))
  568.  
  569.  
  570. ;; Tree stuff
  571. (define make-node cons)
  572. (define datum car)
  573. (define children cdr)
  574.  
  575.  
  576. ;; I/O
  577.  
  578. (define show
  579. (let ((= =) (length length) (display display) (car car) (newline newline)
  580. (not not) (output-port? output-port?) (apply apply) (whoops whoops))
  581. (lambda args
  582. (cond
  583. ((= (length args) 1)
  584. (display (car args))
  585. (newline))
  586. ((= (length args) 2)
  587. (if (not (output-port? (car (cdr args))))
  588. (whoops "Invalid second argument to SHOW (not an output port): "
  589. (car (cdr args))))
  590. (apply display args)
  591. (newline (car (cdr args))))
  592. (else (whoops "Incorrect number of arguments to procedure SHOW"))))))
  593.  
  594. (define show-line
  595. (let ((>= >=) (length length) (whoops whoops) (null? null?)
  596. (current-output-port current-output-port) (car car) (not not)
  597. (list? list?) (display display) (for-each for-each) (cdr cdr)
  598. (newline newline))
  599. (lambda (line . args)
  600. (if (>= (length args) 2)
  601. (whoops "Too many arguments to show-line")
  602. (let ((port (if (null? args) (current-output-port) (car args))))
  603. (cond ((not (list? line))
  604. (whoops "Invalid argument to SHOW-LINE (not a list):" line))
  605. ((null? line) #f)
  606. (else
  607. (display (car line) port)
  608. (for-each (lambda (wd) (display " " port) (display wd port))
  609. (cdr line))))
  610. (newline port))))))
  611.  
  612. (define read-string
  613. (let ((read-char read-char) (eqv? eqv?) (apply apply)
  614. (string-append string-append) (substring substring) (reverse reverse)
  615. (cons cons) (>= >=) (+ +) (string-set! string-set!) (length length)
  616. (whoops whoops) (null? null?) (current-input-port current-input-port)
  617. (car car) (cdr cdr) (eof-object? eof-object?) (list list)
  618. (make-string make-string) (peek-char peek-char))
  619. (define (read-string-helper chars all-length chunk-length port)
  620. (let ((char (read-char port))
  621. (string (car chars)))
  622. (cond ((or (eof-object? char) (eqv? char #\newline))
  623. (apply string-append
  624. (reverse
  625. (cons
  626. (substring (car chars) 0 chunk-length)
  627. (cdr chars)))))
  628. ((>= chunk-length 80)
  629. (let ((newstring (make-string 80)))
  630. (string-set! newstring 0 char)
  631. (read-string-helper (cons newstring chars)
  632. (+ all-length 1)
  633. 1
  634. port)))
  635. (else
  636. (string-set! string chunk-length char)
  637. (read-string-helper chars
  638. (+ all-length 1)
  639. (+ chunk-length 1)
  640. port)))))
  641. (lambda args
  642. (if (>= (length args) 2)
  643. (whoops "Too many arguments to read-string")
  644. (let ((port (if (null? args) (current-input-port) (car args))))
  645. (if (eof-object? (peek-char port))
  646. (read-char port)
  647. (read-string-helper (list (make-string 80)) 0 0 port)))))))
  648.  
  649. (define read-line
  650. (let ((= =) (list list) (string->word string->word) (substring substring)
  651. (char-whitespace? char-whitespace?) (string-ref string-ref)
  652. (+ +) (string-length string-length) (apply apply)
  653. (read-string read-string))
  654. (lambda args
  655. (define (tokenize string)
  656. (define (helper i start len)
  657. (cond ((= i len)
  658. (if (= i start)
  659. '()
  660. (list (string->word (substring string start i)))))
  661. ((char-whitespace? (string-ref string i))
  662. (if (= i start)
  663. (helper (+ i 1) (+ i 1) len)
  664. (cons (string->word (substring string start i))
  665. (helper (+ i 1) (+ i 1) len))))
  666. (else (helper (+ i 1) start len))))
  667. (if (eof-object? string)
  668. string
  669. (helper 0 0 (string-length string))))
  670. (tokenize (apply read-string args)))))
  671.  
  672. (define *the-open-inports* '())
  673. (define *the-open-outports* '())
  674.  
  675. (define align
  676. (let ((< <) (abs abs) (* *) (expt expt) (>= >=) (- -) (+ +) (= =)
  677. (null? null?) (car car) (round round) (number->string number->string)
  678. (string-length string-length) (string-append string-append)
  679. (make-string make-string) (substring substring)
  680. (string-set! string-set!) (number? number?)
  681. (word->string word->string))
  682. (lambda (obj width . rest)
  683. (define (align-number obj width rest)
  684. (let* ((sign (< obj 0))
  685. (num (abs obj))
  686. (prec (if (null? rest) 0 (car rest)))
  687. (big (round (* num (expt 10 prec))))
  688. (cvt0 (number->string big))
  689. (cvt (if (< num 1) (string-append "0" cvt0) cvt0))
  690. (pos-str (if (>= (string-length cvt0) prec)
  691. cvt
  692. (string-append
  693. (make-string (- prec (string-length cvt0)) #\0)
  694. cvt)))
  695. (string (if sign (string-append "-" pos-str) pos-str))
  696. (length (+ (string-length string)
  697. (if (= prec 0) 0 1)))
  698. (left (- length (+ 1 prec)))
  699. (result (if (= prec 0)
  700. string
  701. (string-append
  702. (substring string 0 left)
  703. "."
  704. (substring string left (- length 1))))))
  705. (cond ((= length width) result)
  706. ((< length width)
  707. (string-append (make-string (- width length) #\space) result))
  708. (else (let ((new (substring result 0 width)))
  709. (string-set! new (- width 1) #\+)
  710. new)))))
  711. (define (align-word string)
  712. (let ((length (string-length string)))
  713. (cond ((= length width) string)
  714. ((< length width)
  715. (string-append string (make-string (- width length) #\space)))
  716. (else (let ((new (substring string 0 width)))
  717. (string-set! new (- width 1) #\+)
  718. new)))))
  719. (if (number? obj)
  720. (align-number obj width rest)
  721. (align-word (word->string obj))))))
  722.  
  723. (define open-output-file
  724. (let ((oof open-output-file) (cons cons))
  725. (lambda (filename)
  726. (let ((port (oof filename)))
  727. (set! *the-open-outports* (cons port *the-open-outports*))
  728. port))))
  729.  
  730. (define open-input-file
  731. (let ((oif open-input-file) (cons cons))
  732. (lambda (filename)
  733. (let ((port (oif filename)))
  734. (set! *the-open-inports* (cons port *the-open-inports*))
  735. port))))
  736.  
  737. (define remove!
  738. (let ((null? null?) (cdr cdr) (eq? eq?) (set-cdr! set-cdr!) (car car))
  739. (lambda (thing lst)
  740. (define (r! prev)
  741. (cond ((null? (cdr prev)) lst)
  742. ((eq? thing (car (cdr prev)))
  743. (set-cdr! prev (cdr (cdr prev)))
  744. lst)
  745. (else (r! (cdr prev)))))
  746. (cond ((null? lst) lst)
  747. ((eq? thing (car lst)) (cdr lst))
  748. (else (r! lst))))))
  749.  
  750. (define close-input-port
  751. (let ((cip close-input-port) (remove! remove!))
  752. (lambda (port)
  753. (set! *the-open-inports* (remove! port *the-open-inports*))
  754. (cip port))))
  755.  
  756. (define close-output-port
  757. (let ((cop close-output-port) (remove! remove!))
  758. (lambda (port)
  759. (set! *the-open-outports* (remove! port *the-open-outports*))
  760. (cop port))))
  761.  
  762. (define close-all-ports
  763. (let ((for-each for-each)
  764. (close-input-port close-input-port)
  765. (close-output-port close-output-port))
  766. (lambda ()
  767. (for-each close-input-port *the-open-inports*)
  768. (for-each close-output-port *the-open-outports*)
  769. 'closed)))
  770.  
  771. ;; Make arithmetic work on numbers in string form:
  772. (define maybe-num
  773. (let ((string? string?) (string->number string->number))
  774. (lambda (arg)
  775. (if (string? arg)
  776. (let ((num (string->number arg)))
  777. (if num num arg))
  778. arg))))
  779.  
  780. (define logoize
  781. (let ((apply apply) (map map) (maybe-num maybe-num))
  782. (lambda (fn)
  783. (lambda args
  784. (apply fn (map maybe-num args))))))
  785.  
  786. ;; special case versions of logoize, since (lambda args ...) is expensive
  787. (define logoize-1
  788. (let ((maybe-num maybe-num))
  789. (lambda (fn)
  790. (lambda (x) (fn (maybe-num x))))))
  791.  
  792. (define logoize-2
  793. (let ((maybe-num maybe-num))
  794. (lambda (fn)
  795. (lambda (x y) (fn (maybe-num x) (maybe-num y))))))
  796.  
  797. (define strings-are-numbers
  798. (let ((are-they? #f)
  799. (real-* *) (real-+ +) (real-- -) (real-/ /) (real-< <)
  800. (real-<= <=) (real-= =) (real-> >) (real->= >=) (real-abs abs)
  801. (real-acos acos) (real-asin asin) (real-atan atan)
  802. (real-ceiling ceiling) (real-cos cos) (real-even? even?)
  803. (real-exp exp) (real-expt expt) (real-floor floor) (real-align align)
  804. (real-gcd gcd) (real-integer? integer?) (real-item item)
  805. (real-lcm lcm) (real-list-ref list-ref) (real-log log)
  806. (real-make-vector make-vector) (real-max max) (real-min min)
  807. (real-modulo modulo) (real-negative? negative?)
  808. (real-number? number?) (real-odd? odd?) (real-positive? positive?)
  809. (real-quotient quotient) (real-random random) (real-remainder remainder)
  810. (real-repeated repeated) (real-round round) (real-sin sin)
  811. (real-sqrt sqrt) (real-tan tan) (real-truncate truncate)
  812. (real-vector-ref vector-ref) (real-vector-set! vector-set!)
  813. (real-zero? zero?) (maybe-num maybe-num) (number->string number->string)
  814. (cons cons) (car car) (cdr cdr) (eq? eq?) (show show) (logoize logoize)
  815. (logoize-1 logoize-1) (logoize-2 logoize-2) (not not) (whoops whoops))
  816.  
  817. (lambda (yesno)
  818. (cond ((and are-they? (eq? yesno #t))
  819. (show "Strings are already numbers"))
  820. ((eq? yesno #t)
  821. (set! are-they? #t)
  822. (set! * (logoize real-*))
  823. (set! + (logoize real-+))
  824. (set! - (logoize real--))
  825. (set! / (logoize real-/))
  826. (set! < (logoize real-<))
  827. (set! <= (logoize real-<=))
  828. (set! = (logoize real-=))
  829. (set! > (logoize real->))
  830. (set! >= (logoize real->=))
  831. (set! abs (logoize-1 real-abs))
  832. (set! acos (logoize-1 real-acos))
  833. (set! asin (logoize-1 real-asin))
  834. (set! atan (logoize real-atan))
  835. (set! ceiling (logoize-1 real-ceiling))
  836. (set! cos (logoize-1 real-cos))
  837. (set! even? (logoize-1 real-even?))
  838. (set! exp (logoize-1 real-exp))
  839. (set! expt (logoize-2 real-expt))
  840. (set! floor (logoize-1 real-floor))
  841. (set! align (logoize align))
  842. (set! gcd (logoize real-gcd))
  843. (set! integer? (logoize-1 real-integer?))
  844. (set! item (lambda (n stuff)
  845. (real-item (maybe-num n) stuff)))
  846. (set! lcm (logoize real-lcm))
  847. (set! list-ref (lambda (lst k)
  848. (real-list-ref lst (maybe-num k))))
  849. (set! log (logoize-1 real-log))
  850. (set! max (logoize real-max))
  851. (set! min (logoize real-min))
  852. (set! modulo (logoize-2 real-modulo))
  853. (set! negative? (logoize-1 real-negative?))
  854. (set! number? (logoize-1 real-number?))
  855. (set! odd? (logoize-1 real-odd?))
  856. (set! positive? (logoize-1 real-positive?))
  857. (set! quotient (logoize-2 real-quotient))
  858. (set! random (logoize real-random))
  859. (set! remainder (logoize-2 real-remainder))
  860. (set! round (logoize-1 real-round))
  861. (set! sin (logoize-1 real-sin))
  862. (set! sqrt (logoize-1 real-sqrt))
  863.  
  864. (set! tan (logoize-1 real-tan))
  865. (set! truncate (logoize-1 real-truncate))
  866. (set! zero? (logoize-1 real-zero?))
  867. (set! vector-ref
  868. (lambda (vec i) (real-vector-ref vec (maybe-num i))))
  869. (set! vector-set!
  870. (lambda (vec i val)
  871. (real-vector-set! vec (maybe-num i) val)))
  872. (set! make-vector
  873. (lambda (num . args)
  874. (apply real-make-vector (cons (maybe-num num)
  875. args))))
  876. (set! list-ref
  877. (lambda (lst i) (real-list-ref lst (maybe-num i))))
  878. (set! repeated
  879. (lambda (fn n) (real-repeated fn (maybe-num n)))))
  880. ((and (not are-they?) (not yesno))
  881. (show "Strings are already not numbers"))
  882. ((not yesno)
  883. (set! are-they? #f) (set! * real-*) (set! + real-+)
  884. (set! - real--) (set! / real-/) (set! < real-<)
  885. (set! <= real-<=) (set! = real-=) (set! > real->)
  886. (set! >= real->=) (set! abs real-abs) (set! acos real-acos)
  887. (set! asin real-asin) (set! atan real-atan)
  888. (set! ceiling real-ceiling) (set! cos real-cos)
  889. (set! even? real-even?)
  890. (set! exp real-exp) (set! expt real-expt)
  891. (set! floor real-floor) (set! align real-align)
  892. (set! gcd real-gcd) (set! integer? real-integer?)
  893. (set! item real-item)
  894. (set! lcm real-lcm) (set! list-ref real-list-ref)
  895. (set! log real-log) (set! max real-max) (set! min real-min)
  896. (set! modulo real-modulo) (set! odd? real-odd?)
  897. (set! quotient real-quotient) (set! random real-random)
  898. (set! remainder real-remainder) (set! round real-round)
  899. (set! sin real-sin) (set! sqrt real-sqrt) (set! tan real-tan)
  900. (set! truncate real-truncate) (set! zero? real-zero?)
  901. (set! positive? real-positive?) (set! negative? real-negative?)
  902. (set! number? real-number?) (set! vector-ref real-vector-ref)
  903. (set! vector-set! real-vector-set!)
  904. (set! make-vector real-make-vector)
  905. (set! list-ref real-list-ref) (set! item real-item)
  906. (set! repeated real-repeated))
  907. (else (whoops "Strings-are-numbers: give a #t or a #f")))
  908. are-they?)))
  909.  
  910.  
  911. ;; By default, strings are numbers:
  912. (strings-are-numbers #t)
  913.  
  914. (display (word '4 '5))
Success #stdin #stdout 0.04s 12440KB
stdin
Standard input is empty
stdout
45