我是clojure的新手,并试图比较一个字符列表,我遇到了一些令人困惑的行为.当直接比较连接的字符串版本时,为什么难以(不可能?)比较字符列表的相等性?
(identical? (\A \T \C \G) (\A \T \C \G) )
; ClassCastException java.lang.Character cannot be cast to clojure.lang.IFn user/eval672
;(NO_SOURCE_FILE:1)
(identical? '(\A \T \C \G) '(\A \T \C \G) )
;false
;convert to string
(identical? "ATCG" "ATCG" )
;True
Run Code Online (Sandbox Code Playgroud)
Chi*_*ron 10
来自REPL:
user=> (doc identical?)
-------------------------
clojure.core/identical?
([x y])
Tests if 2 arguments are the same object
Run Code Online (Sandbox Code Playgroud)
如果您了解Java编程语言,那么相同吗?在处理引用时,它在Java中表现得像==运算符.
你可以试试这个:
(= '(\A \T \C \G) '(\A \T \C \G) )
=> true
Run Code Online (Sandbox Code Playgroud)
再次,在REPL中:
user=> (doc =)
-------------------------
clojure.core/=
([x] [x y] [x y & more])
Equality. Returns true if x equals y, false if not. Same as
Java x.equals(y) except it also works for nil, and compares
numbers and collections in a type-independent manner. Clojure's immutable data
structures define equals() (and thus =) as a value, not an identity,
comparison.
Run Code Online (Sandbox Code Playgroud)
所以不,比较Clojure中列表的相等性并不是不可能的,也绝对不难.REPL是你最好的朋友.
| 归档时间: |
|
| 查看次数: |
832 次 |
| 最近记录: |