Presentation is loading. Please wait.

Presentation is loading. Please wait.

Haskell: Syntax in Functions

Similar presentations


Presentation on theme: "Haskell: Syntax in Functions"— Presentation transcript:

1 Haskell: Syntax in Functions
Presentation and code examples: Haskell: Syntax in Functions Bohdan Tokariev Using examples and some jokes from “Learn you a Haskell” book

2 FrienDly recap In Haskell we define function like this: So it’s sort of similar with JavaScript where you can do it in such manner: ES5: Or using ES6 arrow functions (please use them when you can): squared x = x * 2 var squared = function(x) { return x * 2 } let squared = (x) => x * 2;

3 datatype is valid for pattern matching
EVERY datatype is valid for pattern matching It means you can use: Numbers Characters Lists Tuples Whatever

4 Pattern matching All you need to do is to declare separate function body and you’re ready to go Patterns will be checked from top to bottom and corresponding one will run his function lucky :: (Integral a) => a -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!"

5 Pattern matching You can create as many patterns as you want (it’s your life hey) IMPORTANT! Always include a catch-all pattern (in this case ”x”) so that our program doesn't crash if we get some unexpected input sayMe :: (Integral a) => a -> String   sayMe 1 = "One!"   sayMe 2 = "Two!"   sayMe 3 = "Three!"   sayMe 4 = "Four!"   sayMe 5 = "Five!"   sayMe x = "Not between 1 and 5"  

6 Pattern matching (now with tuples)
If we (for some reason) want to add elements of two vectors we can do something like this: addVectors :: (Num a) => (a, a) -> (a, a) -> (a, a)   addVectors a b = (fst a + fst b, snd a + snd b)

7 Pattern matching (now with tuples)
If we (for some reason) want to add elements of two vectors we can do something like this: But now lets use some pattern So if we, for example, type addVectors (2,2) (4,3) , the result should look like (6,5) addVectors :: (Num a) => (a, a) -> (a, a) -> (a, a)   addVectors a b = (fst a + fst b, snd a + snd b) MAGIC addVectors :: (Num a) => (a, a) -> (a, a) -> (a, a)   addVectors (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)

8 Pattern matching (now with tuples)
fst and snd extract the components of pairs. But what about triples? Well, there are no provided functions that do that but we can make our own. first :: (a, b, c) -> a   first (x, _, _) = x      second :: (a, b, c) -> b   second (_, y, _) = y   third :: (a, b, c) -> c   third (_, _, z) = z

9 Pattern matching (now with tuples)
Also we can use patterns in list comprehensions In this case the result should be: [4,7,6,8,11,4] ghci> let xs = [(1,3), (4,3), (2,4), (5,3), (5,6), (3,1)]   ghci> [a+b | (a,b) <- xs]

10 Pattern matching (in lists)
Since [1,2,3] is just syntactic sugar for 1:2:3:[], you can also use the former pattern. A pattern like x:xs will bind the head of the list to x and the rest of it to xs If you want to bind the first three elements to variables and the rest of the list to another variable, you can use something like x:y:z:zs

11 Pattern matching (in lists)
With such knowledge we are able to implement our own head function And proceed even further with the function, which can describe our list head' :: [a] -> a head' [] = error "Can't call head on an empty list, dummy!" head' (x:_) = x tell :: (Show a) => [a] -> String tell [] = "The list is empty" tell (x:[]) = "The list has one element: " ++ show x tell (x:y:[]) = "The list has two elements: " ++ show x ++ " and " ++ show y tell (x:y:_) = "This list is long. The first two elements are: " ++ show x ++ " and "  show y

12 Pattern matching (in lists)
If you to your pattern you can assign a name to your list, so you can use it later Keeping in mind that strings are lists of characters we’re able to get a first letter from it With input capital "Haskell" the output should be: "The first letter of Haskell is H" capital :: String -> String   capital "" = "Empty string, whoops!"  

13 Guards! Guards are pretty similar to a basic “if/else if” statement, that is used in imperative languages, but looks a little more fancy With can see them in action in this implementation of max function Otherwise is an equivalent of else, where it equals True and catches everything max' :: (Ord a) => a -> a -> a   max' a b        | a > b     = a       | otherwise = b IMPORTANT! There is no need to put = before the |. Unless you want to get a compilation error of course.

14 Where (is my mind) Because we can’t create variables in function body, the where statement becomes handy, when there is a need in binding names bmiTell :: (RealFloat a) => a -> a -> String bmiTell weight height     | bmi <= 18.5 = "You're underweight, you emo!"     | bmi <= 25.0 = "You're supposedly normal."     | bmi <= 30.0 = "You're fat! Lose some weight, fatty!"     | otherwise   = "You're a whale, congratulations!"     where bmi = weight / height ^ 2

15 Where (is my mind) Another example is a function, that returns initials from provided name initials :: String -> String -> String   initials firstname lastname = [f] ++ ". " ++ [l] ++ "."       where (f:_) = firstname             (l:_) = lastname  IMPORTANT! Where statement needs to be indented (like in Python), because compiler will be confused about where this “where” belongs (pun intended)

16 Bear with me, only few slides left let bindings
Let bindings let you bind to variables anywhere and are expressions themselves, but are very local, so they don't span across guards Here is a function that gives us a cylinder's surface area based on its height and radius cylinder :: (RealFloat a) => a -> a -> a   cylinder r h =      let sideArea = 2 * pi * r * h           topArea = pi * r ^2       in  sideArea + 2 * topArea

17 Case expressions Remember the custom head function that we did earlier? We can rebuild it using case of expression that is same as switch/case used everywhere else Both are completely interchangable head' :: [a] -> a   head' [] = error "No head for empty lists!"   head' (x:_) = x   head' :: [a] -> a head' xs = case xs of [] -> error "No head for empty lists!"                       (x:_) -> x

18 Thank you for your attention
Link for presentation and examples: Book chapter link:


Download ppt "Haskell: Syntax in Functions"

Similar presentations


Ads by Google