Hyperpolyglot Lisp:Common Lisp、Racket、Clojure、Emacs Lisp
摘要
一份对照参考表,比较Common Lisp、Racket、Clojure和Emacs Lisp的语法与特性。
<p><a href="https://lobste.rs/s/dg2sx7/hyperpolyglot_lisp_common_lisp_racket">评论</a></p>
查看缓存全文
缓存时间: 2026/05/17 05:20
# Lisp: Common Lisp, Racket, Clojure, Emacs Lisp 来源: https://hyperpolyglot.org/lisp
*并排参考表*
语法与执行 (https://hyperpolyglot.org/lisp#grammar-execution) | 变量与表达式 (https://hyperpolyglot.org/lisp#var-expr) | 算术与逻辑 (https://hyperpolyglot.org/lisp#arithmetic-logic) | 字符串 (https://hyperpolyglot.org/lisp#strings) | 正则表达式 (https://hyperpolyglot.org/lisp#regexes) | 日期与时间 (https://hyperpolyglot.org/lisp#dates-time) | 列表 (https://hyperpolyglot.org/lisp#lists) | 固定长度数组 (https://hyperpolyglot.org/lisp#fixed-length-arrays) | 字典 (https://hyperpolyglot.org/lisp#dictionaries) | 用户定义类型 (https://hyperpolyglot.org/lisp#user-defined-types) | 函数 (https://hyperpolyglot.org/lisp#functions) | 执行控制 (https://hyperpolyglot.org/lisp#execution-control) | 异常 (https://hyperpolyglot.org/lisp#exceptions) | 流 (https://hyperpolyglot.org/lisp#streams) | Emacs 缓冲区 (https://hyperpolyglot.org/lisp#emacs-buffers) | 文件 (https://hyperpolyglot.org/lisp#files) | 目录 (https://hyperpolyglot.org/lisp#directories) | 进程与环境 (https://hyperpolyglot.org/lisp#processes-environment) | 库与命名空间 (https://hyperpolyglot.org/lisp#libraries-namespaces) | 对象 (https://hyperpolyglot.org/lisp#objects) | Lisp 宏 (https://hyperpolyglot.org/lisp#lisp-macros) | 反射 (https://hyperpolyglot.org/lisp#reflection) | Java 互操作 (https://hyperpolyglot.org/lisp#java-interop)
common lisp (https://hyperpolyglot.org/lisp#common-lisp) | racket (https://hyperpolyglot.org/lisp#racket) | clojure (https://hyperpolyglot.org/lisp#clojure) | emacs lisp (https://hyperpolyglot.org/lisp#emacs-lisp)
使用的版本 (https://hyperpolyglot.org/lisp#version-used-note)
*SBCL 1.2* | *Racket 6.1* | *Clojure 1.6* | *Emacs 24.5*
显示版本 (https://hyperpolyglot.org/lisp#show-version-note)
$ sbcl --version | $ racket --version | *启动时 REPL 显示* | $ emacs --version
语法与执行 (https://hyperpolyglot.org/lisp#grammar-execution-note)
common lisp | racket | clojure | emacs lisp
编译器 (https://hyperpolyglot.org/lisp#compiler-note)
$ raco make *模块*.rkt | M-x byte-compile-file
独立可执行文件 (https://hyperpolyglot.org/lisp#standalone-executable-note)
(sb-ext:save-lisp-and-die "*可执行文件*" :executable t :toplevel '*函数*) | $ mzc —exe *可执行文件* *文件* | *指定完整的 clojure jar 路径:* java -cp clojure.jar clojure.main foo.clj
解释器 (https://hyperpolyglot.org/lisp#interpreter-note)
$ sbcl --script foo.lisp | $ racket -r foo.racket | *指定完整的 clojure jar 路径:* java -cp clojure.jar clojure.main foo.clj | *shebang 行 (https://hyperpolyglot.org/lisp#shebang-note)*
#!/usr/bin/env sbcl --script | #!/usr/bin/env racket --script | *指定完整的 clojure jar 路径:* #!/usr/bin/env java -jar clojure.jar | #!/usr/bin/env emacs --script
REPL (https://hyperpolyglot.org/lisp#repl-note)
$ sbcl | $ racket | $ java -jar /PATH/TO/clojure.jar | M-x ielm
命令行程序 (https://hyperpolyglot.org/lisp#cmd-line-program-note)
$ racket -e '(+ 1 1)'
单词分隔符 (https://hyperpolyglot.org/lisp#word-separator-note)
*空白字符* | *空白字符* | *空白字符和逗号* | *空白字符*
行末注释 (https://hyperpolyglot.org/lisp#eol-comment-note)
(+ 1 1) ; 加法 | (+ 1 1) ; 加法 | (+ 1 1) ; 加法 | (+ 1 1) ; 加法
多行注释 (https://hyperpolyglot.org/lisp#multiple-line-comment-note)
(+ 1 #| 加法 |# 1) | (+ 1 #| 加法 |# 1)
变量与表达式 (https://hyperpolyglot.org/lisp#var-expr-note)
common lisp | racket | clojure | emacs lisp
标识符 (https://hyperpolyglot.org/lisp#id-note)
*不区分大小写,不能以数字开头* | *排除的字符:* SP ( ) " , ' ` ; # | \ | *保留给用户宏:* ? ! [ ] { } | *区分大小写,不能以数字开头* | *排除的字符:* SP ( ) [ ] { } " , ' ` ; # | \ | *区分大小写,不能以数字开头* | *允许的字符:* A-Z a-z 0-9 * + ! - _ ? | *这些有特殊含义或保留:* / . : | *区分大小写,不能以数字开头* | *排除的字符:* SP ( ) " , ' ` ; # | \ _ [ ]
带引号的标识符 (https://hyperpolyglot.org/lisp#quoted-id-note)
*以及转义标识符*
(setq |white space symbol| 3) (setq white\ space\ symbol 3) | (define |white space symbol| 3) (define white\ space\ symbol 3) | *无* | *无* | *无* | (setq white\ space\ symbol 3)
局部变量 (https://hyperpolyglot.org/lisp#local-var-note)
; 并行赋值: (let ((x 3) (y 4)) (+ x y))
; 顺序赋值: (let* ((x 3) (y (* x x))) (+ x y)) | ; 并行赋值: (let ((x 3) (y 4)) (+ x y))
; 顺序赋值: (let* ((x 3) (y (* x x))) (+ x y)) | (let [x 3 y 4] (+ x y)) (let [[x y] [3 4]] (+ x y)) (let [x 3 y (* x x)] (+ x y)) | ; 并行赋值: (lexical-let ((x 3) (y 4)) (+ x y))
(lexical-let* ((x 3) (y (* x x))) (+ x y))
全局变量 (https://hyperpolyglot.org/lisp#global-var-note)
(defparameter *x* 3) ; 如果已设置则不变: (defvar *x* 3) | (define x 3) ; y 不是全局的: (define (double z) (define y 2) (* y z)) | (def x 3) (set 'x 3) | (setq x 3)
移除变量 (https://hyperpolyglot.org/lisp#rm-var-note)
(makunbound 'x) | (namespace-undefine-variable! 'x) | (ns-unmap *ns* 'x) | (makunbound 'x)
空值 (https://hyperpolyglot.org/lisp#null-note)
nil '() null '() | ; 与 Java 中的 null 值相同: nil | nil '() | null
空值测试 (https://hyperpolyglot.org/lisp#null-test-note)
(null x) | (null? x) | (nil? x) | (null x)
标识符作为值 (https://hyperpolyglot.org/lisp#id-as-val-note)
'x (quote x) | 'x (quote x) | 'x (quote x) | 'x (quote x)
标识符测试 (https://hyperpolyglot.org/lisp#id-test-note)
(symbolp 'x) | (symbol? 'x) | (symbol? 'x) | (symbolp 'x)
标识符相等性测试
(eq 'x 'x) | (eq? 'x 'x) | (= 'x 'x) | (eq 'x 'x)
非引用标识符 (https://hyperpolyglot.org/lisp#non-referential-id-note)
:foo | #:foo | :foo | :foo
标识符属性 (https://hyperpolyglot.org/lisp#id-attr-note)
*设置、获取、移除*
(set 'x 13) (setf (get 'x :desc) "unlucky") (get 'x :desc) (remprop 'x :desc) | *无* | ; 值必须是 clojure.lang.IObj 的实例: (def x (with-meta [13] {:desc "unlucky"})) (get (meta x) :desc) | ; 无 | (set 'x 13) (setf (get 'x :desc) "unlucky") (get 'x :desc) (remprop 'x :desc)
算术与逻辑 (https://hyperpolyglot.org/lisp#arithmetic-logic-note)
common lisp | racket | clojure | emacs lisp
真与假 (https://hyperpolyglot.org/lisp#true-false-note)
t nil | #t #f | true false | true false | t nil
假值 (https://hyperpolyglot.org/lisp#falsehoods-note)
nil () | #f | false | nil | false nil | nil ()
逻辑运算符 (https://hyperpolyglot.org/lisp#logical-op-note)
(or (not t) (and t nil)) | (or (not #t) (and #t #f)) | (or (not true) (and true false)) | (or (not t) (and t nil))
关系运算符 (https://hyperpolyglot.org/lisp#relational-op-note)
= /= < > <= >= | = < > <= >= | = | not= < > <= >= | = /= < > <= >=
最小值与最大值 (https://hyperpolyglot.org/lisp#min-max-note)
(min 1 2 3) (max 1 2 3) | (min 1 2 3) (max 1 2 3) | (min 1 2 3) (max 1 2 3) | (min 1 2 3) (max 1 2 3)
数值谓词 (https://hyperpolyglot.org/lisp#num-predicates-note)
numberp integerp rationalp floatp realp complexp | number? integer? rational? inexact? real? complex? | number? integer? rational? float? *无* | *无* | numberp integerp *无* floatp *无* *无*
算术运算符 (https://hyperpolyglot.org/lisp#arith-op-note)
+ - * / mod | + - * / modulo | + - * / mod | + - * / %
整数除法 (https://hyperpolyglot.org/lisp#int-div-note)
*以及余数*
(truncate 7 3) (rem 7 3) | (quotient 7 3) (remainder 7 3) | (quot 7 3) (rem 7 3) | (/ 7 3) (% 7 3)
整数除以零 (https://hyperpolyglot.org/lisp#int-div-zero-note)
division-by-zero *错误* | division by zero *错误* | arith-error | *错误*
浮点数除法 (https://hyperpolyglot.org/lisp#float-div-note)
*有理数:* (/ 7 3) | *浮点数:* (/ 7 (* 3 1.0)) | *有理数:* (/ 7 3) | *浮点数:* (/ 7 (float 3)) | *有理数:* (/ 7 3) | *浮点数:* (/ 7 (* 3 1.0)) | *整数商:* (/ 7 3) | *浮点数:* (/ 7 (* 3 1.0))
浮点数除以零 (https://hyperpolyglot.org/lisp#float-div-zero-note)
division-by-zero *错误* | -1.0e+INF, -0.0e+NaN, *或* 1.0e+INF | *错误*
幂 (https://hyperpolyglot.org/lisp#power-note)
(expt 2 32) | (expt 2 32) | *返回浮点数:* (Math/pow 2 32) | (expt 2 32)
平方根 (https://hyperpolyglot.org/lisp#sqrt-note)
(sqrt 2) | (sqrt 2) | (Math/sqrt 2) | (sqrt 2)
平方根 -1 (https://hyperpolyglot.org/lisp#sqrt-negative-one)
#c(0.0 1.0) | 0+1i | *(Math/sqrt -1):* NaN -0.0e+NaN | *错误*
超越函数 (https://hyperpolyglot.org/lisp#transcendental-func-note)
exp log sin cos tan asin acos atan atan | exp log sin cos tan asin acos atan atan | Math/exp Math/log Math/sin Math/cos Math/tan Math/asin Math/acos Math/atan Math/atan2 | exp log sin cos tan asin acos atan atan
浮点数截断 (https://hyperpolyglot.org/lisp#float-truncation-note)
*返回两个值,第一个是整数:* truncate round ceiling floor | *返回浮点数:* truncate round ceiling floor | *返回整数:* int Math/round | *返回浮点数:* Math/ceil Math/floor | truncate round ceiling floor fround fceiling ffloor truncate | *返回整数*
绝对值 (https://hyperpolyglot.org/lisp#abs-val-note)
*以及符号函数*
abs signum | abs *racket:* sgn | Math/abs Math/signum | abs signum
整数溢出 (https://hyperpolyglot.org/lisp#int-overflow-note)
*无;任意精度整数* | *无;任意精度整数* | clojure.lang.Numbers.throwIntOverflow *异常* | *错误*
浮点数溢出 (https://hyperpolyglot.org/lisp#float-overflow-note)
floating-point-overflow *错误* | *不是字面量:* -Infity NaN Infinity | *错误*
有理数构造 (https://hyperpolyglot.org/lisp#rational-construction-note)
(/ 3 7);字面量:3/7 | (/ 3 7);字面量:3/7;也是有理数:2.718 (exp 1) | (/ 3 7);字面量:3/7 | *无*
有理数分解 (https://hyperpolyglot.org/lisp#rational-decomposition-note)
(numerator 3/7) (denominator 3/7) | (numerator 3/7) (denominator 3/7) | (numerator 3/7) (denominator 3/7) | *无* *无*
复数构造 (https://hyperpolyglot.org/lisp#complex-construction-note)
#c(1 2) | 1+2i (+ 1 +2i) | *无* *无* | *无*
复数分解 (https://hyperpolyglot.org/lisp#complex-decomposition-note)
(realpart #c(1 2)) (imagpart #c(1 2)) (phase #c(1 2)) (abs #c(1 2)) (conjugate #c(1 2)) | (real-part 1+2i) (imag-part 1+2i) (angle 1+2i) (magnitude 1+2i) (conjugate 1+2i) | *无* *无* *无* *无* | *无*
随机数 (https://hyperpolyglot.org/lisp#random-num-note)
*均匀整数,均匀浮点数,正态浮点数*
(random 100) (random 1.0) *无* | (random 100) (random) *无* | (def rnd (java.util.Random.)) (.nextInt rnd 100) (.nextFloat rnd) (.nextGaussian rnd) | (random 100) *无* *无*
随机数种子 (https://hyperpolyglot.org/lisp#random-seed-note)
(setq *random-state* (sb-ext:seed-random-state 17)) | (random-seed 17) | *无* | *无*
位运算符 (https://hyperpolyglot.org/lisp#bit-op-note)
ash *左移当第二个参数为正时* logand logior logxor lognot | arithmetic-shift *左移当第二个参数为正时* bitwise-and bitwise-ior bitwise-xor bitwise-not | bit-shift-left bit-shift-right bit-and bit-or bit-xor bit-not | lsh *左移当第二个参数为正时* logand logior logxor lognot
二进制、八进制和十六进制字面量 (https://hyperpolyglot.org/lisp#binary-octal-hex-literals-note)
#b101010 #o52 #x2a | #b101010 #o52 #x2a | *无* | *无*
基数 (https://hyperpolyglot.org/lisp#radix-note)
(format nil "~7r" 42) | *无* | *无* | *无*
字符串 (https://hyperpolyglot.org/lisp#strings-note)
common lisp | racket | clojure | emacs lisp
字符串测试 (https://hyperpolyglot.org/lisp#str-test-note)
(stringp "foo") | (string? "foo") | (string? "foo") | (stringp "foo")
字符串字面量 (https://hyperpolyglot.org/lisp#str-litera-notel)
"foo bar" | "foo bar" | "foo bar" | "foo bar"
字面量中的换行 (https://hyperpolyglot.org/lisp#newline-in-str-literal-note)
*是* | *是* | *是* | *是*
字面量转义 (https://hyperpolyglot.org/lisp#str-literal-esc-note)
\" \\ \t \n \r \u*hhhh* \b \t \n \f \r | \" \\ \t \n \r \u*hhhh* \b \t \n \f \r | \" \\ \t \n \r \u*hhhh* \x*h* \x*hhhhhh* \C-*x* \M-*x* | ?a ?\b ?\t ?\n ?\f ?\r ?\" ?\\ ?\*ooo* ?\u*hhhh* ?\x*h* ?\x*hhhhhh* ?\C-*x* ?\M-*x*
构造函数 (https://hyperpolyglot.org/lisp#str-ctor-note)
(string #\f #\o #\o) | (string ?f ?o ?o) | *无* | *无*
格式化字符串 (https://hyperpolyglot.org/lisp#fmt-str-note)
(format nil "~a: ~a ~,2f" "Foo" 7 13.457) | (format "~a ~a ~a" "Foo" 7 13.457) | (String/format "%s: %d %.2f" (to-array ["Foo" 7 13.457])) | (format "%s: %d %.2f" "Foo" 7 13.457)
格式说明符 (https://hyperpolyglot.org/lisp#fmt-specifiers-note)
~a 任何类型,人类可读 ~s 任何时间,读取可解析 ~% 换行符 ~~ 波浪号 ~c 字符 ~,5f 小数点右5位 ~d 十进制 ~x 十六进制 ~o 八进制 ~b 二进制 | ~a 任何类型,人类可读 ~s 任何时间,读取可解析 ~% 换行符 ~~ 波浪号 ~c 字符 ~d 十进制 ~x 十六进制 ~o 八进制 ~b 二进制 | *无* | *无*
比较字符串 (https://hyperpolyglot.org/lisp#compare-str-note)
(string= "foo" "bar") (string< "foo" "bar") | (string=? "foo" "bar") (string<? "foo" "bar") | (= "foo" "bar") (.equals "foo" "bar") | (string= "foo" "bar") (string< "foo" "bar")
连接字符串 (https://hyperpolyglot.org/lisp#str-concat-note)
(concatenate 'string "Value: " (write-to-string 8)) | (string-append "Value: " (number->string 8)) | (str "Value: " 8) | (concat "value: " (number-to-string 8))
字符串转数字 (https://hyperpolyglot.org/lisp#str-to-num-note)
(+ 7 (parse-integer "12")) (+ 73.9 (read-from-string ".037")) | (+ 7 (string->number "12")) (+ 73.9 (string->number ".037")) | (+ 7 (Integer/parseInt "12")) (+ 73.9 (Float/parseFloat ".037")) | (+ 7 (string-to-number "12")) (+ 73.9 (string-to-number ".037"))
分割 (https://hyperpolyglot.org/lisp#split-note)
(cl-ppcre:split "[ \\t\\n]+" "foo bar baz") | (regexp-split #rx"[ \\n\\t]+" "foo bar baz") | (seq (.split "foo bar baz" "[ \\t\\n]+")) | (split-string "foo bar baz")
字符串连接 (https://hyperpolyglot.org/lisp#str-join-note)
(reduce (lambda (m o) (concatenate 'string m " " o)) '("foo" "bar" "baz")) | (string-join '("foo" "bar" "baz") " ") | (reduce #(str %1 " " %2) '("foo" "bar" "baz")) | (reduce (lambda (m o) (concat m " " o)) '("foo" "bar" "baz"))
长度 (https://hyperpolyglot.org/lisp#str-len-note)
(length "foo") | (string-length "foo") | (.length "foo") | (length "foo")
子串索引 (https://hyperpolyglot.org/lisp#index-substr-note)
(search "bar" "foo bar") | *racket:* (require srfi/13/string) (string-contains "foo bar" "bar") | (.indexOf "foo bar" "bar") | (search "bar" "foo bar")
提取子串 (https://hyperpolyglot.org/lisp#extract-substr-note)
(subseq "foo bar" 4 7) | (substring "foo bar" 4 7) | (.substring "foo bar" 4 7) | (substring "foo bar" 4 7)
字符字面量 (https://hyperpolyglot.org/lisp#char-literal-note)
#\a #\space #\newline #\backspace #\tab #\linefeed #\page #\return #\rubout | #\a #\space #\newline #\backspace #\tab #\linefeed #\page #\return #\nul #\vtab #\alarm #\esc #\delete | *不在 racket 中:* #\alarm #\esc #\delete | \\a \\newline \\space \\backspace \\tab *?* \\formfeed \\return *?* | ?a ?\b ?\t ?\n ?\f ?\r ?\" ?\\ ?\*ooo* ?\u*hhhh* ?\x*h* ?\x*hhhhhh* ?\C-*x* ?\M-*x*
测试字符 (https://hyperpolyglot.org/lisp#char-test-note)
(characterp #\x) (alpha-char-p #\x) (alphanumericp #\x) (digit-char-p #\7) (lower-case-p #\x) (upper-case-p #\X) | (char? #\x) | (char? \\x) | (characterp ?x)
Chr 和 Ord (https://hyperpolyglot.org/lisp#chr-ord-note)
(code-char 97) (char-code #\a) | (integer->char 97) (char->integer #\a) | (char 97) (int \\a) | *无*
转为字符数组 (https://hyperpolyglot.org/lisp#str-to-char-array-note)
; 列表: (string->list "foo") | *无* | *无* | *无*
查找字符 (https://hyperpolyglot.org/lisp#lookup-char-note)
(char "foo" 0) | (string-ref "foo" 0) | (.charAt "foo" 0) | (aref "foo" 0)
正则表达式 (https://hyperpolyglot.org/lisp#regexes-note)
common lisp | racket | clojure | emacs lisp
字面量 (https://hyperpolyglot.org/lisp#regex-literal-note)
*使用字符串:* "\\b\\d\{5\}\\b" | *posix 扩展:* #rx"^[0-9][0-9][0-9][0-9]
相似文章
在浏览器中试用 LispE
关于在浏览器中试用 LispE(一种 Lisp 方言)的简要介绍或链接。
Common Lisp 可移植性库状态
Common Lisp 可移植性库的全面状态概览,展示了不同 Common Lisp 实现之间的兼容性百分比。
推荐一下……理解 Emacs 的模式
文章解释了 Emacs 的架构模式,重点介绍了通用缓冲区数据模型和增量补全读取(ICR),并将其比作玫瑰的根与花瓣。文章强调了 Emacs 如何统一界面并通过 Elisp 实现可扩展性。
JavaScript 精简
LispE 是 NAVER 开发的一个紧凑的 Lisp 方言,它结合了函数式和数组语言特性,并支持 PyTorch 和 llama.cpp 等 AI 库。
将 Python 转译为 Lisp
LispE 是 NAVER 推出的一款开源 Lisp 方言,兼具函数式与数组编程特性,并支持 PyTorch、llama.cpp 以及 MLX 等 AI 库。该语言既可作为原生应用运行,也可打包为支持多线程与现代函数式编程特性的 WebAssembly 库。