Jordan Durci
Jordan Durci

Reputation: 43

How do I fix my Haskell conditional statement?

I'm trying to make a simple conditional statement that checks if a value is equal to another value, then sets a boolean to either true or false, but I'm having some difficulties.

So far, I've tried to reformat my text several different ways, none of which have worked, but I'm not finding much help in official documentation or from googling the issue.

import Data.List

numberB :: Integer
numberB = 10
eql :: Integer -> Bool
eql = 10
if eql == numberB
  then True
  else False

The error I'm currently getting says:

functions.hs:14:1: error:
    Parse error: module header, import declaration
    or top-level declaration expected.
   |
14 | if eql == numberB
   | ^^^^^^^^^^^^^^^^^...

I'm not fully certain what it's looking for me to do here, as I can't find any effective import declarations. I also don't think there's any modules I really need to use, either, as I'm not making a module here, and, as I mentioned before, there were no import statements I could find that would make this work. I'm also not sure what a top-level declaration is, as I'm not that experienced in Haskell. That said, I don't know what that would even do to fix this error.

Any help would be much appreciated.

Upvotes: 2

Views: 141

Answers (3)

Will Ness
Will Ness

Reputation: 71070

Nothing wrong with your conditional statement expression, but to what definition does it belong?

To decide that, Haskell uses indentation. Any code which is a part of some definition must be indented at least one space under it:

name =   -- value
  if eql == numberB
    then True
    else False

But your if ... starts at the same column as the preceding definition, eql = ....

Thus it is a piece of code which does not belong to any definition, and that is the syntax error which you get (which talks about "declarations" which I guess is the official term for it).

In the definition

eql = 10

the name is eql and the value is 10. It wouldn't match the declared type,

eql :: Integer -> Bool

but the syntax error aborts the compilation even before the types are checked.

Upvotes: 4

developer_hatch
developer_hatch

Reputation: 16224

Let's slow down things a little bit, first of all, let's review the type of eq:

(==) :: Eq a => a -> a -> Bool

you want to compare two Integer

numberB :: Integer
numberB = 10
eql :: Integer
eql = 10

and yes, one way s to do:

comparision = if eql == numberB
              then True
              else False

But by definition of == you can remove the if, is redundant, so:

comparisionShort = eql == numberB

and remember also, in Haskell you have to bind the values to something, you can't just write an if just like that.

Upvotes: 3

Yogendra
Yogendra

Reputation: 382

You have declared eql to be a Integer -> Bool which is a function that takes Integer and returns bool and then assigned it Integer value 10. Unlike dynamically typed languages like python you can't re-assign variables a value of different type. What you probably want is:

numberA ::Integer
numberB ::Integer
eql :: Bool
numberA = 10
numberB = 10
eql = numberA == numberB

Upvotes: 2

Related Questions