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
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.
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 >>= .
The ++ operator is the list concatenation operator which takes two lists as operands and "combine" them into a single list.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With