基本数据类型 #
一、数据类型概述 #
Clojure是动态类型语言,数据类型在运行时确定。Clojure提供了丰富的数据类型,同时与Java类型系统无缝集成。
1.1 类型分类 #
| 类别 | 类型 | 示例 |
|---|---|---|
| 数字 | 整数、浮点、比率、大数 | 42, 3.14, 1/3 |
| 布尔 | true、false | true, false |
| 空值 | nil | nil |
| 字符串 | String | "hello" |
| 字符 | Character | \a, \newline |
| 关键字 | Keyword | :name |
| 符号 | Symbol | x, my-var |
二、数字类型 #
2.1 整数 #
clojure
42
-17
0
2r1010
8r77
16rFF
36rZ
整数类型:
| 类型 | 范围 | 示例 |
|---|---|---|
| Long | -2^63 到 2^63-1 | 42 |
| BigInt | 任意精度 | 42N |
clojure
(class 42)
(class 42N)
2.2 浮点数 #
clojure
3.14
-0.5
1.0e10
6.022e23
浮点类型:
| 类型 | 说明 | 示例 |
|---|---|---|
| Double | 64位浮点 | 3.14 |
| BigDecimal | 任意精度 | 3.14M |
clojure
(class 3.14)
(class 3.14M)
2.3 比率 #
Clojure原生支持有理数:
clojure
1/3
22/7
(/ 1 3)
(+ 1/3 1/4)
clojure
(class 1/3)
比率运算保持精度:
clojure
(+ 1/3 1/3 1/3)
(* 1/3 3)
2.4 算术运算 #
clojure
(+ 1 2 3)
(- 10 3)
(* 2 3 4)
(/ 10 2)
(/ 10 3)
(quot 10 3)
(rem 10 3)
(mod 10 3)
(inc 5)
(dec 5)
2.5 数学函数 #
clojure
(max 1 5 3 2)
(min 1 5 3 2)
(abs -5)
(Math/pow 2 10)
(Math/sqrt 16)
(Math/round 3.7)
2.6 比较运算 #
clojure
(= 1 1)
(= 1 1.0)
(== 1 1.0)
(not= 1 2)
(< 1 2 3)
(<= 1 1 2)
(> 3 2 1)
(>= 3 3 2)
三、布尔类型 #
3.1 布尔值 #
clojure
true
false
(class true)
3.2 逻辑运算 #
clojure
(and true true)
(and true false)
(and true true true)
(or false true)
(or false false)
(not true)
(not false)
3.3 真值判断 #
在Clojure中,只有 false 和 nil 是逻辑假:
clojure
(boolean true)
(boolean false)
(boolean nil)
(boolean 0)
(boolean "")
(boolean [])
(boolean "hello")
3.4 条件判断函数 #
clojure
(true? true)
(true? false)
(false? false)
(false? nil)
(nil? nil)
(nil? false)
(some? nil)
(some? "hello")
四、nil类型 #
4.1 nil的含义 #
nil 表示"无值"或"空":
clojure
nil
(class nil)
4.2 nil的使用场景 #
clojure
(defn find-user [id]
(if (= id 1)
{:id 1 :name "Alice"}
nil))
(find-user 1)
(find-user 2)
4.3 nil处理 #
clojure
(nil? nil)
(some? nil)
(if nil "yes" "no")
(when nil "yes")
4.4 nil与空集合 #
clojure
(empty? nil)
(empty? [])
(empty? "")
(seq nil)
(seq [])
五、字符串类型 #
5.1 字符串字面量 #
clojure
"Hello, World!"
"Line 1\nLine 2"
"Tab\there"
"Quote: \"Hello\""
"Backslash: \\"
clojure
(class "hello")
5.2 字符串操作 #
clojure
(str "Hello" " " "World")
(str 42)
(str :keyword)
(str nil)
(count "hello")
(subs "hello world" 0 5)
(subs "hello world" 6)
5.3 字符串函数 #
clojure
(require '[clojure.string :as str])
(str/upper-case "hello")
(str/lower-case "HELLO")
(str/capitalize "hello world")
(str/trim " hello ")
(str/triml " hello ")
(str/trimr " hello ")
(str/split "a,b,c" #",")
(str/join ["a" "b" "c"])
(str/join "," ["a" "b" "c"])
(str/replace "hello world" "world" "Clojure")
(str/replace "hello 123" #"\d+" "NUM")
5.4 字符串判断 #
clojure
(str/starts-with? "hello world" "hello")
(str/ends-with? "hello.txt" ".txt")
(str/includes? "hello world" "wor")
(str/blank? "")
(str/blank? " ")
(str/blank? nil)
六、字符类型 #
6.1 字符字面量 #
clojure
\a
\A
\newline
\space
\tab
\u03A9
\o41
clojure
(class \a)
6.2 字符操作 #
clojure
(int \a)
(char 97)
(str \a \b \c)
(char-escape-string \newline)
6.3 字符判断 #
clojure
(Character/isDigit \5)
(Character/isLetter \a)
(Character/isWhitespace \space)
(Character/isUpperCase \A)
(Character/isLowerCase \a)
七、类型判断 #
7.1 class函数 #
clojure
(class 42)
(class 3.14)
(class "hello")
(class :keyword)
(class [1 2 3])
(class {:a 1})
7.2 type函数 #
clojure
(type 42)
(type 42N)
(type 3.14M)
7.3 类型谓词 #
clojure
(integer? 42)
(integer? 3.14)
(float? 3.14)
(ratio? 1/3)
(number? 42)
(number? "42")
(string? "hello")
(char? \a)
(boolean? true)
(nil? nil)
(keyword? :name)
(symbol? 'x)
(coll? [1 2 3])
(list? '(1 2 3))
(vector? [1 2 3])
(map? {:a 1})
(set? #{1 2 3})
八、类型转换 #
8.1 数字转换 #
clojure
(int 3.14)
(long 3.14)
(float 42)
(double 42)
(bigint 42)
(bigdec 3.14)
(num 42)
(rationalize 3.5)
8.2 字符串转换 #
clojure
(str 42)
(str 3.14)
(str :keyword)
(str \a \b \c)
(format "Hello, %s! You have %d messages." "Alice" 5)
8.3 解析字符串 #
clojure
(Integer/parseInt "42")
(Long/parseLong "42")
(Float/parseFloat "3.14")
(Double/parseDouble "3.14")
(BigDecimal. "3.14159265359")
(BigInteger. "12345678901234567890")
九、相等性 #
9.1 = vs == #
clojure
(= 1 1)
(= 1 1.0)
(== 1 1.0)
(= "hello" "hello")
(= [1 2 3] [1 2 3])
(= {:a 1} {:a 1})
(identical? "hello" "hello")
9.2 值相等 #
Clojure的 = 比较值相等:
clojure
(= [1 2 3] [1 2 3])
(= {:a 1 :b 2} {:b 2 :a 1})
(= #{1 2 3} #{3 2 1})
9.3 引用相等 #
clojure
(identical? 1 1)
(let [v [1 2 3]]
(identical? v v))
十、总结 #
Clojure基本数据类型要点:
| 类型 | 特点 |
|---|---|
| 数字 | 支持整数、浮点、比率、大数 |
| 布尔 | 只有true和false |
| nil | 表示空值,逻辑假 |
| 字符串 | Java String,不可变 |
| 字符 | Java Character |
类型判断常用函数:
class/type:获取类型integer?、string?等:类型谓词=:值相等==:数字相等
下一步,让我们学习关键字与符号!
最后更新:2026-03-27