Haskell 101

February 4, 2008

Matching items

Filed under: Functional programming, Guards, Haskell, Pattern matching, Recursion — Haskell 101 blogger @ 8:02 pm

So far, we have created a function that determine whether a given item appears in a list, and another that counts how many times a given item appears in a list. Now let’s create one that returns a complete list of all items from a given list that match a given item. In other words, we have a list y and want to get a list of all items in y that match a given item x.We want a function that takes an integer and a list, and returns a list. 

(1) matches::Int->[Int]->[Int]

 As we did with our instances function, we start with the case that ends a recursion. 

(2) matches x [] = []

 For the other cases, we’ll mimic the structure from our instances function. This time, though, instead of adding one to the total, we’ll simply add the matching item to the list we’re collecting. 

(3) matches x (y:ys)
	| x==y = x:(matches x ys)
	| otherwise = matches x ys



February 3, 2008

Count matches in a list

Filed under: Functional programming, Guards, Haskell, Pattern matching, Recursion — Haskell 101 blogger @ 8:07 pm

What if, instead of just checking to see whether a value exists in a list, we want to count the number of times it is in the list? In other words, we want a function that will take a value, compare it to a given list of integers, and return an integer with the number of times it matches.As usual, let’s start with our function signature, which we have just about created with the previous sentence: 

(1) instances::Int->[Int]->Int

 By the way, we’ll generalize this function to be used for a list of any type (integers, strings, etc.) in a near-future posting as a way of introducing the concept of classes in type signatures. But for now, let’s stick with the simple case of integers. OK, we know that we need to look through the list, adding one to our total number of instances for each item that matches our chosen item. Since Haskell does not use the the loop structures typical in imperative programming, we can use what turns out to be a very simple approach with recursion. One helpful way to think about this is ask what will end the recursion. Usually it is an empty list. If we are evaluating our function on an empty list, we know that empty list does not match our chosen value. In other words: 

(2) instances x [] = 0

 What about if we are evaluating a non-empty list? Well, let’s split that list into one item in the list and the rest of the list, using the (y:ys) concept. Here, y represents the first item, and ys represents the remainder of the list. We should add one to our total if y equals our chosen value. Then we should re-run our function on the remainder of the list, or ys. In an imperative programming language, we might say something likeif x==y then return 1+(instances x ys)else return (instances x ys)But in Haskell, we’ll use the concept of guards to do this. 

(3) instances x (y:ys)
	| x==y = 1+(instances x ys)
	| otherwise = instances x ys

 All those equal signs can be confusing to a new Haskeller. Remember, the first thing just to the right of the guard (|) is an expression that will be evaluated. So x==y is evaluated, and, if true, then the function evaluates to the expression onthe right. Otherwise, the function will evaluate to to the expression to the right of the “otherwise” item.

January 27, 2008

Checking for a value in a list

Filed under: Functional programming, Haskell, List comprehensions, Pattern matching, Ruby — Haskell 101 blogger @ 9:24 pm

Once we have a list, how do we check whether that list contains a certain value? This is sort of like the Ruby method “include?”.

We want to provide the value we are looking for, and the list in which we would like to look for it.

(1) includes::Int->[Int]->Bool

Let’s first try using pattern matching and recursion.

(2) includes x (y:ys)
      | x==y = True
      |otherwise = includes x ys

In other words, take the head of the provided list and compare it to the desired value. If equal, return true. We don’t need to look anymore, because we know the value is there. If not equal, then we need to keep looking, so recurse, using the tail of the provided list for the next evaluation.

(3) includes x [] = False

We will only get to this step if we have not matched the value before we get to the empty list. if that is the case, then we want to return False.

We’ll look next time at some other, perhaps more elegant, but perhaps slower, way to do this.

January 26, 2008

Our first function

Filed under: Haskell, Pattern matching, Recursion — Haskell 101 blogger @ 4:23 pm

Part of what we will be doing throughout these blog entries is to create Haskell functions. Our first function is a simple one. In fact, it already exists. But half of the fun (at least) is going to be creating functions to learn the language. Building up basic code (even if it replicates a higher-order existing function) is one of the ways I have found to best learn a new language.So, today’s function is one that will take two lists and concatenate them. In the Haskell 98 Report, this is known as ++. I’ll call the function “union.”Let’s set the type signature first. We want to take two lists and return a third. So:

(1) union::[a]->[a]->[a]

How do we define the function? Let’s do it using recursion. We’ll add each item in the first list to an empty list [], and then add each element of the first list after that, and then, once the first list is empty, we’ll add the second list. So, we know we want to stop recursing when the first list is empty:

(2) union [] y = y

How about adding each item to the initial list? We’ll cons the item on using the : operator.

(3) union (x:xs) y = x:(union xs y)

Note the splitting of the first list into x and xs. This is a handy way of getting access to the first element of the list x, and assigning the remainder, or tail, of the list to xs (with the “s” indicating a plural number of elements). You can also see that we grab the first element and then call our union function again. We then grab the second element, call the function again, and so on. The pattern in (3) will no longer match when the first list is empty, and then the pattern in (2) will match, ending the recursion.  So, in summary, here is our function:

union [] y = y
union (x:xs) y = x:(union xs y)

 If you want, you can take a look at the aforementioned Haskell 98 Report and see how our function compares to the official definition of ++. 

January 25, 2008

Welcome to Haskell 101

Filed under: Functional programming, Haskell, List comprehensions, Pattern matching, Ruby — Haskell 101 blogger @ 4:32 am

Welcome to Haskell 101, a blog where I will walk through my own learnings with the functional programming language known as Haskell. How did I decide to do this? Well, I like to program as a hobby. Of all the languages that I have programmed in (Java, PHP, Visual Basic, and Ruby), Ruby is by far the one I enjoy most. I started to read about the roots of the Ruby programming language and learned of its functional programming roots. I then started looking into the various functional programming languages out there (especially Erlang, Haskell, and Lisp). The one that for some reason drew most of my attention was Haskell. I love the concept of pattern matching, which is not related much at all to regular expressions, by the way. I also like the elegance of list comprehensions, which feel much like the power and succinctness of some Ruby structures, like


Welcome aboard!

Create a free website or blog at WordPress.com.