0xAX
0xAX

Reputation: 21837

Haskell let in/where and if indentation

I have a function:

isSimpleNumber :: Int -> Bool
isSimpleNumber x = let deriveList = map (\y -> (x `mod` y)) [1 .. x]
                       filterLength = length ( filter (\z -> z == 0) deriveList
                       ....

After filterLength i want to check how much filterLength, i try:

isSimpleNumber :: Int -> Bool
isSimpleNumber x = let deriveList = map (\y -> (x `mod` y)) [1 .. x]
                       filterLength = length ( filter (\z -> z == 0) deriveList
                       in if filterLength == 2
                          then true

I get error:

    parse error (possibly incorrect indentation)
Failed, modules loaded: none.

How can i put indentation correctly with if and in?

Thank you.

Upvotes: 0

Views: 2108

Answers (2)

user434817
user434817

Reputation:

This will compile:

isSimpleNumber :: Int -> Bool
isSimpleNumber x = let deriveList = map (\y -> (x `mod` y)) [1 .. x]
                       filterLength = length ( filter (\z -> z == 0) deriveList )
                   in filterLength == 2

main = print $ isSimpleNumber 5

There was a missing closing ")" after "deriveList". You don't need an if-then-true expression, also.

Upvotes: 3

Landei
Landei

Reputation: 54584

An if needs always both a then and an else branch, so you probably need if filterLength == 2 then true else false, which is equivalent to filterLength == 2.

Upvotes: 3

Related Questions