【发布时间】:2012-08-04 00:42:06
【问题描述】:
我正在尝试编写一个返回给定数字以下所有素数列表的过程。
例如:
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相同。 -
谢谢维特斯。实际上我之前没有它,但是在它无法编译时添加了它。我想这并没有什么区别。