素數我試圖寫一個返回低於給定數量的所有質數的列表的過程。得到如下X
例如:
Prelude>primes 8
[2,3,5,7]
當我嘗試加載該文件,我得到Parse error in pattern Failed, modules loaded: none.
如果有人能在正確的方向指向我,我將不勝感激。
primes :: Int -> [Int]
primes x < 2 = []
primes x | isPrime x == True = primes (x - 1) ++ x
| otherwise = primes (x - 1)
isPrime :: Int -> Bool
isPrime x | x < 2 = False
| x == 2 || x == 3 = True
| divEven x == True = False
| divOdd x 3 == True = False
| otherwise = True
divEven :: Int -> Bool
divEven x | mod x 2 == 0 = True
| otherwise = False
divOdd :: Int Int -> Bool
divOdd x num | mod x num == 0 == True
| num <= x/2 = divOdd x (num + 2)
| otherwise = False
'x == True'與'x'相同。 – Vitus 2012-08-07 02:49:50
謝謝維庫斯。我之前沒有這樣做過,但在不編譯的時候添加了它。我想這並沒有什麼區別。 – 2012-08-07 02:58:28