Home

Syntax in Functions

Pattern matching

When defining functions, you can define separate function bodies for different patterns. This leads to really neat code that's simple and readable.

lucky :: (Integral a) => a -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!"

Here is a more useful example with factorials:

factorial :: (Integral a) => a -> a factorial 0 = 1 factorial n = n * factorial (n - 1)

Note: Pattern matching can also fail if the fallback case is not defined.

Pattern matching can also happen with list comprehensions:

ghci> let xs = [(1,3), (4,3), (2,4), (5,3), (5,6), (3,1)] ghci> [a+b | (a,b) <- xs] [4,7,6,8,11,4]

Guards

bmiTell :: (RealFloat a) => a -> String bmiTell bmi | bmi <= 18.5 = "You're underweight, you emo, you!" | bmi <= 25.0 = "You're supposedly normal. Pffft, I bet you're ugly!" | bmi <= 30.0 = "You're fat! Lose some weight, fatty!" | otherwise = "You're a whale, congratulations!"

An example of a small myCompare function:

myCompare :: (Ord a) => a -> a -> Ordering a `myCompare` b | a > b = GT | a == b = EQ | otherwise = LT -- 3 `myCompare` 2 -> GT

Where bindings

bmiTell :: (RealFloat a) => a -> a -> String bmiTell weight height | bmi <= 18.5 = "You're underweight, you emo, you!" | bmi <= 25.0 = "You're supposedly normal. Pffft, I bet you're ugly!" | bmi <= 30.0 = "You're fat! Lose some weight, fatty!" | otherwise = "You're a whale, congratulations!" where bmi = weight / height ^ 2

Let bindings

Repository

https://github.com/okeeffed/developer-notes-nextjs/content/haskell/syntax-in-functions

Sections


Related