Page 1 of 2

Haskell

Posted: Mon Jan 21, 2008 10:20 pm
by Trond
I'm trying to learn Haskell. It's totally different from PB. I can't even make heads or tails of it, probably because there is not head nor tail. Everything happens at once, but doesn't actually happen until it's necessary. :shock:

Basically it's very simple, but also very complicated. I can understand certain individual statements, but I have absolutely no idea on how to actually DO something...

Has anyone else tried this inside out language?

Posted: Mon Jan 21, 2008 11:05 pm
by SquareIris
It's a functional programming language, I had to learn "SML" in the first semester of computer science and I hated it! Haskel looks very similar, I guess functional is just not for me :?

Posted: Tue Jan 22, 2008 12:19 pm
by srod
Cripes, that does look complex! I reckon a good book is required for that one!

Posted: Tue Jan 22, 2008 1:42 pm
by pdwyer
Doesn't look too good for algorithm implementation.

There's an example on their wiki that shows qsort in one line of code :shock: but if you read on it says it doesn't match performance and allocates heaps more memory to do it rather than in place memory sorting like the C version.

It sounds like it takes the "programming" out of programming and focuses on ideas. Those logic languages do that in a way (prolog etc). Great idea but I can't get into them.

Maybe if you didn't already know how to program they would be more appealing

Posted: Tue Jan 22, 2008 2:19 pm
by freak
functional programming rocks! 8)

Its a quite different way of thinking though. Closer to the mathematical view of things,
which allows certain tasks to be done in a very elegant way, much better than in imperative languages.
Once you get the grip of it, its not more difficult than imperative programming imho. Its just... different ;)

I haven't used Haskell that much, but some other functional languages.
I think i can give some tips:

Think of the task to do in terms of a function that calculates a result in the mathematical sense,
not in terms of a C function or PB procedure.
There are no loops in functional programming generally, to do things repeatedly, there
is recursion. Everything that can be done in a loop can also be transformed to a
recursive function call, so this is no limitation at all.

What seems confusing at first is that Haskell allows no side-effects, so you cannot
have a variable and then change its value.
(other functional languages have this too, but they have spechial constructs to allow it again in some way)

The difference is in how the language calculates a result. Languages like PB have a "state"
(the content of all variables/memory blocks + the current point of execution),
and a result is calculated by manipulating that state (change variables).
Functional lanugages only evaluate expressions. All the code is seen as one big
expression, and when the evaluation is done you have your result. No changing of
variables is needed here.

An example: Lets calculate the sum of all numbers 0 to 10 in PB:

Code: Select all

x = 0
For i = 0 To 10
  x + i
Next i
Debug x
How do we do the same without loops and without changing a variable ?
Lets look how we could define a mathematical function to calculate this. Something like this:

Code: Select all

       / 0             if x = 0
f(x) = |
       \ x + f(x - 1)  otherwise
Lets implement this in PB:

Code: Select all

Procedure f(x)
  If x = 0
    ProcedureReturn 0
  Else
    ProcedureReturn x + f(x - 1)
  EndIf
EndProcedure

Debug f(10)
No more loops. And we also never change the value of "x" anymore. All we do
is call f() again, which will have a new x with a different value.
This is how functional programming works.

Here is the same in Haskell:

Code: Select all

f x = 
  if x == 0
  then 0
  else x + f (x - 1)
"f 10" gives the same result as the PB program.
With the pattern matching feature of Haskell, it is even possible to express this
much closer to the mathematical definition:

Code: Select all

g 0 = 0
g x = x + g (x - 1)
Looks just like the definition above:
"if g is called with 0, the result is 0, else it is x + g(x - 1)"
simply elegant 8)

Of course there is much more to functional programming, for example the fact that functions
can create functions, or the Haskell lazy evaluation which allows to create
infinite data structures (lists of infinite size for example).
All cool stuff that languages like PB or C simply cannot do.

@pdwyer:

This is not true. Functional languages can be very efficient, espechially those that
compile to native code. Of course for some algorithms their performance
is bad, but you can easily find such examples for any language.

Imho they are espechially good for implementing algorithms, because some things
can be expressed much more clearly than in other languages, so the implementation is easier to follow.
Also the closeness to the mathematical definition is a good point when it comes
to prooving that an algorithm actually works correctly.
(good luck prooving that for a C code that throws around pointers like hell... :P )

Oops, too much text and out of time... have to go :)

Posted: Tue Jan 22, 2008 2:35 pm
by SquareIris
You have many valid points in your post. But I still don't like functional programming and I'm sudying Mathematics now :)
Well, maybe it's just the though that mathematical expressions are fine for me on the paper while I don't want to see them in my hobby (programming) :P

Posted: Tue Jan 22, 2008 4:00 pm
by yoxola
It's a way of doing things, as I only used to BASIC so I have to stick with it....

As to Functional Language, I remember there's Clean http://clean.cs.ru.nl/ ...

Posted: Tue Jan 22, 2008 5:29 pm
by djes
freak> Thank you for this great explanation :)

Posted: Tue Jan 22, 2008 8:11 pm
by blueznl
djes wrote:freak> Thank you for this great explanation :)
Yeah. Now get back to working on 4.20 :-)

Posted: Tue Jan 22, 2008 11:21 pm
by Psychophanta
blueznl wrote:
djes wrote:freak> Thank you for this great explanation :)
Yeah. Now get back to working on 4.20 :-)
LOL

Posted: Wed Jan 23, 2008 12:48 am
by pdwyer
freak wrote:@pdwyer:

This is not true. Functional languages can be very efficient, espechially those that
compile to native code. Of course for some algorithms their performance
is bad, but you can easily find such examples for any language.

Imho they are espechially good for implementing algorithms, because some things
can be expressed much more clearly than in other languages, so the implementation is easier to follow.
Also the closeness to the mathematical definition is a good point when it comes
to prooving that an algorithm actually works correctly.
(good luck prooving that for a C code that throws around pointers like hell... :P )

Oops, too much text and out of time... have to go :)
I suppose my point is more that, with implementing algorithms, its often not enough that it just works. Optimisation is a key point. In my (admittedly very limited) experience with these languages they are used at a level that makes it difficult to see what's happening under the hood so you can optimise the code. Some algorithms if not implemented properly can blow all memory away in a millisecond.

It seems like you would end up having to learn the "tricks" of the compiler to optimise code, little syntax peculiarities that lead to a more optimal version. and if these syntax oddities looked less elegant from from a funtional perspective, what have you then gained by using this language? less lines of code?

Maybe I'm missing the point. I like languages like PB. (specifically PB! 8) ). It's (for me) the perfect balance between control and ease of understanding

Posted: Wed Jan 23, 2008 2:21 am
by freak
As i said, it is easy to implement poor algorithms in other languages as well.
In my experience it is not harder to implement most algorithms (there are exceptions of course) efficiently in functional languages than in others.

Functional programming is not the holy grail. Its a different approach with its strengths and weaknesses,
but in my opinion it is well worth exploring, even if only for the sake of knowing what other methods are out there.

Posted: Wed Jan 23, 2008 2:50 am
by pdwyer
do you have control over the data structures though? Or are you relying on the product?

(not an attack, I'm honestly curious)

Posted: Wed Jan 23, 2008 4:12 pm
by Trond
Of course everything won't be as fast, but the speed drop of Haskell vs. C compared to other high-level languages is very small.

It beats the crap out of Java when it comes to memory use, and is mostly faster as well:
http://shootout.alioth.debian.org/gp4/haskell.php

It does in fact not lose by a lot compared to plain C:
http://shootout.alioth.debian.org/gp4/b ... &lang2=gcc

Blows LISP out of the water:
http://shootout.alioth.debian.org/gp4/b ... lang2=sbcl

Posted: Wed Jan 23, 2008 4:20 pm
by Rook Zimbabwe
BOOK here: http://en.wikibooks.org/wiki/Haskell

Book free... how good??? Well you get what you pay for... 8)

I have browsed this book and it turned my brains into a knot in about 4 minutes... I want an intuitive programming langague, not a logical one!!!! I have one of those IG minds...