Computer Science Canada [Haskell-tut] Lists |
Author: | wtd [ Fri Nov 19, 2004 5:07 am ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Post subject: | [Haskell-tut] Lists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Recap We have integers, floating point numbers, characters, and strings. What's new? Lists are collections of a single type of data. What does a list look like? A simple list of integers looks like:
Of course, a simple range like this can be more succinctly represented as:
How is a list defined? This really deals with the very first operator/function we'll see for dealing with lists.
Yields:
So, really, we can rewrite that as:
Of course, is an empty list. That's nice, but... Lists are roughly analogous to arrays in other languages, and there are similar operations available. Finding the length of a list:
Getting the first element:
Getting everything else:
Adding two lists together:
Finding any element in a list (in this case, the 3rd):
Finding if an element is in a list:
Or, using the infix form of any function which takes two arguments:
Which should read as "is foo an element of this list". Now, if we want to apply a function to each element in a list, to square each:
Or say we want to filter a list:
Another look at the last two Haskell offers a special bit of syntactic sugar for list operations, called the list comprehension. Python borrowed this feature. Squaring 1 through 6 can look like:
And the filter operation can look like:
What about loops? Loops and lists (or arrays) usually go hand in hand. Haskell has no looping construct. So how is "map" possible? Recursion Let's consider the map function again. How do you suppose they do that? Well, they apply the function to the first item in the list, then recursively do the same to the rest of the list. Obviously the result of mapping a function to an empty list is another empty list.
The _ in this case indicates an argument, but one whose value we don't care about. In this case we're going to get the same result, no matter what function we pass to map.
The function argument in this case is "f".
Might look a bit familiar. It has that ":" we associated with building a list. Indeed, x is the first element of a list, and xs is the rest of the list.
We call the function f with argument x, and merge it to the list returned by calling the function recursively for the rest of the list. So, breaking down:
We get:
Take another look
This is a basic pattern you'll find when it comes to recursion, and it provides all the power of a loop in an imperative language. Another example: sum The problem: given a list of numbers, find their sum. First analysis: summing an empty list will always give us zero.
Second analysis: we can calculate the sum by adding the first element of the list to the sum of the rest of the list.
Breaks down to:
Now a different take on this So far we've been doing pretty simple recursion, and handling the recursion ourselves. There's a method which avoids this, called "folding". Consider a deck of 52 cards which can represent a list metaphorically. They each have a number on them. To count them, we could add the top two cards and put a card with that value back on top of the deck. We then do the same thing all over again until we're left with one card with the final result. One of the things we need in a fold is a function which takes two arguments which represent the two cards. Since our list might only have one element, we provide a starting value for the first time around. For a sum, we should start out with an intial value of zero.
Is a function. The only thing it lacks is a name. Of course, we can simplify this even further by realizing that we don't need to construct the extra function.
And we can partially apply the function to create a general sumList function.
Or even:
One last thing The "l" in "foldl" stands for "left". It starts on the left side of a list and works its way to the other end. The "foldr" function works identically except that it works through the list in the opposite direction. Take some time... I realize all of this will take some time to digest, so sit back and think about it a bit. Another good idea is to try the code I've posted to see that as weird as it looks, it works. |
Author: | Mazer [ Fri Nov 19, 2004 12:12 pm ] |
Post subject: | |
So far it looks almost exactly like Miranda. But can you tell me just what uses there are for which using a functional programming language would be an advantage? I see easy to use lists, but aside from that... |
Author: | wtd [ Fri Nov 19, 2004 2:51 pm ] | ||||
Post subject: | |||||
Well, type safety. Every type error is going to be caught at run-time. For Haskell at least, the fact that it's purely functional means that a function given the same arguments will always produce the same result. This means a good compiler can implicitly memoize. Tail call recursion optimization. A basically factorial function, for instance:
Can have the recursion optimized away into an iterative loop by a good compiler. Type inference. You don't waste time writing out types if you don't want to. Concise syntax. Consider the legendary Haskell quicksort:
Compare it to a quicksort in C++. It pretty much has to be C++, since few other languages support generic programming well enough to give us a quicksort that will work on any data type for which <= and > are defined. Two quicksort functions in Turing, for instance, would have to be written to sort both ints and strings. |
Author: | wtd [ Fri Nov 19, 2004 8:46 pm ] | ||||
Post subject: | |||||
An addition To get a certain number of elements from the front of a list, use the "take" function.
Yields:
|