LISP
5 Basic Functions
Scheme | Common Lisp | Clojure | |
---|---|---|---|
1. car | car | car, first | first |
2. cdr | cdr | cdr, rest | rest |
3. cons | cons | cons | cons |
4. atom | (lambda (x) (not (pair? x))) | atom | #(not (list? %)) |
5. eq | eq? | eq | = |
Special Forms (Syntax)
scheme | Common Lisp | Clojure | |
---|---|---|---|
cond | cond | cond | cond |
lambda | lambda | lambda | fn |
define | define | set | def |
Other Basics
scheme | Common Lisp | Clojure | |
---|---|---|---|
(= 0 0) | #t | T | true |
(= 0 1) | #f | NIL | false |
Functional
Basics
Haskell | scheme | Common Lisp | Clojure | |
---|---|---|---|---|
function literal | (\x y -> x^2 + y^2) 3 4 -> 25 | ((lambda (x y) (+ (* x x) (* y y))) 3 4) -> 25 | ((lambda (x y) (+ (* x x) (* y y))) 3 4) -> 25 | ((fn [x y] (+ (* x x) (* y y))) 3 4) (#(+ (* % %) (* %2 %2)) 3 4) |
bind a function literal to a variable | sos = (\x y -> x^2 + y^2) sos 3 4 -> 25 | (define sos (lambda (x y) (+ (* x x) (* y y))) (sos 3 4) -> 25 | (setq sos (lambda (x y) (+ (* x x) (* y y))) | (def sos (fn [x y] (+ (* x x) (* y y))) (sos 3 4) -> 25 |
bind a function literal to a variable in a scope | let sos = (\x y -> x^2 + y^2) in sos 3 4 | (let ((sos (lambda (x y) (+ (* x x) (* y y))))) | (let [sos (fn [x y] (+ (* x x) (* y y)))] (sos 3 4)) -> 25 | |
named function | sos x y = x^2 + y^2 | (define (sos x y) (+ (* x x) (* y y))) (sos 3 4) -> 25 | (defun sos (x y) (+ (* x x) (* y y))) (sos 3 4) -> 25 | (defn sos [x y] (+ (* x x) (* y y))) (sos 3 4) -> 25 |
named function in a scope | let sos x y = x^2 + y^2 in | (letfn [(sos [x y] (+ (* x x) (* y y)))] (sos 3 4)) -> 25 | ||
Haskell | scheme | Common Lisp | Clojure | |
(3,1,4,1,5,9,2) -> 3 | head [3,1,4,1,5,9,2] -> 3 | (car '(3 1 4 1 5 9 2)) -> 3 | (car '(3 1 4 1 5 9 2)) -> 3 | (first '(3 1 4 1 5 9 2)) -> 3 |
(3,1,4,1,5,9,2) -> (1,4,1,5,9,2) | tail [3,1,4,1,5,9,2] -> [1,4,1,5,9,2] | (cdr '(3 1 4 1 5 9 2)) -> (1 4 1 5 9 2) | (cdr '(3 1 4 1 5 9 2)) -> (1 4 1 5 9 2) | (rest '(3 1 4 1 5 9 2)) -> (1 4 1 5 9 2) |
3 (1,4,1,5,9,2) -> (3,1,4,1,5,9,2) | 3:[1,4,1,5,9,2] -> [3,1,4,1,5,9,2] | (cons 3 '(1 4 1 5 9 2)) -> (3 1 4 1 5 9 2) | (cons 3 '(1 4 1 5 9 2)) -> (3 1 4 1 5 9 2) | (cons 3 '(1 4 1 5 9 2)) (conj '(1 4 1 5 9 2) 3) |
(3,1,4) (1,5,9,2) -> (3,1,4,1,5,9,2) | [3,1,4] ++ [1,5,9,2] -> [3,1,4,1,5,9,2] | (append '(3 1 4) '(1 5 9 2)) -> (3 1 4 1 5 9 2) | (append '(3 1 4) '(1 5 9 2)) -> (3 1 4 1 5 9 2) | (concat '(3 1 4) '(1 5 9 2)) -> (3 1 4 1 5 9 2) |
5 (3,1,4,1,5,9,2) -> (3,1,4,1,5) | take 5 [3,1,4,1,5,9,2] -> [3,1,4,1,5] | (take 5 '(3 1 4 1 5 9 2)) -> (3 1 4 1 5) | ||
odd? (3,1,4,1,5,9,2) -> (3,1) | takeWhile odd [3,1,4,1,5,9,2] -> [3,1] | (take-while odd? '(3 1 4 1 5 9 2)) -> (3 1) | ||
odd? (3,1,4,1,5,9,2) -> (4,1,5,9,2) | dropWhile odd [3,1,4,1,5,9,2] -> [4,1,5,9,2] | (drop-while odd? '(3 1 4 1 5 9 2)) -> (4 1 5 9 2) | ||
odd? (3,1,4,1,5,9,2) -> (3,1,1,5,9) | filter odd 5 [3,1,4,1,5,9,2] -> [3,1,1,5,9] | (filter odd? '(3 1 4 1 5 9 2)) -> (3 1 1 5 9) | ||
Haskell | scheme | Common Lisp | Clojure | |
2 (3,1,4,1,5,9,2) -> true 7 (3,1,4,1,5,9,2) -> false | elem 2 [3,1,4,1,5,9,2] -> True elem 7 [3,1,4,1,5,9,2] -> False | (member 2 '(3 1 4 1 5 9 2)) -> (2) (member 7 '(3 1 4 1 5 9 2)) -> #f | (member 2 '(3 1 4 1 5 9 2)) -> (2) (member 7 '(3 1 4 1 5 9 2)) -> NIL | |
even? '(3 1 4 1) -> true even? '(3 5 7 9) -> false | (some even? '(3 1 4 1)) -> true (some even? '(3 5 7 9)) -> nil | |||
odd? '(3 1 4 1) -> false odd? '(3 5 7 9) -> true | (every? odd? '(3 1 4 1)) -> false (every? odd? '(3 5 7 9)) -> true | |||
Haskell | scheme | Common Lisp | Clojure | |
0, 1, 2, ... infinite | [0..] -> [0,1,2,...infinitely] | (range) -> (0 1 2 3 ... infinitely) | ||
0, 1, 2, 3, 4 | [0..4] -> [0,1,2,3,4] | (use srfi-1) (iota 5) -> (0 1 2 3 4) | (range 5) | |
2, 3, 4, 5, 6 | [2..6] -> [2,3,4,5,6] | (use srfi-1) (iota 5 2) -> (2 3 4 5 6) | (range 2 7) | |
3, 5, 7, 9, 11 | [3,5..11] -> [3,5,7,9,11] | (use srfi-1) (iota 5 3 2) -> (3 5 7 9 11) | (range 3 12 2) -> (3 5 7 9 11) | |
(3, 1, 4, 1, 5) -> 14 | sum [3,1,4,1,5] foldl (+) 0 [3,1,4,1,5] | (apply + '(3 1 4 1 5)) (fold-right + 0 '(3 1 4 1 5)) | (apply #'+ '(3 1 4 1 5)) (reduce #'+ '(3 1 4 1 5)) | (apply + '(3 1 4 1 5)) (reduce + 0 '(3 1 4 1 5)) |
f 3 (1, 4, 1, 5) -> f(5, f(1, f(4, f(1, 3)))) | x2y x y = x^2 + y | (define (x2y x y) (+ (* x x) y)) | ||
f 3 (1, 4, 1, 5) -> f(f(f(f(3, 1), 4), 1) 5) | x2y x y = x^2 + y foldl x2y 3 [1,4,1,5] -> 117007494 | (defun x2y (x y) (+ (* x x) y)) (reduce #'x2y '(3 1 4 1 5)) | (defn x2y [x y] (+ (* x x) y)) (reduce x2y 3 '(1 4 1 5)) -> 117007494 | |
x f | iterate (2 *) 1 -> [1,2,4,8,16,... infinitely] | (iterate (partial * 2) 1) -> (1 2 4 8 16 ... infinitely) | ||
x -> (x, x, x, ...) | repeat 2 -> [2,2,2,2,2,... infinitely] | (repeat 2) -> (2 2 2 2 2 ... infinitely) | ||
Haskell | scheme | Common Lisp | Clojure | |
show contents of a file | (print (slurp "hoge.clj")) |
Processors
Haskell | Glasgow Haskell Compiler (GHC) Hugs 98 |
Scheme | Gauche GNU Guile Racket (PLT Scheme) MIT Scheme |
Common Lisp | Steeel Bank Common Lisp (SBCL) |
Clojure | Clojure |
Standard ML | Standard ML of New Jersey (SML/NJ) MLton SML# |
OCaml | OCaml (INRIA) |
F# | F# (Microsoft) |
Scala | Scala |
JavaScript | Node.js Rhino |
GHC | Gauche | CLISP | Clojure | Scala | |
---|---|---|---|---|---|
interpreper | ghci | gosh | clisp | java clojure.main | java scala.tools.nsc.MainGenericRunner (Use a bundled script instead) |
exit interpreter | :quit | (exit) | (exit) (quit) | ^D | exit :quit |
import a library | :m +Data.List | (use srfi-1) | (use 'clojure.java.io) | ||
load | :l hoge.hs | (load "./hoge.scm") | (load-file "hoge.clj") | :l hoge.scala | |
args | $ cat args.scm | ||||
multi line | :{ |