我正在寫,其基於起始號碼在Collatz鏈的功能,但我已經遇到意想不到的問題
下面的代碼:
-- original, works
collatzA :: Integer -> [Integer]
collatzA 1 = [1]
collatzA n
| even n = n:collatzA (n `div` 2)
| odd n = n:collatzA (n * 3 + 1)
-- what I'm trying to do, won't compile, gives nasty errors
collatzB :: Integer -> [Integer]
collatzB 1 = [1]
collatzB n
| even n = n:collatzB $ n `div` 2
| odd n = n:collatzB $ n * 3 + 1
-- attempted solution, works but re-adds the parentheses I tried to get rid of
collatzC :: Integer -> [Integer]
collatzC 1 = [1]
collatzC n
| even n = n: (collatzC $ n `div` 2)
| odd n = n: (collatzC $ n * 3 + 1)
那麼爲什麼collatzA
和collatzC
工作,但collatzB
沒有?
非常巧妙的解決方案;但我認爲你是對的,它確實會對校對員造成更多的困惑,而不是它的幫助,所以我認爲我會按照你的建議並堅持括號 –