The Little Scheme Implementation on Top of Clojure
Scheje
Web REPL by
@turbopape
Scheje v0.2.16, Web REPL v0.1.1, May 2016. This editor has some emacs bindings. Eval last s-exp before cursor using
C-x C-e
. Load the whole buffer using
C-j
.
;; A tiny scheme implementation on Scheje, based on eval/apply from ;; The Lisp 1.5 manual. First eval the whole Buffer (C-j), ;; Then scroll down the buffer for some examples: ;;;;;;;;;;;;;; ;; The code ;; ;;;;;;;;;;;;;; (define atom? (lambda(x) (not (pair? x)))) (define pairlis (lambda(u v t) (cond ((eq? (length u) (length v)) (cond ((null? u) t) (else (alist-cons (car u) (car v) (pairlis (cdr u) (cdr v) t)))))))) (define evcon (lambda(c a) (cond ((my-eval (caar c) a) (my-eval (cadar c) a)) (else (evcon (cdr c) a))))) (define evlis (lambda(m a) (cond ((null? m) '()) (else (cons (my-eval (car m) a) (evlis (cdr m) a)))))) (define my-apply (lambda(fn x a) (cond ((atom? fn) (cond ((eq? fn 'car) (caar x)) ((eq? fn 'cdr) (cdar x)) ((eq? fn 'cons) (cons (car x) (cadr x))) ((eq? fn 'eq?) (eq? (car x) (cadr x))) (else (my-apply (my-eval fn a) x a)))) ((eq? (car fn) 'lambda) (my-eval (caddr fn) (pairlis (cadr fn) x a)))))) (define my-eval (lambda (e a) (cond ((atom? e) (cadr (assoc e a))) ((atom? (car e)) (cond ((eq? (car e) 'quote)(cadr e)) ((eq? (car e) 'cond) (evcon (cdr e) a)) (else (my-apply (car e) (evlis (cdr e) a) a)))) (else (my-apply (car e) (evlis (cdr e) a) a))))) (define eval-quote (lambda (fn) (my-apply (car fn) (evlis (cdr fn) '()) '()))) ;;;;;;;;;;;;;;;;;;; ;; Usage samples ;; ;;;;;;;;;;;;;;;;;;; (eval-quote '(cons 'a '(a b c))) ;;=> (a a b c) (eval-quote '(cdr '(a b c))) ;;=> (b c) (eval-quote '((lambda(a) (cdr a)) '(b c d))) ;;=> (c d)