Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an operator in Haskell?

Making a ternary logic table, and I would like to make my own function for an operator that I'll call <=>.

So, for example, I want to do this, but that isn't right. what's the correct way to do this?

data Ternary = T | F | M deriving (Eq,  Show, Ord)  <=> :: Ternary -> Ternary -> Ternary <=> T F = F <=> T T = T <=> T M = M <=> F F = T <=> F T = F <=> F M = M <=> M F = M <=> M T = M <=> M M = T 
like image 501
user1189352 Avatar asked Feb 20 '12 05:02

user1189352


People also ask

What is the OR operator in Haskell?

In Haskell we have or operator to compare the values of the variable, this operator also comes under the lexical notation of the Haskell programming language. This operator works in the same way as any other programming language, it just returns true or false based on the input we have provided.

What is >> in Haskell?

There are many tutorials available on monads; here's one good one. Essentially, a >> b can be read like "do a then do b , and return the result of b ". It's similar to the more common bind operator >>= .

What does ++ mean in Haskell?

The ++ operator is the list concatenation operator which takes two lists as operands and "combine" them into a single list.


2 Answers

Just add parentheses around your operator:

(<=>) :: Ternary -> Ternary -> Ternary (<=>) T F = F (<=>) T T = T (<=>) T M = M (<=>) F F = T (<=>) F T = F (<=>) F M = M (<=>) M F = M (<=>) M T = M (<=>) M M = T 

This turns it from infix form to prefix form. Alternatively, you can just use infix in the definition:

(<=>) :: Ternary -> Ternary -> Ternary T <=> F = F T <=> T = T T <=> M = M F <=> F = T F <=> T = F F <=> M = M M <=> F = M M <=> T = M M <=> M = T 
like image 70
Clark Gaebel Avatar answered Sep 28 '22 16:09

Clark Gaebel


Function names with symbols have different syntax than those without:

-- Works: (<^>) :: Int -> Int -> Int a <^> b = a + b  -- Doesn't work: {- <^> :: Int -> Int -> Int <^> a b = a + b -}  -- Works: letters :: Int -> Int -> Int letters a b = a + b  -- Doesn't work: {- (letters) :: Int -> Int -> Int a letters b = a + b -} 

I promise, though - Haskell is well worth learning the complex rules.

like image 26
amindfv Avatar answered Sep 28 '22 17:09

amindfv