2
我正在學習clojure,並且遇到了似乎與我的代碼無關的NullPointerException。該程序在生成錯誤之前運行完成。代碼:簡單clojure程序後發生NullPointerException
; solves the collatz conjecture
; return one step in the sequence
(defn collatz-step [n]
(if (= (rem n 2) 0)
(/ n 2)
(+ 1 (* 3 n))))
; recurse over all numbers
(defn collatz [n]
(if (= n 1)
(println "All done!")
((println (format "N = %d" n))
(collatz (collatz-step n)))))
; get input and run it
(println "Enter a positive number:")
(collatz (read-string (read-line)))
有什麼我失蹤了嗎?
哦男人的較大值吹堆棧,非常感謝。這是完全合理的。我認爲要分組多個陳述,我必須在括號中列出它們。現在我瞭解瞭解決這個問題的(do)形式。 我這麼長時間都在b my我的頭,再次感謝! – 2013-03-27 00:41:35
函數自動添加do,所以在這種情況下你不需要它。函數中的最後一個形式是返回值 – 2013-03-27 01:07:52