MAL文档
- 语法
- 读取器宏
- 特殊形式
- 内置符号
- 内置函数
- 算数运算
- 谓词
- 字符串
- 解释器
- 读取
- 求值
- 打印
- 原子
- 序列操作
- 字典
- 元数据
- 时间
- 异常
- FFI
- 标准库
- 符号
- 函数
- 宏
语法
- 空白符 所有的空白符会被忽略
,
逗号也会被忽略;
以分号起始的内容直到行尾都被视为注释- 符号
- 符号中不允许含有空白符及
[]{}()'",;`
诸特殊字符 - 符号亦不得以
~@^
诸特殊字符开头 - 以
:
开头的符号称为关键字,可以用作字典的键 - 按照惯例,
!
结尾的符号表示副作用,?
结尾的符号表示谓词 - 符号中可以使用unicode字符(包括emoji)
- 符号中不允许含有空白符及
- 数字
- 目前仅支持整数,尚不支持浮点数
- 字符串
- 有限的unicode支持
读取器宏
()
/[]
/{}
/""
列表/向量/字典/字符串'
/~
/~@
/`
引号/解引号/解引号并展平/反引号@
解原子引用^
元数据
特殊形式
Lisp “specials” (or “special atoms”) which means that they are language level features and more specifically that the rest of the list elements (arguments) may be evaluated differently (or not at all) unlike the default apply case where all elements of the list are evaluated before the first element is invoked. Lists which contain a “special” as the first element are known as “special forms”. They are special because they follow special evaluation rules.
def!
定义符号
定义(def! a b)
a
为b
,求值并返回b
The!
suffix on symbols is used to indicate that this symbol refers to a function that mutates something else.let*
定义块
在(let* (a1 b1 a2 b2 ...) body)
body
中,定义a1
为b1
,a2
为b2
,… 且后方绑定可以引用前方绑定。
最终求值并返回body
.do
顺序执行
顺序执行(do a1 a2 ... an)
a1 a2 ...
,最终求值并返回an
.if
条件分支
如果(if cond true_clause) (if cond true_clause false_clause)
cond
为nil
或false
以外的其他值,求值并返回true_clause
,否则求值并返回false_clause
(如果存在)或nil
.fn*
匿名函数 / 闭包
以(fn* params body) (fn* (p1 p2 & rest) body)
params
为参数,body
为函数体创建闭包并返回。可被直接调用或使用def!
使其具名。&
之后的参数将绑定到所有未绑定的实参。quote
/'
引号quasiquote
/`
反引号unquote
/~
解引号splice-unquote
/~@
解引号并展平(def! lst (quote (b c))) ;-> (b c) (quasiquote (a lst d)) ;-> (a lst d) (quasiquote (a (unquote lst) d)) ;-> (a (b c) d) (quasiquote (a (splice-unquote lst) d)) ;-> (a b c d)
quasiquoteexpand
展开反引号
展开反引号至求值前的状态defmacro!
定义宏(defmacro! a b)
macroexpand
展开宏
完全展开宏至求值前的状态try*
/catch*
异常处理(try* a (catch* e b))
内置符号
true
/false
/nil
*ARGV*
参数列表
内置函数
算数运算
+
/-
/*
//
以运算符连接诸参数并返回求值结果。
对空参数列表,+
/-
返回0,*
//
返回1.
对单参数,+
/*
返回其自身,-
返回其相反数,/
返回其倒数。=
/<
/<=
/>
/>=
除=
外,其余比较运算仅对数字进行比较。
=
可对任意对象进行比较。
谓词
nil?
/true?
/false?
/symbol?
/atom?
/keyword?
/vector?
/sequential?
/map?
/list?
/empty?
/contains?
/fn?
/string?
/number?
/macro?
字符串
str
转普通字符串
将诸参数转换为字符串,返回诸字符串拼接的结果,不转义特殊字符。(str (list 1 2 "abc" "\"") "def") ;=> "(1 2 abc \")def"
pr-str
转机读字符串
将诸参数转换为字符串,返回诸字符串以空格拼接的结果,转义特殊字符。(pr-str (list 1 2 "abc" "\"") "def") ;=> "(1 2 \"abc\" \"\\\"\") \"def\""
symbol
从字符串创建符号
解释器
读取
read-string
读字符串
读入机读字符串,返回内部表示(AST).(read-string "(1 2 (3 4) nil)") ;=>(1 2 (3 4) nil)(= nil (read-string "nil")) ;=>true(read-string "(+ 2 3)") ;=>(+ 2 3)(read-string "\"\n\"") ;=>"\n"(read-string "7 ;; comment") ;=>7
slurp
读文件内容
读入指定文件,将文件内容返回为字符串。readline
读取键盘输入
输出提示符,并返回用户输入的字符串,如果读到EOF,返回nil
.
求值
eval
求值
对内部表示(AST)进行求值并返回。(eval (read-string "(+ 2 3)")) ;=>5
打印
println
普通打印
不转义特殊字符并打印诸参数,返回(println (list 1 2 "abc" "\"") "def") ; (1 2 abc ") def ;=> nil
nil
.prn
按原样打印
转义特殊字符并打印诸参数,返回(prn (list 1 2 "abc" "\"") "def") ; (1 2 "abc" "\"") "def" ;=> nil
nil
.
对机读字符串进行普通打印相当于对原字符串按原样打印,即println ∘ pr-str = prn
.
原子
atom
创建原子deref
/@
解原子引用reset!
重置原子并返回重置值swap!
更新原子并返回更新值(swap! myatom (fn* [x] (+ 1 x)))
序列操作
cons
将元素追加至序列头,返回列表concat
拼接多个序列,返回列表list
/()
从参数创建列表vector
/[]
从参数创建向量vec
转换序列到向量count
返回序列元素个数nth
返回序列的第n个元素first
返回序列的首元素rest
返回除去首元素后的序列apply
以序列为参数调用函数(apply + 4 (list 5)) ;=>9
apply
至少需要两个参数,第一个为被调用函数,最后一个为序列,其他参数将与序列拼接,并以拼接结果调用函数。map
对序列中的每一个元素应用函数并返回结果组成的序列conj
向列表头或向量尾追加多个元素seq
将序列或字符串转换为列表
字典
keyword
/:
从字符串创建键hash-map
/{}
创建字典assoc
向字典中追加键/值(assoc {:a 1 :b 2} :a 3 :c 1)
dissoc
从字典中删除键及其对应的值(dissoc hm3 "a" "b" "c")
get
从字典中查找键对应的值keys
返回键的列表vals
返回值的列表
元数据
with-meta
/^
为函数/序列/字典追加元数据meta
返回函数/序列/字典的元数据
时间
time-ms
返回自epoch(00:00:00 UTC January 1, 1970)以来经过的毫秒数
异常
throw
抛出异常
FFI
c-eval
对C语句求值
标准库
符号
*host-language*
宿主语言
函数
not
对谓词取反load-file
加载并求值文件内容
宏
cond
多条件分支语句
展开为多个if
级联。