2014-02-17 47 views
0

這是Lisp的第一天給我。我試圖最終寫一個if else語句...希望今年某個時候。我不知道爲什麼這給我一個錯誤?爲什麼這個小小的Lisp表達式不起作用?

(cond (< 1 2) (print "hey")) 

爲什麼會崩潰?它說變量'<'沒有綁定?我根本沒有得到Lisp ...提前致謝。

+3

「我根本沒有得到List」 - 不是一個好兆頭。 – duffymo

+1

「我試圖最終寫一個if else語句。」即使在你提供的代碼中,它看起來並不像你正在試圖寫一個if(then)-else;它看起來像你試圖寫if-then。你可能想要'(if(<1 2)(print'hey「))'(其中有一個隱含的'nil'或'(when(<1 2)(print」hey「))',它更清楚你不關心其他部分,或'(cond((<1 2)(print「hey」)))'。 –

回答

4

cond需要的測試和條款

(cond (<test> <if test is true>) 
     (<test2> <if test2 is true>) 
     ...) 

的清單,我認爲你的意思寫的是

(cond ((< 1 2) (print "hey"))) ;; if 1 is less than 2, print "hey" 

什麼,你實際上已經在你的問題得到的是

(cond (< 1 2)  ;; if `<` is bound as a variable, return 2 
     (print "hey")) ;; if `print` is bound as a variable, return "hey" 

默認情況下,這些符號都不是在變量名稱空間中定義的,所以您會得到錯誤。

如果您只有一個表單要發送,並且只想要做某些事情,那麼使用whencond更常見。

(when (< 1 2) (print "hey")) 
相關問題