Skip to content

Instantly share code, notes, and snippets.

@martintrojer
Created January 28, 2012 17:05
Show Gist options
  • Save martintrojer/1695041 to your computer and use it in GitHub Desktop.
Save martintrojer/1695041 to your computer and use it in GitHub Desktop.
scheme-clojure-embedded
;Scheme
(cond ((> 3 2) 'greater)
((< 3 2) 'less)
(else equal))
;Clojure
(cond (> 3 2) :greater
(< 3 2) :less)
:else :equal)
(defmacro cond [& args]
(when args
(let [fst# (ffirst args)]
(list `if (if (= fst# (symbol "else")) :else fst#)
(second (first args))
(cons 'cond (next args))))))
Scheme
(cons 1 2)
;Clojure
(cons 1 [2])
(defn cons [fst snd]
(clojure.core/cons fst (if (seq? snd) snd [snd])))
(defmacro define [& args]
`(def ~@args))
(def display println)
(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)))
mtscheme v0.1
=> (define (factorial x) (if (= x 0) 1 (* x (factorial (- x 1)))))
#'mtscheme-repl/factorial
=> (factorial 9)
362880.00
=>
(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")
; 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