Created
January 28, 2012 17:05
-
-
Save martintrojer/1695041 to your computer and use it in GitHub Desktop.
scheme-clojure-embedded
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
;Scheme | |
(cond ((> 3 2) 'greater) | |
((< 3 2) 'less) | |
(else equal)) | |
;Clojure | |
(cond (> 3 2) :greater | |
(< 3 2) :less) | |
:else :equal) |
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
(defmacro cond [& args] | |
(when args | |
(let [fst# (ffirst args)] | |
(list `if (if (= fst# (symbol "else")) :else fst#) | |
(second (first args)) | |
(cons 'cond (next args)))))) |
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
Scheme | |
(cons 1 2) | |
;Clojure | |
(cons 1 [2]) |
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
(defn cons [fst snd] | |
(clojure.core/cons fst (if (seq? snd) snd [snd]))) |
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
(defmacro define [& args] | |
`(def ~@args)) |
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
(def display println) |
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
(macroexpand-1 | |
'(cond ((< x 0) (- 0 x)) ((= x 0) 100) (else x))) | |
=> (if (< x 0) (- 0 x) (cond ((= x 0) 100) (else x))) | |
(clojure.walk/macroexpand-all | |
'(cond ((< x 0) (- 0 x)) ((= x 0) 100) (else x))) | |
=> (if (< x 0) (- 0 x) (if (= x 0) 100 (if :else x nil))) |
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
mtscheme v0.1 | |
=> (define (factorial x) (if (= x 0) 1 (* x (factorial (- x 1))))) | |
#'mtscheme-repl/factorial | |
=> (factorial 9) | |
362880.00 | |
=> |
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
(ns mtscheme-repl | |
(:refer-clojure :exclude [cond cons let]) ; our DSL re-defines these | |
(:use mtscheme)) | |
(defn repl [res] | |
(println res) | |
(print "=> ") | |
(flush) | |
(clojure.core/let [l (read-line)] | |
(recur (eval (read-string l))))) | |
(repl "mtscheme 0.1") |
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
; addition | |
(+ 1 2) | |
(+ 1) | |
; division | |
(/ 9 3) | |
(/ 2) | |
; equality | |
(= 2 2) | |
(= 1) | |
(= 1 (+ 1 1) 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment