I have recently read up on Haskell, a programming language. It’s rather interesting, as Haskell uses a completely different approach than the likes of C++ or Java. You mainly define functions, and you can do that in very clever ways. My interest got started by Good Math, Bad Math, but now i found this tutorial Haskell for C Programmers that’s well written and teaches you the basic ideas in as much text as is needed but doesn’t need the mathematical background (or computational scientic theoretical background) of the likes of lamba calculus et al.
I am trying to learn a bit about the important programming languages. I already am able to program in C++, have picked up Perl and now do most my data processing with Perl scripts. I know how I had to use Java, and studied Ruby for abit, but then got bored and now I am looking for something different. I think I might also peek at Lisp, for it seems to use a similar paradigm as Haskell.
As for Haskell, just look at this code (taken from the above tutorial):
map :: (a -> b) -> [a] -> [b]
fooList :: [Int]
fooList = [3, 1, 5, 4]
bar :: Int -> Int
bar n = n - 2
map bar fooList
= [1, -1, 3, 2]
The first line is a type definition, these seem to be rather important.
map is a function that works on functions that work on lists and gives back a list. Lists and functions are the building pieces in Haskell, and the way they are processed is so clever that this will largely simplify your calculations. We have the function
bar, which substracts 2 from n. Using
map, we can apply this function to each member in list
fooList. Simple as that!
Another important difference to C is that Haskell uses pattern matching to evaluate expressions. This works like that: You don’t need explicit and complicated
if-clauses, you just write a pattern that fits. I steal another example from the tutorial:
sumAll :: (Num a) => [a] -> a
sumAll (x:xs) = x + sumAll xs
sumAll  = 0
The first line again gives the data types.
Num is a type for everything that is a number, so that you won’t have to redefine the function for int, float or whatever. The patterns here are
(x:xs), meaning a list with at least one item in front of the list which will be called
xs will be the rest of the list. So the function works by adding x to another number which is calculated by applying itself on the rest of the list (recursion is everywhere in Haskell). The other pattern is for the obvious other case, a list without any members, and thus the condition to end the recursion. As you can see, this looks very natural, like the way you would write down what you have to do before implementing it in C. But here, you are already done. I like this pattern matching, but currently I am in the process of figuring out how to actually do something with Haskell. This looks all very good, but I am sure there are many traps. I will finish the tutorial and then look at some simple sample code. Maybe this language will come in handy for some calculations.
If I wrote anything dumb in this blog entry on Haskell, it’s because I just wrote down what I found out until now learning Haskell. It’s a good thing to collect your informations when learning something, and why not as a blog entry. It’s a real experience to learn new programming paradigms, it really widens your horizon of what you can do and gives you new tools that might someday come in handy. Or if not, the knowledge about other ways to approach problems is invaluable!
3 Comments »
Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed!