Created
April 7, 2019 22:46
-
-
Save SYZYGY-DEV333/d74ca36b9a4c504b25c0a2380203c98d to your computer and use it in GitHub Desktop.
LispLisp to LazyK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (replace source target replacement) | |
(cond ((null? source)'()) | |
((equal? source target) replacement) | |
(else (let ((next (car source)) | |
(rest (cdr source))) | |
(cons (if (not (list? next)) | |
next | |
(replace next target replacement)) | |
(replace rest target replacement)))))) | |
(define (replace-S source) | |
(replace source '(lisp (lisp (lisp (lisp lisp)))) 's)) | |
(define (replace-K source) | |
(replace source '(lisp (lisp (lisp lisp))) 'k)) | |
(define (replace-I source) | |
(replace source '(lisp lisp) 'i)) | |
(define (parse program) | |
(let* ( [step1 (replace-S program)] | |
[step2 (replace-K step1)] | |
[step3 (replace-I step2)] ) | |
step3)) | |
(define (expr-dispatch expr leaf appl lamb) | |
(if (pair? expr) | |
(if (eq? (car expr) 'lambda) | |
(lamb (caadr expr) (caddr expr)) | |
(appl (car expr) (cadr expr)) ) | |
(leaf expr) )) | |
(define (print-as-generic aply k s i) | |
(lambda (lazified-code) | |
(let self ((code lazified-code)) | |
(expr-dispatch code | |
(lambda (leaf) | |
(cond ((eq? leaf 'i) (display i)) | |
((eq? leaf 'k) (display k)) | |
((eq? leaf 's) (display s)) | |
(else (display "[") (display leaf) (display "]")) )) | |
(lambda (f g) | |
(display aply) | |
(self f) | |
(self g) ) | |
(lambda (var body) | |
(error "Can't print lambdas as Lazy code!") ))) | |
(newline) )) | |
(define print-as-unlambda (print-as-generic "`" "k" "s" "i")) | |
(print-as-unlambda (parse (read))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment