Skip to content

Instantly share code, notes, and snippets.

@bolerap
Created November 20, 2016 02:33
Show Gist options
  • Save bolerap/70c93748bd09994625e59ab264485eff to your computer and use it in GitHub Desktop.
Save bolerap/70c93748bd09994625e59ab264485eff to your computer and use it in GitHub Desktop.
-- define max function accept two number and return greater number
max :: Ord a => a -> a -> a
-- single line function equation
max x y = if x >= y then x else y
-- multiple line function equation (Guard style -> this is idiomatic haskell)
max x y | x >= y = x
| otherwise = y
-- signum function (Ord a, Num a) meant type variable a can belong both type class Ord and Num
signum :: (Ord a, Num a) => a -> Int
-- single line function equation
signum x = if x < 0 then -1 else if x == 0 then 0 else 1
-- multiple line function equation (Guard)
signum x | x < 0 = -1
| x == 0 = 0
| otherwise = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment