2010-02-12 31 views
5

我想寫一個函數來檢查一個字符是否在某個十六進制範圍內。如何確定角色是否在Clojure的範圍內?

我想要的代碼如下所示:

(def current \s) 
(and (>= current (char 0x20)) (<= current (char 0xD7FF))) 

我得到以下錯誤:

java.lang.ClassCastException: java.lang.Character cannot be cast to 
java.lang.Number (NO_SOURCE_FILE:0) 

我假設,因爲> =運營​​商期望的數字,它試圖強制類型轉換它。在常規的Java,我只是做:

(current >= 0x20) && (current <= 0xD7FF) 

回答

8

明確地將其轉換爲int第一

(<= 0x20 (int current) 0xD7FF) 
3

字符數不Clojure中的數字,雖然它很容易將它們轉換成字符與int功能。

(number? \a)  => false 
(number? 42)  => true 
(number? (int \a)) => true 

對於強制轉換成基本類型,你可以使用該功能你想要的類型的名稱(見(find-doC#"Coerce"))。

(int (char 0x20)) 
(float ...) 
(map double [1 2 3 4]) 
.... 
相關問題