📄 scheme.scm
字号:
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(define (gen-case code1 code2) (lambda (rte) (code2 rte (code1 rte))))(define (gen-case-clause datums code1 code2) (lambda (rte key) (if (memv key datums) (code1 rte) (code2 rte key))))(define (gen-case-else code) (lambda (rte key) (code rte))); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(define (gen-letrec vals body) (let ((nb-vals (length vals))) (case nb-vals ((1) (gen-letrec-1 (car vals) body)) ((2) (gen-letrec-2 (car vals) (cadr vals) body)) ((3) (gen-letrec-3 (car vals) (cadr vals) (caddr vals) body)) (else (gen-letrec-n nb-vals vals body)))))(define (gen-letrec-1 val1 body) (lambda (rte) (let ((x (vector rte #f))) (vector-set! x 1 (val1 x)) (body x))))(define (gen-letrec-2 val1 val2 body) (lambda (rte) (let ((x (vector rte #f #f))) (vector-set! x 1 (val1 x)) (vector-set! x 2 (val2 x)) (body x))))(define (gen-letrec-3 val1 val2 val3 body) (lambda (rte) (let ((x (vector rte #f #f #f))) (vector-set! x 1 (val1 x)) (vector-set! x 2 (val2 x)) (vector-set! x 3 (val3 x)) (body x))))(define (gen-letrec-n nb-vals vals body) (lambda (rte) (let ((x (make-vector (+ nb-vals 1)))) (vector-set! x 0 rte) (let loop ((x x) (i 1) (l vals)) (if (pair? l) (begin (vector-set! x i ((car l) x)) (loop x (+ i 1) (cdr l))))) (body x)))); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(define (gen-macro name proc) (lambda (rte) (scheme-add-macro name proc))); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(define (gen-combination oper args) (case (length args) ((0) (gen-combination-0 oper)) ((1) (gen-combination-1 oper (car args))) ((2) (gen-combination-2 oper (car args) (cadr args))) ((3) (gen-combination-3 oper (car args) (cadr args) (caddr args))) (else (gen-combination-n oper args))))(define (gen-combination-0 oper) (lambda (rte) ((oper rte))))(define (gen-combination-1 oper arg1) (lambda (rte) ((oper rte) (arg1 rte))))(define (gen-combination-2 oper arg1 arg2) (lambda (rte) ((oper rte) (arg1 rte) (arg2 rte))))(define (gen-combination-3 oper arg1 arg2 arg3) (lambda (rte) ((oper rte) (arg1 rte) (arg2 rte) (arg3 rte))))(define (gen-combination-n oper args) (lambda (rte) (define (evaluate l rte) (if (pair? l) (cons ((car l) rte) (evaluate (cdr l) rte)) '())) (apply (oper rte) (evaluate args rte)))); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(define (scheme-comp expr env) (cond ((symbol? expr) (comp-var expr env)) ((not (pair? expr)) (comp-self-eval expr env)) ((macro?2 (car expr) env) (scheme-comp (macro-expand expr env) env)) (else (cond ((eq? (car expr) 'quote) (comp-quote expr env)) ((eq? (car expr) 'quasiquote) (comp-quasiquote expr env)) ((eq? (car expr) 'unquote) (comp-unquote expr env)) ((eq? (car expr) 'unquote-splicing) (comp-unquote-splicing expr env)) ((eq? (car expr) 'set!) (comp-set! expr env)) ((eq? (car expr) 'lambda) (comp-lambda expr env)) ((eq? (car expr) 'if) (comp-if expr env)) ((eq? (car expr) 'cond) (comp-cond expr env)) ((eq? (car expr) 'and) (comp-and expr env)) ((eq? (car expr) 'or) (comp-or expr env)) ((eq? (car expr) 'case) (comp-case expr env)) ((eq? (car expr) 'let) (comp-let expr env)) ((eq? (car expr) 'let*) (comp-let* expr env)) ((eq? (car expr) 'letrec) (comp-letrec expr env)) ((eq? (car expr) 'begin) (comp-begin expr env)) ((eq? (car expr) 'do) (comp-do expr env)) ((eq? (car expr) 'define) (comp-define expr env)) ((eq? (car expr) 'define-macro) (comp-define-macro expr env)) (else (comp-combination expr env)))))); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(define (scheme-global-var name) (let ((x (assq name scheme-global-variables))) (if x x (let ((y (cons name '()))) (set! scheme-global-variables (cons y scheme-global-variables)) y))))(define (scheme-global-var-ref i) (cdr i))(define (scheme-global-var-set! i val) (set-cdr! i val) '())(define scheme-global-variables '())(define (def-proc name value) (scheme-global-var-set! (scheme-global-var name) value))(def-proc 'not (lambda (x) (not x)))(def-proc 'boolean? boolean?)(def-proc 'eqv? eqv?)(def-proc 'eq? eq?)(def-proc 'equal? equal?)(def-proc 'pair? pair?)(def-proc 'cons cons)(def-proc 'car (lambda (x) (car x)))(def-proc 'cdr (lambda (x) (cdr x)))(def-proc 'set-car! set-car!)(def-proc 'set-cdr! set-cdr!)(def-proc 'caar caar)(def-proc 'cadr cadr)(def-proc 'cdar cdar)(def-proc 'cddr cddr)(def-proc 'caaar caaar)(def-proc 'caadr caadr)(def-proc 'cadar cadar)(def-proc 'caddr caddr)(def-proc 'cdaar cdaar)(def-proc 'cdadr cdadr)(def-proc 'cddar cddar)(def-proc 'cdddr cdddr)(def-proc 'caaaar caaaar)(def-proc 'caaadr caaadr)(def-proc 'caadar caadar)(def-proc 'caaddr caaddr)(def-proc 'cadaar cadaar)(def-proc 'cadadr cadadr)(def-proc 'caddar caddar)(def-proc 'cadddr cadddr)(def-proc 'cdaaar cdaaar)(def-proc 'cdaadr cdaadr)(def-proc 'cdadar cdadar)(def-proc 'cdaddr cdaddr)(def-proc 'cddaar cddaar)(def-proc 'cddadr cddadr)(def-proc 'cdddar cdddar)(def-proc 'cddddr cddddr)(def-proc 'null? (lambda (x) (null? x)))(def-proc 'list? list?)(def-proc 'list list)(def-proc 'length length)(def-proc 'append append)(def-proc 'reverse reverse)(def-proc 'list-ref list-ref)(def-proc 'memq memq)(def-proc 'memv memv)(def-proc 'member member)(def-proc 'assq assq)(def-proc 'assv assv)(def-proc 'assoc assoc)(def-proc 'symbol? symbol?)(def-proc 'symbol->string symbol->string)(def-proc 'string->symbol string->symbol)(def-proc 'number? number?)(def-proc 'complex? complex?)(def-proc 'real? real?)(def-proc 'rational? rational?)(def-proc 'integer? integer?)(def-proc 'exact? exact?)(def-proc 'inexact? inexact?);(def-proc '= =);(def-proc '< <);(def-proc '> >);(def-proc '<= <=);(def-proc '>= >=);(def-proc 'zero? zero?);(def-proc 'positive? positive?);(def-proc 'negative? negative?);(def-proc 'odd? odd?);(def-proc 'even? even?)(def-proc 'max max)(def-proc 'min min);(def-proc '+ +);(def-proc '* *);(def-proc '- -)(def-proc '/ /)(def-proc 'abs abs);(def-proc 'quotient quotient);(def-proc 'remainder remainder);(def-proc 'modulo modulo)(def-proc 'gcd gcd)(def-proc 'lcm lcm);(def-proc 'numerator numerator);(def-proc 'denominator denominator)(def-proc 'floor floor)(def-proc 'ceiling ceiling)(def-proc 'truncate truncate)(def-proc 'round round);(def-proc 'rationalize rationalize)(def-proc 'exp exp)(def-proc 'log log)(def-proc 'sin sin)(def-proc 'cos cos)(def-proc 'tan tan)(def-proc 'asin asin)(def-proc 'acos acos)(def-proc 'atan atan)(def-proc 'sqrt sqrt)(def-proc 'expt expt);(def-proc 'make-rectangular make-rectangular);(def-proc 'make-polar make-polar);(def-proc 'real-part real-part);(def-proc 'imag-part imag-part);(def-proc 'magnitude magnitude);(def-proc 'angle angle)(def-proc 'exact->inexact exact->inexact)(def-proc 'inexact->exact inexact->exact)(def-proc 'number->string number->string)(def-proc 'string->number string->number)(def-proc 'char? char?)(def-proc 'char=? char=?)(def-proc 'char<? char<?)(def-proc 'char>? char>?)(def-proc 'char<=? char<=?)(def-proc 'char>=? char>=?)(def-proc 'char-ci=? char-ci=?)(def-proc 'char-ci<? char-ci<?)(def-proc 'char-ci>? char-ci>?)(def-proc 'char-ci<=? char-ci<=?)(def-proc 'char-ci>=? char-ci>=?)(def-proc 'char-alphabetic? char-alphabetic?)(def-proc 'char-numeric? char-numeric?)(def-proc 'char-whitespace? char-whitespace?)(def-proc 'char-lower-case? char-lower-case?)(def-proc 'char->integer char->integer)(def-proc 'integer->char integer->char)(def-proc 'char-upcase char-upcase)(def-proc 'char-downcase char-downcase)(def-proc 'string? string?)(def-proc 'make-string make-string)(def-proc 'string string)(def-proc 'string-length string-length)(def-proc 'string-ref string-ref)(def-proc 'string-set! string-set!)(def-proc 'string=? string=?)(def-proc 'string<? string<?)(def-proc 'string>? string>?)(def-proc 'string<=? string<=?)(def-proc 'string>=? string>=?)(def-proc 'string-ci=? string-ci=?)(def-proc 'string-ci<? string-ci<?)(def-proc 'string-ci>? string-ci>?)(def-proc 'string-ci<=? string-ci<=?)(def-proc 'string-ci>=? string-ci>=?)(def-proc 'substring substring)(def-proc 'string-append string-append)(def-proc 'vector? vector?)(def-proc 'make-vector make-vector)(def-proc 'vector vector)(def-proc 'vector-length vector-length)(def-proc 'vector-ref vector-ref)(def-proc 'vector-set! vector-set!)(def-proc 'procedure? procedure?)(def-proc 'apply apply)(def-proc 'map map)(def-proc 'for-each for-each)(def-proc 'call-with-current-continuation call-with-current-continuation)(def-proc 'call-with-input-file call-with-input-file)(def-proc 'call-with-output-file call-with-output-file)(def-proc 'input-port? input-port?)(def-proc 'output-port? output-port?)(def-proc 'current-input-port current-input-port)(def-proc 'current-output-port current-output-port)(def-proc 'open-input-file open-input-file)(def-proc 'open-output-file open-output-file)(def-proc 'close-input-port close-input-port)(def-proc 'close-output-port close-output-port)(def-proc 'eof-object? eof-object?)(def-proc 'read read)(def-proc 'read-char read-char)(def-proc 'peek-char peek-char)(def-proc 'write write)(def-proc 'display display)(def-proc 'newline newline)(def-proc 'write-char write-char); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(define (run) (let ((result #f)) (do ((i 100 (- i 1))) ((zero? i) result) (set! result (scheme-eval '(let () (define (sort-list obj pred) (define (loop l) (if (and (pair? l) (pair? (cdr l))) (split l '() '()) l)) (define (split l one two) (if (pair? l) (split (cdr l) two (cons (car l) one)) (merge (loop one) (loop two)))) (define (merge one two) (cond ((null? one) two) ((pred (car two) (car one)) (cons (car two) (merge (cdr two) one))) (else (cons (car one) (merge (cdr one) two))))) (loop obj)) (sort-list '("one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve") string<?)))))))(let ((r (time (run)))) (if (not (equal? r '("eight" "eleven" "five" "four" "nine" "one" "seven" "six" "ten" "three" "twelve" "two"))) (error "wrong result" r) ) )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -