Skip to content

Instantly share code, notes, and snippets.

@teodorlu
Created August 17, 2019 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teodorlu/68ebbb7f4b5b5e8033b914c3c1c3a6fb to your computer and use it in GitHub Desktop.
Save teodorlu/68ebbb7f4b5b5e8033b914c3c1c3a6fb to your computer and use it in GitHub Desktop.
Create your own operator in Haskell
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 $++$
-- 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