From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?utf-8?q?mayuresh=40kathe=2Ein?= Newsgroups: gmane.lisp.guile.user Subject: =?utf-8?q?Fwd=3A?==?utf-8?q?_Re=3A?= Where can I find the environment files required to work with =?utf-8?q?=22Simply?==?utf-8?q?_Scheme=22=3F?= Date: Fri, 07 Aug 2020 12:23:39 +0200 Message-ID: <3399-5f2d2b80-13-7d625e8@109301890> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_=-_OpenGroupware_org_NGMime-13209-1596795819.130932-4------" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="9900"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: SOGoMail 4.3.2 To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Fri Aug 07 12:24:04 2020 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k3zXf-0002Qx-HZ for guile-user@m.gmane-mx.org; Fri, 07 Aug 2020 12:24:03 +0200 Original-Received: from localhost ([::1]:50504 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k3zXe-0004bn-CM for guile-user@m.gmane-mx.org; Fri, 07 Aug 2020 06:24:02 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42538) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k3zXR-0004bI-Sw for guile-user@gnu.org; Fri, 07 Aug 2020 06:23:49 -0400 Original-Received: from relay4-d.mail.gandi.net ([217.70.183.196]:40937) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k3zXM-0000WD-1b for guile-user@gnu.org; Fri, 07 Aug 2020 06:23:49 -0400 Original-Received: from sogo9.sd4.0x35.net (sogo9.sd4.0x35.net [10.200.201.59]) (Authenticated sender: mayuresh@kathe.in) by relay4-d.mail.gandi.net (Postfix) with ESMTPA id 8F9E9E000B for ; Fri, 7 Aug 2020 10:23:39 +0000 (UTC) X-Forward: 127.0.0.1 Received-SPF: pass client-ip=217.70.183.196; envelope-from=mayuresh@kathe.in; helo=relay4-d.mail.gandi.net X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/07 06:23:39 X-ACL-Warn: Detected OS = Linux 3.11 and newer X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:16785 Archived-At: ------=_=-_OpenGroupware_org_NGMime-13209-1596795819.130932-4------ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Length: 34035 Is there anyone on this list with the expertise to adapt the attached (= simply.scm) file to Guile? I cannot do it myself, and I do not wish to take credit for anyone's wo= rk, but if someone could volunteer to make the attached file work with = Guile it would be a great service to the Scheme community around Guile = as there would be an excellent resource in the form of that book (Simpl= y Scheme) to start learning the basics from. Do consider. I have also included the contents of that file below; ;;; simply.scm version 3.13 (8/11/98) ;;; This file uses Scheme features we don't talk about in =5FSimply=5FS= cheme=5F. ;;; Read at your own risk. (if (equal? 'foo (symbol->string 'foo)) (error "Simply.scm already loaded!!") #f) ;; Make number->string remove leading "+" if necessary (if (char=3D? #\+ (string-ref (number->string 1.0) 0)) (let ((old-ns number->string) (char=3D? char=3D?) (string-ref strin= g-ref) (substring substring) (string-length string-length)) (set! number->string (lambda args (let ((result (apply old-ns args))) (if (char=3D? #\+ (string-ref result 0)) (substring result 1 (string-length result)) result))))) 'no-problem) (define number->string (let ((old-ns number->string) (string? string?)) (lambda args (if (string? (car args)) (car args) (apply old-ns args))))) ;; Get strings in error messages to print nicely (especially "") (define whoops (let ((string? string?) (string-append string-append) (error error) (cons cons) (map map) (apply apply)) (define (error-printform x) (if (string? x) (string-append "\"" x "\"") x)) (lambda (string . args) (apply error (cons string (map error-printform args)))))) ;; ROUND returns an inexact integer if its argument is inexact, ;; but we think it should always return an exact integer. ;; (It matters because some Schemes print inexact integers as "+1.0".) ;; The (exact 1) test is for PC Scheme, in which nothing is exact. (if (and (inexact? (round (sqrt 2))) (exact? 1)) (let ((old-round round) (inexact->exact inexact->exact)) (set! round (lambda (number) (inexact->exact (old-round number))))) 'no-problem) ;; Remainder and quotient blow up if their argument isn't an integer. ;; Unfortunately, in SCM, (* 365.25 24 60 60) *isn't* an integer. (if (inexact? (* .25 4)) (let ((rem remainder) (quo quotient) (inexact->exact inexact->exact= ) (integer? integer?)) (set! remainder (lambda (x y) (rem (if (integer? x) (inexact->exact x) x) (if (integer? y) (inexact->exact y) y)))) (set! quotient (lambda (x y) (quo (if (integer? x) (inexact->exact x) x) (if (integer? y) (inexact->exact y) y))))) 'done) ;; Random ;; If your version of Scheme has RANDOM, you should take this out. ;; (It gives the same sequence of random numbers every time.) (define random (let ((*seed* 1) (quotient quotient) (modulo modulo) (+ +) (- -) (* *= ) (> >)) (lambda (x) (let* ((hi (quotient *seed* 127773)) (low (modulo *seed* 127773)) (test (- (* 16807 low) (* 2836 hi)))) (if (> test 0) (set! *seed* test) (set! *seed* (+ test 2147483647)))) (modulo *seed* x)))) ;;; Logo-style word/sentence implementation (define word? (let ((number? number?) (symbol? symbol?) (string? string?)) (lambda (x) (or (symbol? x) (number? x) (string? x))))) (define sentence? (let ((null? null?) (pair? pair?) (word? word?) (car car) (cdr cdr)) (define (list-of-words? l) (cond ((null? l) #t) ((pair? l) (and (word? (car l)) (list-of-words? (cdr l)))) (else #f))) list-of-words?)) (define empty? (let ((null? null?) (string? string?) (string=3D? string=3D?)) (lambda (x) (or (null? x) (and (string? x) (string=3D? x "")))))) (define char-rank ;; 0 Letter in good case or special initial ;; 1 ., + or - ;; 2 Digit ;; 3 Letter in bad case or weird character (let ((*the-char-ranks* (make-vector 256 3)) (=3D =3D) (+ +) (string-ref string-ref) (string-length string-length) (vector-set! vector-set!) (char->integer char->integer) (symbol->string symbol->string) (vector-ref vector-ref)) (define (rank-string str rank) (define (helper i len) (if (=3D i len) 'done (begin (vector-set! *the-char-ranks* (char->integer (string-ref str i)) rank) (helper (+ i 1) len)))) (helper 0 (string-length str))) (rank-string (symbol->string 'abcdefghijklmnopqrstuvwxyz) 0) (rank-string "!$%&*/:<=3D>?~=5F^" 0) (rank-string "+-." 1) (rank-string "0123456789" 2) (lambda (char) ;; value of char-rank (vector-ref *the-char-ranks* (char->integer char))))) (define string->word (let ((=3D =3D) (<=3D <=3D) (+ +) (- -) (char-rank char-rank) (string= -ref string-ref) (string-length string-length) (string=3D? string=3D?) (not not) (char=3D? char=3D?) (string->number string->number) (string->symbol string->symbol)) (lambda (string) (define (subsequents? string i length) (cond ((=3D i length) #t) ((<=3D (char-rank (string-ref string i)) 2) (subsequents? string (+ i 1) length)) (else #f))) (define (special-id? string) (or (string=3D? string "+") (string=3D? string "-") (string=3D? string "..."))) (define (ok-symbol? string) (if (string=3D? string "") #f (let ((rank1 (char-rank (string-ref string 0)))) (cond ((=3D rank1 0) (subsequents? string 1 (string-length strin= g))) ((=3D rank1 1) (special-id? string)) (else #f))))) (define (nn-helper string i len seen-point?) (cond ((=3D i len) (if seen-point? (not (char=3D? (string-ref string (- len 1)) #\0)) #t)) ((char=3D? #\. (string-ref string i)) (cond (seen-point? #f) ((=3D (+ i 2) len) #t) ; Accepts "23.0" (else (nn-helper string (+ i 1) len #t)))) ((=3D 2 (char-rank (string-ref string i))) (nn-helper string (+ i 1) len seen-point?)) (else #f))) (define (narrow-number? string) (if (string=3D? string "") #f (let* ((c0 (string-ref string 0)) (start 0) (len (string-length string)) (cn (string-ref string (- len 1)))) (if (and (char=3D? c0 #\-) (not (=3D len 1))) (begin (set! start 1) (set! c0 (string-ref string 1))) #f) (cond ((not (=3D (char-rank cn) 2)) #f) ; Rejects "-" among oth= ers ((char=3D? c0 #\.) #f) ((char=3D? c0 #\0) (cond ((=3D len 1) #t) ; Accepts "0" but not "-0" ((=3D len 2) #f) ; Rejects "-0" and "03" ((char=3D? (string-ref string (+ start 1)) #\.) (nn-helper string (+ start 2) len #t)) (else #f))) (else (nn-helper string start len #f)))))) ;; The body of string->word: (cond ((narrow-number? string) (string->number string)) ((ok-symbol? string) (string->symbol string)) (else string))))) (define char->word (let ((=3D =3D) (char-rank char-rank) (make-string make-string) (char= =3D? char=3D?) (string->symbol string->symbol) (string->number string->number)= ) (lambda (char) (let ((rank (char-rank char)) (string (make-string 1 char))) (cond ((=3D rank 0) (string->symbol string)) ((=3D rank 2) (string->number string)) ((char=3D? char #\+) '+) ((char=3D? char #\-) '-) (else string)))))) (define word->string (let ((number? number?) (string? string?) (number->string number->str= ing) (symbol->string symbol->string)) (lambda (wd) (cond ((string? wd) wd) ((number? wd) (number->string wd)) (else (symbol->string wd)))))) (define count (let ((word? word?) (string-length string-length) (word->string word->string) (length length)) (lambda (stuff) (if (word? stuff) (string-length (word->string stuff)) (length stuff))))) (define word (let ((string->word string->word) (apply apply) (string-append string= -append) (map map) (word? word?) (word->string word->string) (whoops whoops)) (lambda x (string->word (apply string-append (map (lambda (arg) (if (word? arg) (word->string arg) (whoops "Invalid argument to WORD: " arg))) x)))))) (define se (let ((pair? pair?) (null? null?) (word? word?) (car car) (cons cons)= (cdr cdr) (whoops whoops)) (define (paranoid-append a original-a b) (cond ((null? a) b) ((word? (car a)) (cons (car a) (paranoid-append (cdr a) original-a b))) (else (whoops "Argument to SENTENCE not a word or sentence" original-a )))) (define (combine-two a b) ;; Note: b is always a lis= t (cond ((pair? a) (paranoid-append a a b)) ((null? a) b) ((word? a) (cons a b)) (else (whoops "Argument to SENTENCE not a word or sentence:" a))))= ;; Helper function so recursive calls don't show up in TRACE (define (real-se args) (if (null? args) '() (combine-two (car args) (real-se (cdr args))))) (lambda args (real-se args)))) (define sentence se) (define first (let ((pair? pair?) (char->word char->word) (string-ref string-ref) (word->string word->string) (car car) (empty? empty?) (whoops whoops) (word? word?)) (define (word-first wd) (char->word (string-ref (word->string wd) 0))) (lambda (x) (cond ((pair? x) (car x)) ((empty? x) (whoops "Invalid argument to FIRST: " x)) ((word? x) (word-first x)) (else (whoops "Invalid argument to FIRST: " x)))))) (define last (let ((pair? pair?) (- -) (word->string word->string) (char->word cha= r->word) (string-ref string-ref) (string-length string-length) (empty? empty?) (cdr cdr) (car car) (whoops whoops) (word? word?)) (define (word-last wd) (let ((s (word->string wd))) (char->word (string-ref s (- (string-length s) 1))))) (define (list-last lst) (if (empty? (cdr lst)) (car lst) (list-last (cdr lst)))) (lambda (x) (cond ((pair? x) (list-last x)) ((empty? x) (whoops "Invalid argument to LAST: " x)) ((word? x) (word-last x)) (else (whoops "Invalid argument to LAST: " x)))))) (define bf (let ((pair? pair?) (substring substring) (string-length string-lengt= h) (string->word string->word) (word->string word->string) (cdr cdr) (empty? empty?) (whoops whoops) (word? word?)) (define string-bf (lambda (s) (substring s 1 (string-length s)))) (define (word-bf wd) (string->word (string-bf (word->string wd)))) (lambda (x) (cond ((pair? x) (cdr x)) ((empty? x) (whoops "Invalid argument to BUTFIRST: " x)) ((word? x) (word-bf x)) (else (whoops "Invalid argument to BUTFIRST: " x)))))) (define butfirst bf) (define bl (let ((pair? pair?) (- -) (cdr cdr) (cons cons) (car car) (substring = substring) (string-length string-length) (string->word string->word) (word->string word->string) (empty? empty?) (whoops whoops) (word? wor= d?)) (define (list-bl list) (if (null? (cdr list)) '() (cons (car list) (list-bl (cdr list))))) (define (string-bl s) (substring s 0 (- (string-length s) 1))) (define (word-bl wd) (string->word (string-bl (word->string wd)))) (lambda (x) (cond ((pair? x) (list-bl x)) ((empty? x) (whoops "Invalid argument to BUTLAST: " x)) ((word? x) (word-bl x)) (else (whoops "Invalid argument to BUTLAST: " x)))))) (define butlast bl) (define item (let ((> >) (- -) (< <) (integer? integer?) (list-ref list-ref) (char->word char->word) (string-ref string-ref) (word->string word->string) (not not) (whoops whoops) (count count) (word? word?) (list? list?)) (define (word-item n wd) (char->word (string-ref (word->string wd) (- n 1)))) (lambda (n stuff) (cond ((not (integer? n)) (whoops "Invalid first argument to ITEM (must be an integer): " n)) ((< n 1) (whoops "Invalid first argument to ITEM (must be positive): " n)) ((> n (count stuff)) (whoops "No such item: " n stuff)) ((word? stuff) (word-item n stuff)) ((list? stuff) (list-ref stuff (- n 1))) (else (whoops "Invalid second argument to ITEM: " stuff)))))) (define equal? ;; Note that EQUAL? assumes strings are numbers. ;; (strings-are-numbers #f) doesn't change this behavior. (let ((vector-length vector-length) (=3D =3D) (vector-ref vector-ref)= (+ +) (string? string?) (symbol? symbol?) (null? null?) (pair? pair?) (car car) (cdr cdr) (eq? eq?) (string=3D? string=3D?) (symbol->string symbol->string) (number? number?) (string->word string->word) (vector? vector?) (eqv? eqv?)) (define (vector-equal? v1 v2) (let ((len1 (vector-length v1)) (len2 (vector-length v2))) (define (helper i) (if (=3D i len1) #t (and (equal? (vector-ref v1 i) (vector-ref v2 i)) (helper (+ i 1))))) (if (=3D len1 len2) (helper 0) #f))) (lambda (x y) (cond ((null? x) (null? y)) ((null? y) #f) ((pair? x) (and (pair? y) (equal? (car x) (car y)) (equal? (cdr x) (cdr y)))) ((pair? y) #f) ((symbol? x) (or (and (symbol? y) (eq? x y)) (and (string? y) (string=3D? (symbol->string x) y)))) ((symbol? y) (and (string? x) (string=3D? x (symbol->string y)))) ((number? x) (or (and (number? y) (=3D x y)) (and (string? y) (let ((possible-num (string->word y))) (and (number? possible-num) (=3D x possible-num)))))) ((number? y) (and (string? x) (let ((possible-num (string->word x))) (and (number? possible-num) (=3D possible-num y))))) ((string? x) (and (string? y) (string=3D? x y))) ((string? y) #f) ((vector? x) (and (vector? y) (vector-equal? x y))) ((vector? y) #f) (else (eqv? x y)))))) (define member? (let ((> >) (- -) (< <) (null? null?) (symbol? symbol?) (eq? eq?) (ca= r car) (not not) (symbol->string symbol->string) (string=3D? string=3D?) (cdr cdr) (equal? equal?) (word->string word->string) (string-length string-length) (whoops whoops) (string-ref string-ref) (char=3D? char=3D?) (list? list?) (number? number?) (empty? empty?) (word? word?) (string? string?)) (define (symbol-in-list? symbol string lst) (cond ((null? lst) #f) ((and (symbol? (car lst)) (eq? symbol (car lst)))) ((string? (car lst)) (cond ((not string) (symbol-in-list? symbol (symbol->string symbol) lst)) ((string=3D? string (car lst)) #t) (else (symbol-in-list? symbol string (cdr lst))))) (else (symbol-in-list? symbol string (cdr lst))))) (define (word-in-list? wd lst) (cond ((null? lst) #f) ((equal? wd (car lst)) #t) (else (word-in-list? wd (cdr lst))))) (define (word-in-word? small big) (let ((one-letter-str (word->string small))) (if (> (string-length one-letter-str) 1) (whoops "Invalid arguments to MEMBER?: " small big) (let ((big-str (word->string big))) (char-in-string? (string-ref one-letter-str 0) big-str (- (string-length big-str) 1)))))) (define (char-in-string? char string i) (cond ((< i 0) #f) ((char=3D? char (string-ref string i)) #t) (else (char-in-string? char string (- i 1))))) (lambda (x stuff) (cond ((empty? stuff) #f) ((word? stuff) (word-in-word? x stuff)) ((not (list? stuff)) (whoops "Invalid second argument to MEMBER?: " stuff)) ((symbol? x) (symbol-in-list? x #f stuff)) ((or (number? x) (string? x)) (word-in-list? x stuff)) (else (whoops "Invalid first argument to MEMBER?: " x)))))) (define before? (let ((not not) (word? word?) (whoops whoops) (stringstring word->string)) (lambda (wd1 wd2) (cond ((not (word? wd1)) (whoops "Invalid first argument to BEFORE? (not a word): " wd1)) ((not (word? wd2)) (whoops "Invalid second argument to BEFORE? (not a word): " wd2))= (else (stringstring wd1) (word->string wd2))))))) ;;; Higher Order Functions (define filter (let ((null? null?) (car car) (cons cons) (cdr cdr) (not not) (procedure? procedure?) (whoops whoops) (list? list?)) (lambda (pred l) ;; Helper function so recursive calls don't show up in TRACE (define (real-filter l) (cond ((null? l) '()) ((pred (car l)) (cons (car l) (real-filter (cdr l)))) (else (real-filter (cdr l))))) (cond ((not (procedure? pred)) (whoops "Invalid first argument to FILTER (not a procedure): " pred)) ((not (list? l)) (whoops "Invalid second argument to FILTER (not a list): " l)) (else (real-filter l)))))) (define keep (let ((+ +) (=3D =3D) (pair? pair?) (substring substring) (char->word char->word) (string-ref string-ref) (string-set! string-set!) (word->string word->string) (string-length string-length) (string->word string->word) (make-string make-string) (procedure? procedure?) (whoops whoops) (word? word?) (null? null?)) (lambda (pred w-or-s) (define (keep-string in i out out-len len) (cond ((=3D i len) (substring out 0 out-len)) ((pred (char->word (string-ref in i))) (string-set! out out-len (string-ref in i)) (keep-string in (+ i 1) out (+ out-len 1) len)) (else (keep-string in (+ i 1) out out-len len)))) (define (keep-word wd) (let* ((string (word->string wd)) (len (string-length string))) (string->word (keep-string string 0 (make-string len) 0 len)))) (cond ((not (procedure? pred)) (whoops "Invalid first argument to KEEP (not a procedure): " pred)) ((pair? w-or-s) (filter pred w-or-s)) ((word? w-or-s) (keep-word w-or-s)) ((null? w-or-s) '()) (else (whoops "Bad second argument to KEEP (not a word or sentence): " w-or-s)))))) (define appearances (let ((count count) (keep keep) (equal? equal?)) (lambda (item aggregate) (count (keep (lambda (element) (equal? item element)) aggregate))= ))) (define every (let ((=3D =3D) (+ +) (se se) (char->word char->word) (string-ref str= ing-ref) (empty? empty?) (first first) (bf bf) (not not) (procedure? procedure?= ) (whoops whoops) (word? word?) (word->string word->string) (string-length string-length)) (lambda (fn stuff) (define (string-every string i length) (if (=3D i length) '() (se (fn (char->word (string-ref string i))) (string-every string (+ i 1) length)))) (define (sent-every sent) ;; This proc. can't be optimized or else it will break the ;; exercise where we ask them to reimplement sentences as ;; vectors and then see if every still works. (if (empty? sent) sent ; Can't be '() or exercise breaks. (se (fn (first sent)) (sent-every (bf sent))))) (cond ((not (procedure? fn)) (whoops "Invalid first argument to EVERY (not a procedure):" fn)) ((word? stuff) (let ((string (word->string stuff))) (string-every string 0 (string-length string)))) (else (sent-every stuff)))))) (define accumulate (let ((not not) (empty? empty?) (bf bf) (first first) (procedure? pro= cedure?) (whoops whoops) (member member) (list list)) (lambda (combiner stuff) (define (real-accumulate stuff) (if (empty? (bf stuff)) (first stuff) (combiner (first stuff) (real-accumulate (bf stuff))))) (cond ((not (procedure? combiner)) (whoops "Invalid first argument to ACCUMULATE (not a procedure):"= combiner)) ((not (empty? stuff)) (real-accumulate stuff)) ((member combiner (list + * word se)) (combiner)) (else (whoops "Can't accumulate empty input with that combiner")))))) (define reduce (let ((null? null?) (cdr cdr) (car car) (not not) (procedure? procedu= re?) (whoops whoops) (member member) (list list)) (lambda (combiner stuff) (define (real-reduce stuff) (if (null? (cdr stuff)) (car stuff) (combiner (car stuff) (real-reduce (cdr stuff))))) (cond ((not (procedure? combiner)) (whoops "Invalid first argument to REDUCE (not a procedure):" combiner)) ((not (null? stuff)) (real-reduce stuff)) ((member combiner (list + * word se append)) (combiner)) (else (whoops "Can't reduce empty input with that combiner")))))) (define repeated (let ((=3D =3D) (- -)) (lambda (fn number) (if (=3D number 0) (lambda (x) x) (lambda (x) ((repeated fn (- number 1)) (fn x))))))) ;; Tree stuff (define make-node cons) (define datum car) (define children cdr) ;; I/O (define show (let ((=3D =3D) (length length) (display display) (car car) (newline = newline) (not not) (output-port? output-port?) (apply apply) (whoops whoops)) (lambda args (cond ((=3D (length args) 1) (display (car args)) (newline)) ((=3D (length args) 2) (if (not (output-port? (car (cdr args)))) (whoops "Invalid second argument to SHOW (not an output port): " (car (cdr args)))) (apply display args) (newline (car (cdr args)))) (else (whoops "Incorrect number of arguments to procedure SHOW")= ))))) (define show-line (let ((>=3D >=3D) (length length) (whoops whoops) (null? null?) (current-output-port current-output-port) (car car) (not not) (list? list?) (display display) (for-each for-each) (cdr cdr) (newline newline)) (lambda (line . args) (if (>=3D (length args) 2) (whoops "Too many arguments to show-line") (let ((port (if (null? args) (current-output-port) (car args)))) (cond ((not (list? line)) (whoops "Invalid argument to SHOW-LINE (not a list):" line)) ((null? line) #f) (else (display (car line) port) (for-each (lambda (wd) (display " " port) (display wd port)) (cdr line)))) (newline port)))))) (define read-string (let ((read-char read-char) (eqv? eqv?) (apply apply) (string-append string-append) (substring substring) (reverse reverse) (cons cons) (>=3D >=3D) (+ +) (string-set! string-set!) (length length= ) (whoops whoops) (null? null?) (current-input-port current-input-port) (car car) (cdr cdr) (eof-object? eof-object?) (list list) (make-string make-string) (peek-char peek-char)) (define (read-string-helper chars all-length chunk-length port) (let ((char (read-char port)) (string (car chars))) (cond ((or (eof-object? char) (eqv? char #\newline)) (apply string-append (reverse (cons (substring (car chars) 0 chunk-length) (cdr chars))))) ((>=3D chunk-length 80) (let ((newstring (make-string 80))) (string-set! newstring 0 char) (read-string-helper (cons newstring chars) (+ all-length 1) 1 port))) (else (string-set! string chunk-length char) (read-string-helper chars (+ all-length 1) (+ chunk-length 1) port))))) (lambda args (if (>=3D (length args) 2) (whoops "Too many arguments to read-string") (let ((port (if (null? args) (current-input-port) (car args)))) (if (eof-object? (peek-char port)) (read-char port) (read-string-helper (list (make-string 80)) 0 0 port))))))) (define read-line (let ((=3D =3D) (list list) (string->word string->word) (substring su= bstring) (char-whitespace? char-whitespace?) (string-ref string-ref) (+ +) (string-length string-length) (apply apply) (read-string read-string)) (lambda args (define (tokenize string) (define (helper i start len) (cond ((=3D i len) (if (=3D i start) '() (list (string->word (substring string start i))))) ((char-whitespace? (string-ref string i)) (if (=3D i start) (helper (+ i 1) (+ i 1) len) (cons (string->word (substring string start i)) (helper (+ i 1) (+ i 1) len)))) (else (helper (+ i 1) start len)))) (if (eof-object? string) string (helper 0 0 (string-length string)))) (tokenize (apply read-string args))))) (define *the-open-inports* '()) (define *the-open-outports* '()) (define align (let ((< <) (abs abs) (* *) (expt expt) (>=3D >=3D) (- -) (+ +) (=3D = =3D) (null? null?) (car car) (round round) (number->string number->string) (string-length string-length) (string-append string-append) (make-string make-string) (substring substring) (string-set! string-set!) (number? number?) (word->string word->string)) (lambda (obj width . rest) (define (align-number obj width rest) (let* ((sign (< obj 0)) (num (abs obj)) (prec (if (null? rest) 0 (car rest))) (big (round (* num (expt 10 prec)))) (cvt0 (number->string big)) (cvt (if (< num 1) (string-append "0" cvt0) cvt0)) (pos-str (if (>=3D (string-length cvt0) prec) cvt (string-append (make-string (- prec (string-length cvt0)) #\0) cvt))) (string (if sign (string-append "-" pos-str) pos-str)) (length (+ (string-length string) (if (=3D prec 0) 0 1))) (left (- length (+ 1 prec))) (result (if (=3D prec 0) string (string-append (substring string 0 left) "." (substring string left (- length 1)))))) (cond ((=3D length width) result) ((< length width) (string-append (make-string (- width length) #\space) result)) (else (let ((new (substring result 0 width))) (string-set! new (- width 1) #\+) new))))) (define (align-word string) (let ((length (string-length string))) (cond ((=3D length width) string) ((< length width) (string-append string (make-string (- width length) #\space))) (else (let ((new (substring string 0 width))) (string-set! new (- width 1) #\+) new))))) (if (number? obj) (align-number obj width rest) (align-word (word->string obj)))))) (define open-output-file (let ((oof open-output-file) (cons cons)) (lambda (filename) (let ((port (oof filename))) (set! *the-open-outports* (cons port *the-open-outports*)) port)))) (define open-input-file (let ((oif open-input-file) (cons cons)) (lambda (filename) (let ((port (oif filename))) (set! *the-open-inports* (cons port *the-open-inports*)) port)))) (define remove! (let ((null? null?) (cdr cdr) (eq? eq?) (set-cdr! set-cdr!) (car car)= ) (lambda (thing lst) (define (r! prev) (cond ((null? (cdr prev)) lst) ((eq? thing (car (cdr prev))) (set-cdr! prev (cdr (cdr prev))) lst) (else (r! (cdr prev))))) (cond ((null? lst) lst) ((eq? thing (car lst)) (cdr lst)) (else (r! lst)))))) (define close-input-port (let ((cip close-input-port) (remove! remove!)) (lambda (port) (set! *the-open-inports* (remove! port *the-open-inports*)) (cip port)))) (define close-output-port (let ((cop close-output-port) (remove! remove!)) (lambda (port) (set! *the-open-outports* (remove! port *the-open-outports*)) (cop port)))) (define close-all-ports (let ((for-each for-each) (close-input-port close-input-port) (close-output-port close-output-port)) (lambda () (for-each close-input-port *the-open-inports*) (for-each close-output-port *the-open-outports*) 'closed))) ;; Make arithmetic work on numbers in string form: (define maybe-num (let ((string? string?) (string->number string->number)) (lambda (arg) (if (string? arg) (let ((num (string->number arg))) (if num num arg)) arg)))) (define logoize (let ((apply apply) (map map) (maybe-num maybe-num)) (lambda (fn) (lambda args (apply fn (map maybe-num args)))))) ;; special case versions of logoize, since (lambda args ...) is expensi= ve (define logoize-1 (let ((maybe-num maybe-num)) (lambda (fn) (lambda (x) (fn (maybe-num x)))))) (define logoize-2 (let ((maybe-num maybe-num)) (lambda (fn) (lambda (x y) (fn (maybe-num x) (maybe-num y)))))) (define strings-are-numbers (let ((are-they? #f) (real-* *) (real-+ +) (real-- -) (real-/ /) (real-< <) (real-<=3D <=3D) (real-=3D =3D) (real-> >) (real->=3D >=3D) (re= al-abs abs) (real-acos acos) (real-asin asin) (real-atan atan) (real-ceiling ceiling) (real-cos cos) (real-even? even?) (real-exp exp) (real-expt expt) (real-floor floor) (real-align = align) (real-gcd gcd) (real-integer? integer?) (real-item item) (real-lcm lcm) (real-list-ref list-ref) (real-log log) (real-make-vector make-vector) (real-max max) (real-min min) (real-modulo modulo) (real-negative? negative?) (real-number? number?) (real-odd? odd?) (real-positive? positiv= e?) (real-quotient quotient) (real-random random) (real-remainder r= emainder) (real-repeated repeated) (real-round round) (real-sin sin) (real-sqrt sqrt) (real-tan tan) (real-truncate truncate) (real-vector-ref vector-ref) (real-vector-set! vector-set!) (real-zero? zero?) (maybe-num maybe-num) (number->string number= ->string) (cons cons) (car car) (cdr cdr) (eq? eq?) (show show) (logoize logoize= ) (logoize-1 logoize-1) (logoize-2 logoize-2) (not not) (whoops whoops))= (lambda (yesno) (cond ((and are-they? (eq? yesno #t)) (show "Strings are already numbers")) ((eq? yesno #t) (set! are-they? #t) (set! * (logoize real-*)) (set! + (logoize real-+)) (set! - (logoize real--)) (set! / (logoize real-/)) (set! < (logoize real-<)) (set! <=3D (logoize real-<=3D)) (set! =3D (logoize real-=3D)) (set! > (logoize real->)) (set! >=3D (logoize real->=3D)) (set! abs (logoize-1 real-abs)) (set! acos (logoize-1 real-acos)) (set! asin (logoize-1 real-asin)) (set! atan (logoize real-atan)) (set! ceiling (logoize-1 real-ceiling)) (set! cos (logoize-1 real-cos)) (set! even? (logoize-1 real-even?)) (set! exp (logoize-1 real-exp)) (set! expt (logoize-2 real-expt)) (set! floor (logoize-1 real-floor)) (set! align (logoize align)) (set! gcd (logoize real-gcd)) (set! integer? (logoize-1 real-integer?)) (set! item (lambda (n stuff) (real-item (maybe-num n) stuff))) (set! lcm (logoize real-lcm)) (set! list-ref (lambda (lst k) (real-list-ref lst (maybe-num k)))) (set! log (logoize-1 real-log)) (set! max (logoize real-max)) (set! min (logoize real-min)) (set! modulo (logoize-2 real-modulo)) (set! negative? (logoize-1 real-negative?)) (set! number? (logoize-1 real-number?)) (set! odd? (logoize-1 real-odd?)) (set! positive? (logoize-1 real-positive?)) (set! quotient (logoize-2 real-quotient)) (set! random (logoize real-random)) (set! remainder (logoize-2 real-remainder)) (set! round (logoize-1 real-round)) (set! sin (logoize-1 real-sin)) (set! sqrt (logoize-1 real-sqrt)) (set! tan (logoize-1 real-tan)) (set! truncate (logoize-1 real-truncate)) (set! zero? (logoize-1 real-zero?)) (set! vector-ref (lambda (vec i) (real-vector-ref vec (maybe-num i)))) (set! vector-set! (lambda (vec i val) (real-vector-set! vec (maybe-num i) val))) (set! make-vector (lambda (num . args) (apply real-make-vector (cons (maybe-num num) args)))) (set! list-ref (lambda (lst i) (real-list-ref lst (maybe-num i)))) (set! repeated (lambda (fn n) (real-repeated fn (maybe-num n))))) ((and (not are-they?) (not yesno)) (show "Strings are already not numbers")) ((not yesno) (set! are-they? #f) (set! * real-*) (set! + real-+) (set! - real--) (set! / real-/) (set! < real-<) (set! <=3D real-<=3D) (set! =3D real-=3D) (set! > real->) (set! >=3D real->=3D) (set! abs real-abs) (set! acos real-acos) (set! asin real-asin) (set! atan real-atan) (set! ceiling real-ceiling) (set! cos real-cos) (set! even? real-even?) (set! exp real-exp) (set! expt real-expt) (set! floor real-floor) (set! align real-align) (set! gcd real-gcd) (set! integer? real-integer?) (set! item real-item) (set! lcm real-lcm) (set! list-ref real-list-ref) (set! log real-log) (set! max real-max) (set! min real-min) (set! modulo real-modulo) (set! odd? real-odd?) (set! quotient real-quotient) (set! random real-random) (set! remainder real-remainder) (set! round real-round) (set! sin real-sin) (set! sqrt real-sqrt) (set! tan real-tan) (set! truncate real-truncate) (set! zero? real-zero?) (set! positive? real-positive?) (set! negative? real-negative?) (set! number? real-number?) (set! vector-ref real-vector-ref) (set! vector-set! real-vector-set!) (set! make-vector real-make-vector) (set! list-ref real-list-ref) (set! item real-item) (set! repeated real-repeated)) (else (whoops "Strings-are-numbers: give a #t or a #f"))) are-they?))) ;; By default, strings are numbers: (strings-are-numbers #t) ;;; End of simply.scm Thank you, ~Mayuresh -------- Original Message -------- Subject: Re: Where can I find the environment files required to work wi= th "Simply Scheme"? Date: Thursday, August 06, 2020 03:32 PM IST From: Brian Harvey Organization: University of California, Berkeley To: "mayuresh@kathe.in" References: <54ce-5f2bd100-855-165d5440@133397836> The file is attached. There are other files in the book, but they should just run without any tweaking once simply.scm is loaded. On 8/6/20 2:44 AM, mayuresh@kathe.in wrote: > Hello Professor, would you please share the link to those files which= would require modification such that the whole of your book; "Simply S= cheme" can be learned using GNU Guile? > > Thank you, > > ~Mayuresh > ------=_=-_OpenGroupware_org_NGMime-13209-1596795819.130932-4------ Content-Type: text/plain Content-Disposition: attachment; filename="simply.scm" Content-Transfer-Encoding: quoted-printable Content-Length: 33018 ;;; simply.scm version 3.13 (8/11/98);;; This file uses Scheme features= we don't talk about in =5FSimply=5FScheme=5F.;;; Read at your own risk= .(if (equal? 'foo (symbol->string 'foo)) (error "Simply.scm already = loaded!!") #f);; Make number->string remove leading "+" if necessary= (if (char=3D? #\+ (string-ref (number->string 1.0) 0)) (let ((old-ns= number->string) (char=3D? char=3D?) (string-ref string-ref) (substri= ng substring) (string-length string-length)) (set! number->string = (lambda args (let ((result (apply old-ns args))) (if (char=3D= ? #\+ (string-ref result 0)) (substring result 1 (string-length re= sult)) result))))) 'no-problem)(define number->string (let ((o= ld-ns number->string) (string? string?)) (lambda args (if (stri= ng? (car args)) (car args) (apply old-ns args)))));; Get strings in= error messages to print nicely (especially "")(define whoops (let ((s= tring? string?) (string-append string-append) (error error) (cons cons)= (map map) (apply apply)) (define (error-printform x) (if (stri= ng? x) (string-append "\"" x "\"") x)) (lambda (string . args) = (apply error (cons string (map error-printform args))))));; ROUND r= eturns an inexact integer if its argument is inexact,;; but we think it= should always return an exact integer.;; (It matters because some Sche= mes print inexact integers as "+1.0".);; The (exact 1) test is for PC S= cheme, in which nothing is exact.(if (and (inexact? (round (sqrt 2))) (= exact? 1)) (let ((old-round round) (inexact->exact inexact->exact)) = (set! round (lambda (number) (inexact->exact (old-round = number))))) 'no-problem);; Remainder and quotient blow up if their a= rgument isn't an integer.;; Unfortunately, in SCM, (* 365.25 24 60 60) = *isn't* an integer.(if (inexact? (* .25 4)) (let ((rem remainder) (q= uo quotient) (inexact->exact inexact->exact) (integer? integer?)) = (set! remainder (lambda (x y) (rem (if (integer? x) (inexac= t->exact x) x) (if (integer? y) (inexact->exact y) y)))) (set!= quotient (lambda (x y) (quo (if (integer? x) (inexact->exact= x) x) (if (integer? y) (inexact->exact y) y))))) 'done);; Rando= m;; If your version of Scheme has RANDOM, you should take this out.;; (= It gives the same sequence of random numbers every time.) (define rando= m (let ((*seed* 1) (quotient quotient) (modulo modulo) (+ +) (- -) (* = *) (> >)) (lambda (x) (let* ((hi (quotient *seed* 127773)) = (low (modulo *seed* 127773)) (test (- (* 16807 low) (* 2836 hi)))= ) (if (> test 0) (set! *seed* test) (set! *seed* (+ test 214748= 3647)))) (modulo *seed* x))));;; Logo-style word/sentence implemen= tation(define word? (let ((number? number?) (symbol? symbol?) (string?= string?)) (lambda (x) (or (symbol? x) (number? x) (string? x))= )))(define sentence? (let ((null? null?) (pair? pair?) (word? word?) (= car car) (cdr cdr)) (define (list-of-words? l) (cond ((null? l)= #t) ((pair? l) (and (word? (car l)) (list-of-words? (cdr l)))= ) (else #f))) list-of-words?))(define empty? (let ((null? null?= ) (string? string?) (string=3D? string=3D?)) (lambda (x) (or (n= ull? x) (and (string? x) (string=3D? x ""))))))(define char-rank ;; = 0 Letter in good case or special initial ;; 1 ., + or - ;; 2 Digit ;= ; 3 Letter in bad case or weird character (let ((*the-char-ranks* (mak= e-vector 256 3)) (=3D =3D) (+ +) (string-ref string-ref) (string-length= string-length) (vector-set! vector-set!) (char->integer char->integer)= (symbol->string symbol->string) (vector-ref vector-ref)) (define (r= ank-string str rank) (define (helper i len) (if (=3D i len) 'd= one (begin (vector-set! *the-char-ranks* (char->integer (string-= ref str i)) rank) (helper (+ i 1) len)))) (helper 0 (string= -length str))) (rank-string (symbol->string 'abcdefghijklmnopqrstuvw= xyz) 0) (rank-string "!$%&*/:<=3D>?~=5F^" 0) (rank-string "+-." 1= ) (rank-string "0123456789" 2) (lambda (char) ;; value of ch= ar-rank (vector-ref *the-char-ranks* (char->integer char)))))(defi= ne string->word (let ((=3D =3D) (<=3D <=3D) (+ +) (- -) (char-rank cha= r-rank) (string-ref string-ref) (string-length string-length) (string=3D= ? string=3D?) (not not) (char=3D? char=3D?) (string->number string->num= ber) (string->symbol string->symbol)) (lambda (string) (define = (subsequents? string i length) (cond ((=3D i length) #t) ((<=3D (= char-rank (string-ref string i)) 2) (subsequents? string (+ i 1)= length)) (else #f))) (define (special-id? string) (or (stri= ng=3D? string "+") (string=3D? string "-") (string=3D? string "= ..."))) (define (ok-symbol? string) (if (string=3D? string "") = #f (let ((rank1 (char-rank (string-ref string 0)))) (cond ((= =3D rank1 0) (subsequents? string 1 (string-length string))) ((=3D= rank1 1) (special-id? string)) (else #f))))) (define (nn-hel= per string i len seen-point?) (cond ((=3D i len) (if seen-point?= (not (char=3D? (string-ref string (- len 1)) #\0)) #t)) = ((char=3D? #\. (string-ref string i)) (cond (seen-point? #f) = ((=3D (+ i 2) len) #t) ; Accepts "23.0" (else (nn-helper stri= ng (+ i 1) len #t)))) ((=3D 2 (char-rank (string-ref string i))) = (nn-helper string (+ i 1) len seen-point?)) (else #f))) = (define (narrow-number? string) (if (string=3D? string "") #f = (let* ((c0 (string-ref string 0)) (start 0) (len (string-leng= th string)) (cn (string-ref string (- len 1)))) (if (and (cha= r=3D? c0 #\-) (not (=3D len 1))) (begin (set! start 1) (set!= c0 (string-ref string 1))) #f) (cond ((not (=3D (char-rank cn= ) 2)) #f) ; Rejects "-" among others ((char=3D? c0 #\.) #f) = ((char=3D? c0 #\0) (cond ((=3D len 1) #t) ; Accepts "0" but not = "-0" ((=3D len 2) #f) ; Rejects "-0" and "03" ((char=3D? (st= ring-ref string (+ start 1)) #\.) (nn-helper string (+ start 2) l= en #t)) (else #f))) (else (nn-helper string start len #f)))))= ) ;; The body of string->word: (cond ((narrow-number? string= ) (string->number string)) ((ok-symbol? string) (string->symbol str= ing)) (else string)))))(define char->word (let ((=3D =3D) (char-ra= nk char-rank) (make-string make-string) (char=3D? char=3D?) (str= ing->symbol string->symbol) (string->number string->number)) (lambda= (char) (let ((rank (char-rank char)) (string (make-string 1 c= har))) (cond ((=3D rank 0) (string->symbol string)) ((=3D rank 2)= (string->number string)) ((char=3D? char #\+) '+) ((char=3D= ? char #\-) '-) (else string))))))(define word->string (let ((nu= mber? number?) (string? string?) (number->string number->string) (symbo= l->string symbol->string)) (lambda (wd) (cond ((string? wd) wd)= ((number? wd) (number->string wd)) (else (symbol->string wd)))= )))(define count (let ((word? word?) (string-length string-length) (wo= rd->string word->string) (length length)) (lambda (stuff) (if (= word? stuff) (string-length (word->string stuff)) (length stuff))))= )(define word (let ((string->word string->word) (apply apply) (string-= append string-append) (map map) (word? word?) (word->string word->strin= g) (whoops whoops)) (lambda x (string->word (apply string= -append (map (lambda (arg) (if (word? arg) (word->string= arg) (whoops "Invalid argument to WORD: " arg))) x))))))(define= se (let ((pair? pair?) (null? null?) (word? word?) (car car) (cons co= ns) (cdr cdr) (whoops whoops)) (define (paranoid-append a original-a= b) (cond ((null? a) b) ((word? (car a)) (cons (car a) (p= aranoid-append (cdr a) original-a b))) (else (whoops "Argument to S= ENTENCE not a word or sentence" original-a )))) (define (combine-= two a b) ;; Note: b is always a list (cond ((pair? = a) (paranoid-append a a b)) ((null? a) b) ((word? a) (cons a b)= ) (else (whoops "Argument to SENTENCE not a word or sentence:" a)))= ) ;; Helper function so recursive calls don't show up in TRACE (d= efine (real-se args) (if (null? args) '() (combine-two (car ar= gs) (real-se (cdr args))))) (lambda args (real-se args))))(defi= ne sentence se)(define first (let ((pair? pair?) (char->word char->wor= d) (string-ref string-ref) (word->string word->string) (car car) (empty= ? empty?) (whoops whoops) (word? word?)) (define (word-first wd) = (char->word (string-ref (word->string wd) 0))) (lambda (x) (c= ond ((pair? x) (car x)) ((empty? x) (whoops "Invalid argument to FI= RST: " x)) ((word? x) (word-first x)) (else (whoops "Invalid ar= gument to FIRST: " x))))))(define last (let ((pair? pair?) (- -) (word= ->string word->string) (char->word char->word) (string-ref string-ref) = (string-length string-length) (empty? empty?) (cdr cdr) (car car) (whoo= ps whoops) (word? word?)) (define (word-last wd) (let ((s (word= ->string wd))) (char->word (string-ref s (- (string-length s) 1))))) = (define (list-last lst) (if (empty? (cdr lst)) (car lst) = (list-last (cdr lst)))) (lambda (x) (cond ((pair? x) (list-la= st x)) ((empty? x) (whoops "Invalid argument to LAST: " x)) ((w= ord? x) (word-last x)) (else (whoops "Invalid argument to LAST: " x= ))))))(define bf (let ((pair? pair?) (substring substring) (string-len= gth string-length) (string->word string->word) (word->string word->stri= ng) (cdr cdr) (empty? empty?) (whoops whoops) (word? word?)) (define= string-bf (lambda (s) (substring s 1 (string-length s)))) = (define (word-bf wd) (string->word (string-bf (word->string wd)))= ) (lambda (x) (cond ((pair? x) (cdr x)) ((empty? x) (whoops= "Invalid argument to BUTFIRST: " x)) ((word? x) (word-bf x)) (= else (whoops "Invalid argument to BUTFIRST: " x))))))(define butfirst b= f)(define bl (let ((pair? pair?) (- -) (cdr cdr) (cons cons) (car car)= (substring substring) (string-length string-length) (string->word stri= ng->word) (word->string word->string) (empty? empty?) (whoops whoops) (= word? word?)) (define (list-bl list) (if (null? (cdr list)) '= () (cons (car list) (list-bl (cdr list))))) (define (string-bl s) = (substring s 0 (- (string-length s) 1))) (define (word-bl wd)= (string->word (string-bl (word->string wd)))) (lambda (x) = (cond ((pair? x) (list-bl x)) ((empty? x) (whoops "Invalid argumen= t to BUTLAST: " x)) ((word? x) (word-bl x)) (else (whoops "Inva= lid argument to BUTLAST: " x))))))(define butlast bl)(define item (let= ((> >) (- -) (< <) (integer? integer?) (list-ref list-ref) (char->word= char->word) (string-ref string-ref) (word->string word->string) (not n= ot) (whoops whoops) (count count) (word? word?) (list? list?)) (defi= ne (word-item n wd) (char->word (string-ref (word->string wd) (- n= 1)))) (lambda (n stuff) (cond ((not (integer? n)) (whoops= "Invalid first argument to ITEM (must be an integer): " n)) = ((< n 1) (whoops "Invalid first argument to ITEM (must be positive= ): " n)) ((> n (count stuff)) (whoops "No such item: " n= stuff)) ((word? stuff) (word-item n stuff)) ((list? stuff) (li= st-ref stuff (- n 1))) (else (whoops "Invalid second argument to IT= EM: " stuff))))))(define equal? ;; Note that EQUAL? assumes strings ar= e numbers. ;; (strings-are-numbers #f) doesn't change this behavior. = (let ((vector-length vector-length) (=3D =3D) (vector-ref vector-ref) (= + +) (string? string?) (symbol? symbol?) (null? null?) (pair? pair?) (c= ar car) (cdr cdr) (eq? eq?) (string=3D? string=3D?) (symbol->string sym= bol->string) (number? number?) (string->word string->word) (vector? vec= tor?) (eqv? eqv?)) (define (vector-equal? v1 v2) (let ((len1 (v= ector-length v1)) (len2 (vector-length v2))) (define (helper i) (= if (=3D i len1) #t (and (equal? (vector-ref v1 i) (vector-r= ef v2 i)) (helper (+ i 1))))) (if (=3D len1 len2) (helper 0) = #f))) (lambda (x y) (cond ((null? x) (null? y)) ((null? y= ) #f) ((pair? x) (and (pair? y) (equal? (car x) (car y)) = (equal? (cdr x) (cdr y)))) ((pair? y) #f) ((symbol? x) (o= r (and (symbol? y) (eq? x y)) (and (string? y) (string=3D? (symbol->s= tring x) y)))) ((symbol? y) (and (string? x) (string=3D? x (sy= mbol->string y)))) ((number? x) (or (and (number? y) (=3D x y)= ) (and (string? y) (let ((possible-num (string->word y))) (a= nd (number? possible-num) (=3D x possible-num)))))) ((number= ? y) (and (string? x) (let ((possible-num (string->word x))) = (and (number? possible-num) (=3D possible-num y))))) ((string= ? x) (and (string? y) (string=3D? x y))) ((string? y) #f) ((vec= tor? x) (and (vector? y) (vector-equal? x y))) ((vector? y) #f) = (else (eqv? x y))))))(define member? (let ((> >) (- -) (< <) (null? n= ull?) (symbol? symbol?) (eq? eq?) (car car) (not not) (symbol->string s= ymbol->string) (string=3D? string=3D?) (cdr cdr) (equal? equal?) (word-= >string word->string) (string-length string-length) (whoops whoops) (st= ring-ref string-ref) (char=3D? char=3D?) (list? list?) (number? number?= ) (empty? empty?) (word? word?) (string? string?)) (define (symbol-i= n-list? symbol string lst) (cond ((null? lst) #f) ((and (symbo= l? (car lst)) (eq? symbol (car lst)))) ((string? (car lst)) = (cond ((not string) (symbol-in-list? symbol (symbol->string symbo= l) lst)) ((string=3D? string (car lst)) #t) (else (symbol-in-li= st? symbol string (cdr lst))))) (else (symbol-in-list? symbol strin= g (cdr lst))))) (define (word-in-list? wd lst) (cond ((null? ls= t) #f) ((equal? wd (car lst)) #t) (else (word-in-list? wd (cdr = lst))))) (define (word-in-word? small big) (let ((one-letter-st= r (word->string small))) (if (> (string-length one-letter-str) 1) (= whoops "Invalid arguments to MEMBER?: " small big) (let ((big-str (= word->string big))) (char-in-string? (string-ref one-letter-str 0= ) big-str (- (string-length big-str) 1)))))) (defi= ne (char-in-string? char string i) (cond ((< i 0) #f) ((char=3D= ? char (string-ref string i)) #t) (else (char-in-string? char strin= g (- i 1))))) (lambda (x stuff) (cond ((empty? stuff) #f) (= (word? stuff) (word-in-word? x stuff)) ((not (list? stuff)) (w= hoops "Invalid second argument to MEMBER?: " stuff)) ((symbol? x) (= symbol-in-list? x #f stuff)) ((or (number? x) (string? x)) (wo= rd-in-list? x stuff)) (else (whoops "Invalid first argument to MEMB= ER?: " x))))))(define before? (let ((not not) (word? word?) (whoops wh= oops) (stringstring word->string)) (lambda (wd1 = wd2) (cond ((not (word? wd1)) (whoops "Invalid first argument= to BEFORE? (not a word): " wd1)) ((not (word? wd2)) (whoops "= Invalid second argument to BEFORE? (not a word): " wd2)) (else (str= ingstring wd1) (word->string wd2)))))));;; Higher Order Funct= ions(define filter (let ((null? null?) (car car) (cons cons) (cdr cdr)= (not not) (procedure? procedure?) (whoops whoops) (list? list?)) (l= ambda (pred l) ;; Helper function so recursive calls don't show up= in TRACE (define (real-filter l) (cond ((null? l) '()) ((pr= ed (car l)) (cons (car l) (real-filter (cdr l)))) (else (r= eal-filter (cdr l))))) (cond ((not (procedure? pred)) (whoops= "Invalid first argument to FILTER (not a procedure): " pred)) = ((not (list? l)) (whoops "Invalid second argument to FILTER (not= a list): " l)) (else (real-filter l))))))(define keep (let ((+ +)= (=3D =3D) (pair? pair?) (substring substring) (char->word char->word) = (string-ref string-ref) (string-set! string-set!) (word->string word->s= tring) (string-length string-length) (string->word string->word) (make-= string make-string) (procedure? procedure?) (whoops whoops) (word? word= ?) (null? null?)) (lambda (pred w-or-s) (define (keep-string in= i out out-len len) (cond ((=3D i len) (substring out 0 out-len)) = ((pred (char->word (string-ref in i))) (string-set! out out-len= (string-ref in i)) (keep-string in (+ i 1) out (+ out-len 1) le= n)) (else (keep-string in (+ i 1) out out-len len)))) (defin= e (keep-word wd) (let* ((string (word->string wd)) (len (string-= length string))) (string->word (keep-string string 0 (make-string = len) 0 len)))) (cond ((not (procedure? pred)) (whoops "Invali= d first argument to KEEP (not a procedure): " pred)) ((pair? w= -or-s) (filter pred w-or-s)) ((word? w-or-s) (keep-word w-or-s)) = ((null? w-or-s) '()) (else (whoops "Bad second argument to K= EEP (not a word or sentence): " w-or-s))))))(define appearances = (let ((count count) (keep keep) (equal? equal?)) (lambda (item aggre= gate) (count (keep (lambda (element) (equal? item element)) aggreg= ate)))))(define every (let ((=3D =3D) (+ +) (se se) (char->word char->= word) (string-ref string-ref) (empty? empty?) (first first) (bf bf) (no= t not) (procedure? procedure?) (whoops whoops) (word? word?) (word->str= ing word->string) (string-length string-length)) (lambda (fn stuff) = (define (string-every string i length) (if (=3D i length) '() = (se (fn (char->word (string-ref string i))) (string-every string (= + i 1) length)))) (define (sent-every sent) ;; This proc. can't be= optimized or else it will break the ;; exercise where we ask them to r= eimplement sentences as ;; vectors and then see if every still works. (= if (empty? sent) sent ; Can't be '() or exercise breaks. (se (= fn (first sent)) (sent-every (bf sent))))) (cond ((not (proce= dure? fn)) (whoops "Invalid first argument to EVERY (not a procedu= re):" fn)) ((word? stuff) (let ((string (word->string st= uff))) (string-every string 0 (string-length string)))) (els= e (sent-every stuff))))))(define accumulate (let ((not not) (empty? em= pty?) (bf bf) (first first) (procedure? procedure?) (whoops whoops) (me= mber member) (list list)) (lambda (combiner stuff) (define (rea= l-accumulate stuff) (if (empty? (bf stuff)) (first stuff) (comb= iner (first stuff) (real-accumulate (bf stuff))))) (cond ((not (pr= ocedure? combiner)) (whoops "Invalid first argument to ACCUMULATE = (not a procedure):" combiner)) ((not (empty? stuff)) (real-ac= cumulate stuff)) ((member combiner (list + * word se)) (combiner)) = (else (whoops "Can't accumulate empty input with that combiner= "))))))(define reduce (let ((null? null?) (cdr cdr) (car car) (not not= ) (procedure? procedure?) (whoops whoops) (member member) (list list)) = (lambda (combiner stuff) (define (real-reduce stuff) (if (null?= (cdr stuff)) (car stuff) (combiner (car stuff) (real-reduce (c= dr stuff))))) (cond ((not (procedure? combiner)) (whoops "Inv= alid first argument to REDUCE (not a procedure):" combiner)) = ((not (null? stuff)) (real-reduce stuff)) ((member combiner (list += * word se append)) (combiner)) (else (whoops "Can't reduce empty i= nput with that combiner"))))))(define repeated (let ((=3D =3D) (- -)) = (lambda (fn number) (if (=3D number 0) (lambda (x) x) (lamb= da (x) ((repeated fn (- number 1)) (fn x)))))));; Tree stuff(define= make-node cons)(define datum car)(define children cdr);; I/O (d= efine show (let ((=3D =3D) (length length) (display display) (car car)= (newline newline) (not not) (output-port? output-port?) (apply apply) = (whoops whoops)) (lambda args (cond ((=3D (length args) 1= ) (display (car args)) (newline)) ((=3D (length args) 2) (if (not= (output-port? (car (cdr args)))) (whoops "Invalid second argument = to SHOW (not an output port): " (car (cdr args)))) (apply display = args) (newline (car (cdr args)))) (else (whoops "Incorrect number= of arguments to procedure SHOW"))))))(define show-line (let ((>=3D >=3D= ) (length length) (whoops whoops) (null? null?) (current-output-port cu= rrent-output-port) (car car) (not not) (list? list?) (display display) = (for-each for-each) (cdr cdr) (newline newline)) (lambda (line . arg= s) (if (>=3D (length args) 2) (whoops "Too many arguments to sho= w-line") (let ((port (if (null? args) (current-output-port) (car args= )))) (cond ((not (list? line)) (whoops "Invalid argument to SHO= W-LINE (not a list):" line)) ((null? line) #f) (else (display= (car line) port) (for-each (lambda (wd) (display " " port) (displa= y wd port)) (cdr line)))) (newline port))))))(define read-st= ring (let ((read-char read-char) (eqv? eqv?) (apply apply) (string-app= end string-append) (substring substring) (reverse reverse) (cons cons) = (>=3D >=3D) (+ +) (string-set! string-set!) (length length) (whoops who= ops) (null? null?) (current-input-port current-input-port) (car car) (c= dr cdr) (eof-object? eof-object?) (list list) (make-string make-string)= (peek-char peek-char)) (define (read-string-helper chars all-length= chunk-length port) (let ((char (read-char port)) (string (car= chars))) (cond ((or (eof-object? char) (eqv? char #\newline)) (= apply string-append (reverse (cons (substring (car cha= rs) 0 chunk-length) (cdr chars))))) ((>=3D chunk-length 80) = (let ((newstring (make-string 80))) (string-set! newstring 0 char= ) (read-string-helper (cons newstring chars) (+ all-length 1)= 1 port))) (else (string-set! string chunk= -length char) (read-string-helper chars (+ all-length 1) = (+ chunk-length 1) port))))) (lambda args (if (>=3D = (length args) 2) (whoops "Too many arguments to read-string") (let = ((port (if (null? args) (current-input-port) (car args)))) (if (eof= -object? (peek-char port)) (read-char port) (read-string-helper (list= (make-string 80)) 0 0 port)))))))(define read-line (let ((=3D =3D) (l= ist list) (string->word string->word) (substring substring) (char-white= space? char-whitespace?) (string-ref string-ref) (+ +) (string-length s= tring-length) (apply apply) (read-string read-string)) (lambda args = (define (tokenize string) (define (helper i start len) (cond ((=3D= i len) (if (=3D i start) '() (list (string->word (substr= ing string start i))))) ((char-whitespace? (string-ref string i)) (i= f (=3D i start) (helper (+ i 1) (+ i 1) len) (cons (string-= >word (substring string start i)) (helper (+ i 1) (+ i 1) len)))) = (else (helper (+ i 1) start len)))) (if (eof-object? string) = string (helper 0 0 (string-length string)))) (= tokenize (apply read-string args)))))(define *the-open-inports* '())(de= fine *the-open-outports* '())(define align (let ((< <) (abs abs) (* *)= (expt expt) (>=3D >=3D) (- -) (+ +) (=3D =3D) (null? null?) (car car) = (round round) (number->string number->string) (string-length string-len= gth) (string-append string-append) (make-string make-string) (substring= substring) (string-set! string-set!) (number? number?) (word->string w= ord->string)) (lambda (obj width . rest) (define (align-number = obj width rest) (let* ((sign (< obj 0)) (num (abs obj)) (= prec (if (null? rest) 0 (car rest))) (big (round (* num (expt 10= prec)))) (cvt0 (number->string big)) (cvt (if (< num 1) = (string-append "0" cvt0) cvt0)) (pos-str (if (>=3D (string-lengt= h cvt0) prec) cvt (string-append (make-string (- pre= c (string-length cvt0)) #\0) cvt))) (string (if sign (str= ing-append "-" pos-str) pos-str)) (length (+ (string-length stri= ng) (if (=3D prec 0) 0 1))) (left (- length (+ 1 prec))) = (result (if (=3D prec 0) string (string-append (sub= string string 0 left) "." (substring string left (- length = 1)))))) (cond ((=3D length width) result) ((< length width) (strin= g-append (make-string (- width length) #\space) result)) (else (let ((= new (substring result 0 width))) (string-set! new (- width 1) #\+) = new))))) (define (align-word string) (let ((length (string-length = string))) (cond ((=3D length width) string) ((< length width) (str= ing-append string (make-string (- width length) #\space))) (else (let = ((new (substring string 0 width))) (string-set! new (- width 1) #\+) = new))))) (if (number? obj) (align-number obj width rest) (al= ign-word (word->string obj))))))(define open-output-file (let ((oof op= en-output-file) (cons cons)) (lambda (filename) (let ((port (oo= f filename))) (set! *the-open-outports* (cons port *the-open-outports*)= ) port))))(define open-input-file (let ((oif open-input-file) (cons co= ns)) (lambda (filename) (let ((port (oif filename))) (set! *the= -open-inports* (cons port *the-open-inports*)) port))))(define remove! = (let ((null? null?) (cdr cdr) (eq? eq?) (set-cdr! set-cdr!) (car car))= (lambda (thing lst) (define (r! prev) (cond ((null? (cdr prev)= ) lst) ((eq? thing (car (cdr prev))) (set-cdr! prev (cdr (= cdr prev))) lst) (else (r! (cdr prev))))) (cond ((nul= l? lst) lst) ((eq? thing (car lst)) (cdr lst)) (else (r! lst)))= )))(define close-input-port (let ((cip close-input-port) (remove! remo= ve!)) (lambda (port) (set! *the-open-inports* (remove! port *th= e-open-inports*)) (cip port))))(define close-output-port (let ((c= op close-output-port) (remove! remove!)) (lambda (port) (set! *= the-open-outports* (remove! port *the-open-outports*)) (cop port))= ))(define close-all-ports (let ((for-each for-each) (close-input-port = close-input-port) (close-output-port close-output-port)) (lambda () = (for-each close-input-port *the-open-inports*) (for-each clos= e-output-port *the-open-outports*) 'closed)));; Make arithmetic wo= rk on numbers in string form:(define maybe-num (let ((string? string?)= (string->number string->number)) (lambda (arg) (if (string? ar= g) (let ((num (string->number arg))) (if num num arg)) arg))))(= define logoize (let ((apply apply) (map map) (maybe-num maybe-num)) = (lambda (fn) (lambda args (apply fn (map maybe-num args))))));; s= pecial case versions of logoize, since (lambda args ...) is expensive(d= efine logoize-1 (let ((maybe-num maybe-num)) (lambda (fn) (lam= bda (x) (fn (maybe-num x))))))(define logoize-2 (let ((maybe-num maybe= -num)) (lambda (fn) (lambda (x y) (fn (maybe-num x) (maybe-num = y))))))(define strings-are-numbers (let ((are-they? #f) (real-*= *) (real-+ +) (real-- -) (real-/ /) (real-< <) (real-<=3D <=3D)= (real-=3D =3D) (real-> >) (real->=3D >=3D) (real-abs abs) (real= -acos acos) (real-asin asin) (real-atan atan) (real-ceiling ceil= ing) (real-cos cos) (real-even? even?) (real-exp exp) (real-expt= expt) (real-floor floor) (real-align align) (real-gcd gcd) (rea= l-integer? integer?) (real-item item) (real-lcm lcm) (real-list-= ref list-ref) (real-log log) (real-make-vector make-vector) (rea= l-max max) (real-min min) (real-modulo modulo) (real-negative? n= egative?) (real-number? number?) (real-odd? odd?) (real-positive= ? positive?) (real-quotient quotient) (real-random random) (real= -remainder remainder) (real-repeated repeated) (real-round round= ) (real-sin sin) (real-sqrt sqrt) (real-tan tan) (real-truncate = truncate) (real-vector-ref vector-ref) (real-vector-set! vector-= set!) (real-zero? zero?) (maybe-num maybe-num) (number->string n= umber->string) (cons cons) (car car) (cdr cdr) (eq? eq?) (show show) (l= ogoize logoize) (logoize-1 logoize-1) (logoize-2 logoize-2) (not not) (= whoops whoops)) (lambda (yesno) (cond ((and are-they? (eq? yesn= o #t)) (show "Strings are already numbers")) ((eq? yesno #t) = (set! are-they? #t) (set! * (logoize real-*)) (set! + (lo= goize real-+)) (set! - (logoize real--)) (set! / (logoize rea= l-/)) (set! < (logoize real-<)) (set! <=3D (logoize real-<=3D= )) (set! =3D (logoize real-=3D)) (set! > (logoize real->)) = (set! >=3D (logoize real->=3D)) (set! abs (logoize-1 real-abs))= (set! acos (logoize-1 real-acos)) (set! asin (logoize-1 real= -asin)) (set! atan (logoize real-atan)) (set! ceiling (logoiz= e-1 real-ceiling)) (set! cos (logoize-1 real-cos)) (set! even= ? (logoize-1 real-even?)) (set! exp (logoize-1 real-exp)) (se= t! expt (logoize-2 real-expt)) (set! floor (logoize-1 real-floor))= (set! align (logoize align)) (set! gcd (logoize real-gcd)) = (set! integer? (logoize-1 real-integer?)) (set! item (lambda (= n stuff) (real-item (maybe-num n) stuff))) (set! lcm (logoize = real-lcm)) (set! list-ref (lambda (lst k) (real-list-ref = lst (maybe-num k)))) (set! log (logoize-1 real-log)) (set! ma= x (logoize real-max)) (set! min (logoize real-min)) (set! mod= ulo (logoize-2 real-modulo)) (set! negative? (logoize-1 real-negat= ive?)) (set! number? (logoize-1 real-number?)) (set! odd? (lo= goize-1 real-odd?)) (set! positive? (logoize-1 real-positive?)) = (set! quotient (logoize-2 real-quotient)) (set! random (logoize= real-random)) (set! remainder (logoize-2 real-remainder)) (s= et! round (logoize-1 real-round)) (set! sin (logoize-1 real-sin)) = (set! sqrt (logoize-1 real-sqrt)) (set! tan (logoize-1 real-t= an)) (set! truncate (logoize-1 real-truncate)) (set! zero? (l= ogoize-1 real-zero?)) (set! vector-ref (lambda (vec i) (real-v= ector-ref vec (maybe-num i)))) (set! vector-set! (lambda (vec = i val) (real-vector-set! vec (maybe-num i) val))) (set! make= -vector (lambda (num . args) (apply real-make-vector (cons (m= aybe-num num) args)))) (set! list-ref (lambda (lst i) = (real-list-ref lst (maybe-num i)))) (set! repeated (lambda (fn= n) (real-repeated fn (maybe-num n))))) ((and (not are-they?) (not = yesno)) (show "Strings are already not numbers")) ((not yesno)= (set! are-they? #f) (set! * real-*) (set! + real-+) (set! - = real--) (set! / real-/) (set! < real-<) (set! <=3D real-<=3D) (set= ! =3D real-=3D) (set! > real->) (set! >=3D real->=3D) (set! abs re= al-abs) (set! acos real-acos) (set! asin real-asin) (set! atan rea= l-atan) (set! ceiling real-ceiling) (set! cos real-cos) (set!= even? real-even?) (set! exp real-exp) (set! expt real-expt) = (set! floor real-floor) (set! align real-align) (set! gcd real-gcd= ) (set! integer? real-integer?) (set! item real-item) (set! l= cm real-lcm) (set! list-ref real-list-ref) (set! log real-log) (se= t! max real-max) (set! min real-min) (set! modulo real-modulo) (se= t! odd? real-odd?) (set! quotient real-quotient) (set! random real= -random) (set! remainder real-remainder) (set! round real-round) = (set! sin real-sin) (set! sqrt real-sqrt) (set! tan real-tan) = (set! truncate real-truncate) (set! zero? real-zero?) (set! positi= ve? real-positive?) (set! negative? real-negative?) (set! number? = real-number?) (set! vector-ref real-vector-ref) (set! vector-set! = real-vector-set!) (set! make-vector real-make-vector) (set! l= ist-ref real-list-ref) (set! item real-item) (set! repeated real-r= epeated)) (else (whoops "Strings-are-numbers: give a #t or a #f")))= are-they?)));; By default, strings are numbers:(strings-are-number= s #t) ------=_=-_OpenGroupware_org_NGMime-13209-1596795819.130932-4--------