James Yeoman
James Yeoman

Reputation: 675

How do I declare an 'operator' that takes 2 arguments and uses letters rather than symbols?

I want to be able to make operators such as found in VB(.net) like shown below

Console.WriteLine ( 16 Mod 2 )

produces an output shown below

0


As you can see, this makes code slightly easier to read. How would I go about making functions that do this?

I have tried the following format

equal :: Integer -> Integer -> bool
x `equal` y = x == y

and I get the following error

*Main> 5 equal 5

<interactive>:1:1: error:
    * Non type-variable argument
        in the constraint: Num ((Integer -> Integer -> Bool) -> t -> t1)
      (Use FlexibleContexts to permit this)
    * When checking the inferred type
        it :: forall t t1.
              (Num ((Integer -> Integer -> Bool) -> t -> t1), Num t) =>
              t1
*Main>

What is going wrong and how do I do it correctly?

Upvotes: 1

Views: 76

Answers (1)

Ben
Ben

Reputation: 71590

You need to use backticks around equal when you call it, exactly as you did to define it.

5 `equal` 5

The way you wrote it, Haskell thinks you're trying to pass equal as an argument to 5, rather than the other way around.

Upvotes: 7

Related Questions