I’m trying to write a procedure that returns a list of all primes below a given number.
For example:
Prelude>primes 8
[2,3,5,7]
When I try to load the file I get Parse error in pattern Failed, modules loaded: none. If someone could point me in the right direction I would be grateful.
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
A collection of small mistakes.
Your syntax is incorrect.
Probably you meant
Similarly, where you write
you probably meant
The type signature
is not valid. You probably meant
xis of typeInt, and(/) :: Fractional a => a -> a -> acannot be applied to it. You probably meannum <= x `div` 2or2 * num <= x.xis of typeInt, not[Int].(++) :: [a] -> [a] -> [a]will not apply to it.Perhaps you meant
Finally, this is a fairly inefficient way of generating primes. Have you considered a sieve? Prime numbers – HaskellWiki may be a bit difficult for you right now, but shows many different strategies.