Haskell

For everything that's not in any way related to PureBasic. General chat etc...
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Haskell

Post 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?
SquareIris
New User
New User
Posts: 9
Joined: Fri Jan 04, 2008 4:06 pm
Location: Germany

Post 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 :?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Cripes, that does look complex! I reckon a good book is required for that one!
I may look like a mule, but I'm not a complete ass.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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 :)
quidquid Latine dictum sit altum videtur
SquareIris
New User
New User
Posts: 9
Joined: Fri Jan 04, 2008 4:06 pm
Location: Germany

Post 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
User avatar
yoxola
Enthusiast
Enthusiast
Posts: 386
Joined: Sat Feb 25, 2006 4:23 pm

Post 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/ ...
This field was left intentionally as signature.
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Post by djes »

freak> Thank you for this great explanation :)
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

djes wrote:freak> Thank you for this great explanation :)
Yeah. Now get back to working on 4.20 :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

blueznl wrote:
djes wrote:freak> Thank you for this great explanation :)
Yeah. Now get back to working on 4.20 :-)
LOL
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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.
quidquid Latine dictum sit altum videtur
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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)
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post 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...
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Post Reply