Created
August 17, 2019 11:35
-
-
Save teodorlu/68ebbb7f4b5b5e8033b914c3c1c3a6fb to your computer and use it in GitHub Desktop.
Create your own operator in Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Lib where | |
someFunc :: IO () | |
someFunc = putStrLn "someFunc" | |
data Expr a = | |
Number a | |
| Plus (Expr a) (Expr a) | |
deriving (Show, Read) | |
-- shorthand for number, type constructor for Expr | |
n = Number | |
($++++$) :: Num a => a -> a -> a | |
a $++++$ b = a*a + b*b | |
($+$) :: Expr a -> Expr a -> Expr a | |
a $+$ b = Plus a b | |
infixl 6 $+$ | |
($++$) :: Expr a -> Expr a -> Expr a | |
a $++$ b = Plus a b | |
infixr 6 $++$ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Run this in a `ghci` or `stack ghci` -- this isn't really a file. | |
*Main> :load Lib | |
*Lib> n 10 $+$ n 20 $+$ n 30 | |
Plus (Plus (Number 10) (Number 20)) (Number 30) | |
*Lib> n 10 $++$ n 20 $++$ n 30 | |
Plus (Number 10) (Plus (Number 20) (Number 30)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment