Lisp: Common Lisp, Racket, Clojure, Emacs Lisp - Hyperpolyglot
Hyperpolyglot
Lisp: Common Lisp, Racket, Clojure, Emacs Lisp
ca side-by-side reference sheet
grammar and execution | variables and expressions | arithmetic and logic | strings | regular expressions | dates and time | lists | fixed-length arrays | dictionaries | user-defined types | functions | execution control | exceptions | streams | emacs buffers | files | directories | processes and environment | libraries and namespaces | objects | lisp macros | reflection | java interop
common lisp<br>racket<br>clojure<br>emacs lisp
version used
SBCL 1.2<br>Racket 6.1<br>Clojure 1.6<br>Emacs 24.5
show version
$ sbcl --version<br>$ racket --version<br>displayed by repl on startup<br>$ emacs --version
grammar and execution
common lisp<br>racket<br>clojure<br>emacs lisp
compiler
$ raco make module.rkt
M-x byte-compile-file
standalone executable<br>(sb-ext:save-lisp-and-die
"executable"
:executable t
:toplevel 'function)<br>$ mzc —exe executable file
interpreter
$ sbcl --script foo.lisp<br>$ racket -r foo.racket<br>specify full path to clojure jar:
java -cp clojure.jar clojure.main foo.clj
shebang<br>#!/usr/bin/env sbcl --script<br>#!/usr/bin/env racket --script<br>specify full path to clojure jar:
#!/usr/bin/env java -jar clojure.jar<br>#!/usr/bin/env emacs --script
repl
$ sbcl<br>$ racket<br>$ java -jar /PATH/TO/clojure.jar<br>M-x ielm
command line program
$ racket -e '(+ 1 1)'
word separator
whitespace<br>whitespace<br>whitespace and commas<br>whitespace
end-of-line comment<br>(+ 1 1) ; adding<br>(+ 1 1) ; adding<br>(+ 1 1) ; adding<br>(+ 1 1) ; adding
multiple line comment<br>(+ 1 #| adding |# 1)<br>(+ 1 #| adding |# 1)
variables and expressions
common lisp<br>racket<br>clojure<br>emacs lisp
identifier<br>case insensitive, cannot start with digit
excluded characters:
SP ( ) " , ' ` : ; # | \
reserved for user macros:
? ! [ ] { }<br>case sensitive, cannot start with digit
excluded characters:
SP ( ) [ ] { } " , ' ` ; # | \<br>case sensitive, cannot start with digit
permitted characters:
A-Z a-z 0-9 * + ! - _ ?
these have special meaning or are reserved:
/ . :<br>case sensitive, cannot start with digit
excluded characters:
SP ( ) " , ' ` ; # | \ _ [ ]
quoted identifier
and escaped identifier<br>(setq |white space symbol| 3)
(setq white\ space\ symbol 3)<br>(define |white space symbol| 3)
(define white\ space\ symbol 3)<br>none
none<br>none
(setq white\ space\ symbol 3)
local variable<br>; parallel assignment:
(let ((x 3) (y 4))
(+ x y))
; sequential assignment:
(let* ((x 3) (y (* x x)))
(+ x y))<br>; parallel assignment:
(let ((x 3) (y 4))
(+ x y))
; sequential assignment:
(let* ((x 3) (y (* x x)))
(+ x y))<br>(let [x 3 y 4]
(+ x y))
(let [[x y] [3 4]]
(+ x y))
(let [x 3 y (* x x)]
(+ x y))<br>; parallel assignment:
(lexical-let ((x 3) (y 4))
(+ x y))
(lexical-let* ((x 3) (y (* x x)))
(+ x y))
global variable
(defparameter *x* 3)
; doesn't change x if already set:
(defvar *x* 3)<br>(define x 3)
; y is not global:
(define (double z)
(define y 2)
(* y z))<br>(def x 3)<br>(set 'x 3)
(setq x 3)
remove variable
(makunbound 'x)<br>(namespace-undefine-variable! 'x)<br>(ns-unmap *ns* 'x)<br>(makunbound 'x)
null
nil '()<br>null '()<br>; same value as null in Java:
nil<br>nil '()
null test
(null x)<br>(null? x)<br>(nil? x)<br>(null x)
identifier as value<br>'x
(quote x)<br>'x
(quote x)<br>'x
(quote x)<br>'x
(quote x)
identifier test
(symbolp 'x)<br>(symbol? 'x)<br>(symbol? 'x)<br>(symbolp 'x)
identifier equality test<br>(eq 'x 'x)<br>(eq? 'x 'x)<br>(= 'x 'x)<br>(eq 'x 'x)
non-referential identifier
:foo<br>#:foo<br>:foo<br>:foo
identifier attributes
set, get, remove<br>(set 'x 13)
(setf (get 'x :desc) "unlucky")
(get 'x :desc)
(remprop 'x :desc)<br>none<br>; value must be instance of clojure.lang.IObj:
(def x (with-meta [13] {:desc "unlucky"}))
(get (meta x) :desc)
; none<br>(set 'x 13)
(setf (get 'x :desc) "unlucky")
(get 'x :desc)
(remprop 'x :desc)
arithmetic and logic
common lisp<br>racket<br>clojure<br>emacs lisp
true and false<br>t nil<br>#t #f
true false<br>true false<br>t nil
falsehoods
nil ()<br>#f false<br>false nil<br>nil ()
logical operators
(or (not t) (and t nil))<br>(or (not #t) (and #t #f))<br>(or (not true) (and true false))<br>(or (not t) (and t nil))
relational operators
= /= =<br>= none =<br>= not= =<br>= /= =
min and max<br>(min 1 2 3)
(max 1 2 3)<br>(min 1 2 3)
(max 1 2 3)<br>(min 1 2 3)
(max 1 2 3)<br>(min 1 2 3)
(max 1 2 3)
numeric predicates<br>numberp integerp
rationalp floatp
realp complexp<br>number? integer?
rational? inexact?
real? complex?<br>number? integer?
rational? float?
none none<br>numberp integerp
none floatp
none none
arithmetic operators
+ - * / mod<br>+ - * / modulo<br>+ - * / mod<br>+ - * / %
integer division
and remainder<br>(truncate 7 3)
(rem 7 3)<br>(quotient 7 3)
(remainder 7 3)<br>(quot 7 3)
(rem 7 3)<br>(/ 7 3)
(% 7 3)
integer division by zero<br>division-by-zero error<br>division by zero error
arith-error
float division<br>rational:
(/ 7 3)
float:
(/ 7 (* 3 1.0))<br>rational:
(/ 7 3)
float:
(/ 7 (float 3))<br>rational:
(/ 7 3)
float:
(/ 7 (* 3 1.0))<br>integer quotient:
(/ 7 3)
float:
(/ 7 (*...