• entries
67
114
• views
34146

# University programming

267 views

One good thing about Computing at university is that although you do theory, it's useful theory - lists, data stuctures etc. You also do plenty of practical work, which means doing actual hands on programming.

The language that we use at least in this semester is Haskell, because we're doing functional programming. This is something new to me, which is part of the reason why they picked it - it will probably be new to experienced and new programmers alike, given that most people will have used C, Java, etc.

I realised shortly after looking at the basic syntax that it reminds me very much of what I've seen of lisp, and sure enough lisp is indeed a functional programming language too. I plan to take a closer look at lisp than I have previously, now that I have been taught the basic principles of the language (I also understand why the hell it has so many brackets, but that's another issue).

One thing that I love about haskell is that it almost seems to encourage density of code. Whereas "clean" code in C or similar would be spread over many lines, "clean" code in Haskell is all embedded nicely into a single function (not necessarily one line, but often so). The language has many constructs to help you do this.

For example, one program that we went through today in a tutorial was such that it takes a list and returns a list of all the even numbers in the input list, halfed. The code goes like this:

halfEvens :: [Int] -> [Int]halfEvens xs = [div x 2 | x <- xs, mod x 2 == 0]

Man that code brings tears to my eyes, it's just so amazingly compact, and at the same time it's not the same as C code which obfuscates as you compact it. With a simple explanation of the above you can understand it easily (for those of you not interested in the details skip the next two paragraphs):

The first line declares halfEvens as a function taking a list of Ints and returning a list of Ints. The [] denotes a list. The next line is the function definition. It says that the function halfEvens has a parameter xs. This is just a convention meaning a list of values, each would be called x. The bit after the = is called a list comprehension.

The list comprehension says that "divide x by 2 (taking the quotient), where x is each value from xs, and x satisifies 'mod x 2 == 0'". The 'mod x 2 == 0' would be written in other languages like x % 2 == 0 - ie, is x even.

You can scale this up too, for example a qsort is defined like this:

qsort :: [Int] -> [Int]qsort (x:xs) = qsort [y | y<-xs, y<=x] ++ [x] ++ qsort[y | y <- xs, y>x]

And again, this code is really simple to read off and understand (given a couple of more explanations of syntax), and yet it does the job perfectly.

One more thing, I'm impressed by the course's decision at the lowest level of programming excercises not to fall into the trap of "define a function to calculate prime numbers". Instead they predefine a simple library which provides data types and functions for manipulating "pictures" (monochrome bitmaps in reality), and base excercises and assignments on that. Hats off to them it really helps in giving a good understanding of what this whole thing is useful for.

Anyway more as I think of it. Either way, I really love Haskell and functional programming in general. Go read a tutorial or something, you'll see why it's so great (I hope ;)).

St Andrews shove Java down your throat, but if you've done any programming before, the first semester here is a pisstake - you can basically sleep as it's all OO and other programming concepts that you've done before.

Yes, Haskell is awsome, especially that quicksort implemention taking only two lines.

I wish I had taken the time to learn it more thoroughly; it would seem perfect for testing out mathematical ideas in.

Quote:
 Haskell? Well, they do teach ADA at Glasgow after all, so Haskell doesn't sound that bizarre. St Andrews shove Java down your throat, but if you've done any programming before, the first semester here is a pisstake - you can basically sleep as it's all OO and other programming concepts that you've done before.

We do java in the second semester here too, that should be fun and games :).

(btw, how come there's no quote feature on the comments here?)

Quote:
 Original post by baldurk We do java in the second semester here too, that should be fun and games :).

I'm afraid to say the more haskell the you know (heck even a little) the less "fun and games" Java will be :)

## Create an account

Register a new account