1

Question

Above is a question I am having problems with. It asks me to give a type and define a non infix function.

So my approach was to use pattern matching.

The idea was to take two booleans, then go through patterns

 xor :: bool -> bool -> bool xor True True == False xor True False == True xor False True == True xor False False == False 

I am getting a compile error.

Parse error: naked expression at top level Perhaps you intended to use TemplateHaskell 

Can someone tell me what I am doing wrong here and how I can fix it?

Thanks alot.

1 Answer 1

3

please consider reading a basic haskell tutorial - like learnyouahaskell.com.

Types are written with captial cases and the == operator is not assignment but tests for equality

xor :: Bool -> Bool -> Bool xor True True = False xor True False = True xor False True = True xor False False = False 

also note that this can be simplified to

xor x y = x /= y 

after all it is just checking wether the two values you pass in are not the same.

or even more

xor = (/=) 

this style is called point-free, which means if the type signature is clear no parameters are needed (sometimes parameters are referred to as points, for historical/mathematical reasons).

By the way if you wonder what TemplateHaskell is - it is a compiler extension of GHC, which allows you to write code that at compile time creates haskell code - which then is type-checked and compiled again - I think about it as type-safe meta-programming.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.