Select Page

“Learn You a Haskell” Chapter 3:

So in Learn you a Haskell the book chapter 3 covers pattern matching, guards, using where, and let in. Part 4 is exclusively covering recursion. I am currently have been practicing pattern matching, guards, and using where. My experience so far has been mostly positive. When my programming environment works, then working in Haskell is cool.

Pattern Matching

What makes pattern matching is cool, and that the code is is extraordinarily readable and works almost like a switch. For each possible function you are defining, the function body is visible. Pattern matching can use nearly any data type, which makes its versatility, on par with its readability. You can use pattern matching when creating recursive functions, which chapter 4 covers in great detail.

-- Pattern Matching

FirstOrNot :: int -> String
FirstOrNot 1 = "This is the first number"
FirstOrNot x = "Sorry this is not the first number" 

-- This is an example of a pattern match
-- from one of my Haskell 99 problems solutions

itemTest :: (Ord a) => [a] -> a
itemTest [] = error "List is empty" 
itemTest [x] = x
itemTest (x:xs) = last xs

-- this takes a list of numbers or letters
-- then prints the very last one.

FirstOrNot 1 "This is the 
first number"

FirstOrNot 4 "Sorry this is
 not the first number"

Here is an image of the item test function in action.


Guards are like pattern matching with boolean values. Guards allow you to check if a value is true or false and looks like a modified and condensed if statement. I do like how clean guards look in comparison to if-else statements. Guards also have catch-alls just like switch statements; this is called “otherwise” which is typically called default in a switch statement. I am still practicing the best methods of using where, as of right now, my understanding is that where allows you to use values in functions that were declared globally. Yet, you can use where on values that are defined within a function as well. Where scope, can also be used with pattern matching, to allow even cleaner functions.

--This is a height test for roller coasters made with guards
heightTest :: Double -> String
heightTest height
            | height <= 5.0 = "You can ride the rollercoaster"
            | height <= 6.0 = "You can ride the rollercoaster"
            | otherwise = " You're too tall or too short to ride the roller coaster "

-- I feel like guards work just like switch statements 


Chapter 3 also covers lets, which are expressions that are very similar to where bindings, except Let allows you to bind variable anywhere. The thing to consider when using let is to make sure you pay attention to the scope of the variable you are attempting to bind. Let can also be used with pattern matching. Case Expression looks very similar to pattern matching but can be used almost anywhere.

-- This is how I binded my input list and my secondLast function. 
let nextToLast = secondLast userList 
-- I was able to use this in a Do function that operates similar to imperative languages.

Coding Game

I attempted to use as learning to the tool, but it was missing a lot of the functions from the learning materials that I am currently using. I have been seeking another tool to practice Haskell, with I have been using the Haskell 99 problems.

Haskell 99

It is a super simple website that is very simple to interact with, and honestly, that is a breath of fresh air. Haskell 99 has been working great for me because this is where I have code that I have written that I can use for example going forward. I do hope that the difficulty scales up as I answer more and more questions.

Until Next Time

I will have another post out later this week, covering recursion and that is covered in chapter 4 in “Learn You a Haskell.” My journey is still at its beginning but I am making progress. I hope to one day eventually look back at these posts in the same manner one would look at old home videos. If you made it this far thank you, and if you enjoy this type of content go ahead and like this post and check out some more of my posts on my Journey into Haskell.