0

I'm trying to learn Haskell and I got this error.
parse error on input `=' Here is my code:

nAnd1 :: Bool -> Bool -> Bool
nAnd x y = if (x==False && y == False) || x/=y then True else False

nAnd2 :: Bool -> Bool -> Bool
nAnd x y | if ((x == False && y == False) || x/=y) = True
         | otherwise = False

The place where the error takes place is at the "=" before True in nAnd2. Any solution?

3
  • 2
    remove if after | Commented Sep 25, 2020 at 9:38
  • 2
    but why after definition of type in nAnd1 and nAnd2 you write nAnd? Commented Sep 25, 2020 at 9:39
  • 1
    if ... then True else False is just a long way of writing .... Commented Sep 25, 2020 at 13:19

2 Answers 2

3

The answer to drop the if was already given. I suppose the exercise was meant differently. The nAnd means "not and", i.e.

nAnd a b = not (a && b)

but you have nAnd x y = (not x && not y) || x /= y. That is very strange and I suspect an X-Y-Problem here. Did you get the definition of nand from a truth table?

  a  |  b  | a `nAnd` b
-----+-----+----------
False|False|   True
False|True |   True
True |False|   True
True |True |   False

Then take the time to rewrite the truth table as

  a  |  b  | a `nAnd` b
-----+-----+----------
True |True |   False
  *  |  *  |   True

and use Pattern Matching:

nAnd :: Bool -> Bool -> Bool
nAnd True True = False
nAnd _    _    = True
Sign up to request clarification or add additional context in comments.

Comments

2

The guards (|) act as if clauses, it does not make sense to write if for these:

nAnd :: Bool -> Bool -> Bool
nAnd x y | (x == False && y == False) || x /= y = True
         | otherwise = False

It furthermore however is odd to return True or False based on a condition, you can rewrite this to:

nAnd :: Bool -> Bool -> Bool
nAnd x y = (not x && not y) || x /= y

or we can negate the values and use (||):

import Data.Function(on)

nAnd :: Bool -> Bool -> Bool
nAnd = on (||) not

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.