Log in

View Full Version : Learning Haskell



Trinithis
10-28-2007, 06:48 PM
I'm interested in learning the programming language Haskell simply because it looks interesting.

I have a few questions regarding it:

1) What are good tutorials for Haskell?
2) Or better yet, is there another functional language I should learn in favor of Haskell?
3) Is functional programming radically different from imperative programming?
4) What IDE should I use? An Eclipse plugin perhaps? Notepad?

Twey
10-28-2007, 08:04 PM
http://www.haskell.org/tutorial/ I prefer Lisp for practical application. Haskell is a beautiful language from a computer science point of view, and it's certainly fun to work with, but I find it somewhat more difficult than necessary for a lot of tasks which, let's face it, require state to represent properly. Lisp is often described as a "semi-functional" language: it's primarily functional, and supports all the operations one expects in a functional language, but it allows other paradigms as well (it supports a very powerful class-based object orientation system, for example, superior to that of any other language of which I'm aware, as well as allowing functions to have side effects and letting the programmer specify order of execution, in the manner of an imperative language).

I certainly don't mean to discourage you from learning Haskell, though: it is a lovely language, and definitely worth learning, especially if you're doing so for curiosity. Not necessarily, although it can be sometimes. You may not be aware, but Javascript is a fully functional functional language, and there's a library (http://osteele.com/sources/javascript/functional/) to implement some of the common extras often found in such languages in Javascript. The most obvious difference between a (purely) functional and an imperative language is that no data is stored. So, for example, in C one might write:
int mod_num(int n) {
int nn = n;
nn *= 5;
nn += 3;
return nn;
}However, this can be simplified:
int mod_num(int n) {
return n * 5 + 3;
}And in Haskell:
mod_num :: Int -> Int
mod_num n = n * 5 + 3Lisp:
(defun mod-num (n)
(+ 3 (* 5 n)))This is obviously a very simple example, but in the real world it gets complex (think of input and output, for example). Haskell has several clever ways around the necessity to store state, such as monads, which you'll come to in good time.

The other defining feature of functional languages is that one can modify and pass around functions as if they were any other data type, but you're probably familiar with this from Javascript. C's function pointers are a similar concept.

I seem to remember from somewhere that you're a mathematician. You'll have more success with Haskell thinking in terms of mathematical functions than functions in imperative programming languages. Vim works fine for me.

jscheuer1
10-28-2007, 08:28 PM
Haskell, wasn't that "The Beaver's" 'friend'?

Twey
10-28-2007, 08:33 PM
Wikipedia says "wha'?"

Trinithis
10-28-2007, 08:58 PM
Thanks for your input Twey. I've always thought first-class functions were fun to use in Javascript once I learned how to use them :D. As for mathematician, I'm just a fledging one. I am studing it as my major, and I also want to (at the very least) minor in computer science . . . programming is addicting! And the rest is interesting.

Trinithis
10-28-2007, 11:38 PM
Wow, this is so cool!

To paraphrase the tutorial:


add :: Integer -> Integer -> Integer
add x y = x + y

....

map :: (a->b) -> [a] -> [b]
map f [] = []
map f (x:xs) = f x : map f xs

....

map (add 1) [1,2,3] => [2,3,4]

Can't wait to dig deeper than this :D. And now I want to re-read Twey's article on Javascript currying.

Twey
10-29-2007, 12:20 AM
Huh, someone did read that then? I thought it was a wasted effort :p

Yeah, functional programming is cool. I do recommend you learn Lisp, too, though, and see how it works in combination with other paradigms, as well as on its own as in Haskell. Python has quite a few functional influences, too.

Trinithis
10-29-2007, 02:25 AM
I've read that Lisp is a family of languages. Which dialect should I get acquainted with first when I get the chance?

Twey
10-29-2007, 02:46 AM
It is. The only one I've learnt is Common Lisp; the other most common variant is Scheme. Some of the ideas behind Scheme seem silly to me, though, and CL has more libraries available.

jscheuer1
10-29-2007, 04:59 AM
Wikipedia says "wha'?"

Third result on Google:


JuneCleaversBeaver's Profile on The Huffington Post - The ...I would NEVER knowingly serve alcohol to my boys' friends. Ward and I do suspect that Eddie Haskell may have, on one or two ocassions, poured some type of ...
www.huffingtonpost.com/users/profile/JuneCleaversBeaver - 49k - Cached - Similar pages

Search terms:

Haskell, wasn't that "The Beaver's" 'friend'?

It's a reference to the old TV show 'Leave it to Beaver'.

I neither know nor care much if the character from that show named Eddie Haskell had any role in the naming of the programming language under discussion. I though it might. In any case, the show was hilarious, more so now, in that it is so outdated.

Twey
10-29-2007, 05:16 AM
I neither know nor care much if the character from that show named Eddie Haskell had any role in the naming of the programming language under discussion. I though it might.Heh, that would be Haskell Curry, a very famous mathematician/logician, rather than Eddie Haskell, "The Beaver's" 'friend' ;)

I fear I never saw (or in fact heard of) this programme. Perhaps it only aired in America.

Interesting mix of quotes there, by the bye. Was that part of the original phrasing?

jscheuer1
10-29-2007, 05:19 AM
I quoted "The Beaver's" because it was a unique character reference, double quotes because it already had an apostrophe. Single quotes around 'friend', because Eddie was a troublemaker.

cgibbard
10-30-2007, 05:43 AM
Hi Trinithis!

Before I answer your questions, one thing I really want to make sure you know about, because it's an incredibly useful resource, is that Haskell has an IRC channel, which is #haskell on irc.freenode.net. I highly recommend firing up an IRC client (I recommend X-Chat (Win (http://www.silverex.org/news/), Linux (http://www.xchat.org/))), and asking lots of questions there. It's very beginner friendly, and there are lots of people who will be more than happy to answer any questions you might have, or help you with code. It is by far the most useful piece of advice I could give you.


1) What are good tutorials for Haskell?

I recommend either the Wikibook (http://http://en.wikibooks.org/wiki/Haskell), or "Yet Another Haskell Tutorial" (http://en.wikibooks.org/wiki/Haskell/YAHT), up to the part where they start to talk about monads, at which point I'd strongly recommend getting a tutorial specifically about monads, as there are some much better materials in that regard. I wrote a couple tutorials on monads myself: Monads as Computation (http://www.haskell.org/haskellwiki/Monads_as_computation) and Monads as Containers (http://www.haskell.org/haskellwiki/Monads_as_containers), which give two ways to look at things, that can be helpful in different cases. I also wrote an Introduction to IO (http://www.haskell.org/haskellwiki/Introduction_to_IO), which is just about the one monad in Haskell which is used to describe I/O actions. I'd also recommend the fairly comprehensive tutorial "All About Monads" (http://www.haskell.org/all_about_monads/html/), and the encouraging "You could have invented monads! (And maybe you already have)" (http://sigfpe.blogspot.com/2006/08/you-could-have-invented-monads-and.html).


2) Or better yet, is there another functional language I should learn in favor of Haskell?

Of course, you probably shouldn't restrict yourself in the long run -- there are plenty of interesting functional languages. However, I don't think starting with Haskell is at all a bad idea. Of the 20 or 30 programming languages I know reasonably well, it is by quite a good stretch my personal favourite. Others which you might be interested in examining at some point are languages in the ML family like O'Caml or SML, as well as languages in the Lisp family like Scheme. They're not *quite* the same thing as Haskell, but if they were too similar, I probably wouldn't have mentioned them.


3) Is functional programming radically different from imperative programming?

Yes, most certainly. Especially so in Haskell, since it doesn't take any cop-out approaches to functional purity. Functions in Haskell, unlike functions in most imperative languages, are real, honest-to-goodness mathematical functions. This means that they only depend on their parameters, and only produce results, they have no side effects. This means they cannot quietly update and read some memory cell somewhere in order to return something different each time they're called. It also means they can't go and send stuff over the network behind your back. If you dislike bugs (and who really likes bugs?), this is great. It makes your functions easy to test: if you call your function with some parameters, and it gives the right result, then it will *always* give the right result for those parameters. There's no need to set up just the right environment for the bug to occur.

Of course, eventually you have to deal with real input/output, because without it, your programs won't do anything but make your machine get a bit hotter. So in Haskell, we describe the I/O actions we want to have carried out with values of a special type. You might think of these values as little snippets of imperative code that we can manipulate in various ways. These actions are allowed to use all the pure functions we've written, and we can also build them up using pure functions (which is sort of like writing your own control structures). In the end, you define an action called main, and it's the only action (in a compiled program) which actually runs, built up from lots of other actions defined in your program.

So the outlook is rather different, to say the least, and I haven't even got to lazy evaluation, or the type system yet, both of which are rather mind-altering experiences to learn about.


4) What IDE should I use? An Eclipse plugin perhaps? Notepad?

I personally prefer Vim (http://www.vim.org/) and Emacs (http://www.gnu.org/software/emacs/). I've heard that Windows users like to use something called TextPad (http://www.textpad.com/), but it appears to cost money. Any editor will do, so long as it has two things: reasonably good syntax colouring support for Haskell, and the ability to automatically convert all tab characters into spaces (bonus points if it can also treat multiple spaces as a tab, or auto-indent code). You *really* don't want tab characters in your source files. If there are tab characters, they are treated by the compiler as aligning to the nearest following 8-space boundary, which means that a line starting with <space><space><tab> is the same as starting with just <tab>, or with 8 spaces. Mixing spaces and tabs causes huge headaches, so it's best to just have your editor convert all the tabs away so there's no confusion.

See you on IRC!
-- Cale

Twey
10-30-2007, 03:50 PM
Hi Cale, welcome to DD!

Trinithis
10-30-2007, 04:44 PM
Hey, thank you for your wonderful post :D. I'll have to bookmark the pages you gave, and I got XChat to work. Again, thanks.

Oh, and side note: When I looked up "haskell" in two library indexes, "Leave it to Beaver" came up.

v3r5u5
12-01-2007, 01:31 PM
I have a programming question in haskell. Lets say I have the list [4,6,2,3,9,10] and I want to change the number in position 2 to be 7. The result should look like this [4,6,7,3,9,10].

How do I do that?

Twey
12-01-2007, 02:23 PM
The obvious:
head(mylist) : 7 : tail(tail(mylist))will work. There may be a better way of doing it though.

Trinithis
12-01-2007, 05:57 PM
replace :: [a] -> a -> Int -> [a]
replace (x:xs) y 0 = y : xs
replace (x:xs) y n = x : replace xs y (n-1)



replace [4,6,2,3,9,10] 7 2

v3r5u5
12-02-2007, 08:45 PM
thanks

v3r5u5
12-02-2007, 10:24 PM
I am pretty sure that there is a way in Haskell to use the previous result.
ex:
*Main> 6+7
13
*Main> 8+$1

But $1 will not work. What I want basically to do is 8+13.

Any ideas of what I should use?

Twey
12-02-2007, 11:27 PM
There's no such way in Haskell because there's no way of telling what "the last result" is. Haskell programs are executed in the order necessary, not the order you set them out in the source file. There may be a way to do it in your command-line interpreter, however.

v3r5u5
12-04-2007, 09:06 PM
you can do it in winhugs :)

Kristian1984
06-16-2008, 06:17 AM
I need help quickly please. I got one problem to solve in Haskell, not very
hard but I don't know anything about Haskell programming language so I must
ask for help here and hope for (positive) answer.

So, the question goes like this:

Write function in Haskell which delete every member of a list that is bigger
then the member after him. Test function on list of Integers and on list of
Doubles.

You may use your own functions for help but you may not use functions from
extra Haskell libraries.

Can someone help me please!

Kristian1984
06-16-2008, 08:01 AM
Anyone???

Please!

boogyman
06-16-2008, 01:23 PM
this sounds alot like a homework assignment.

i suggest reading your text book

Trinithis
06-17-2008, 05:58 AM
Hint: Split your function into two functions. The first one would be the one that the coder uses on a regular basis. The second would be the one that the first function uses behind the scenes for help (called a helper function).

I'll give you some of the code. Try filling in the rest (in the ... parts).


mkDecreasing [] = ... -- for an empty list
mkDecreasing (x:xs) = mkDecreasing' x xs

mkDecreasing' x [] = ... -- base case for a recursive call to mkDecreasing'
mkDecreasing' x (y:ys)
| ... = ...
| ... = ...

DigitalDreams
06-19-2008, 08:27 PM
Hi All,

I am trying to implement the 6 basic logic gates as functions in haskell.

I have so far managed to get an AND, OR gate to work but i am struggling with the others.

Can anyone point me in the direction of some useful websites that could help me with this.

Also how would you implement "not equal to" i thought it would be the following but it did not work.

> notgate :: Bool -> Bool
> notgate a = /=a -- a is not equal to a

Trinithis
06-20-2008, 06:21 AM
not :: Bool -> Bool
not True = False
not False = True

p XOR q = (p OR q) AND (NOT (p AND q))
p NAND q = NOT (p AND q)
p NOR q = NOT (p AND q)

DigitalDreams
06-20-2008, 12:02 PM
Thanks for the advice, can you confirm if i have coded the following correctly.

> nand :: Bool -> Bool -> Bool
> nand a b = not (a and b)

> nor :: Bool -> Bool -> Bool
> nor a b = not (a and b)

> xor :: Bool -> Bool -> Bool
> xor a b = (a or b) and (not (a and b))

Thanks

Trinithis
06-20-2008, 06:21 PM
To make non-infix functions infix (aka, any non-operators), you need to surround them with backquotes (`).


nand :: Bool -> Bool -> Bool
nand a b = not (a `and` b)

nor :: Bool -> Bool -> Bool
nor a b = not (a `and` b)

xor :: Bool -> Bool -> Bool
xor a b = (a `or` b) `and` (not (a `and` b))

DigitalDreams
06-20-2008, 07:38 PM
Thanks for the information, i did try that before i posted but it still did not work.

for some reason the not function works on its own, but when i put it with the following code below, i get the following error.

Syntax error in input (unexpected `=`)

> and :: Bool -> Bool -> Bool
> and a b = a&&b

> or :: Bool -> Bool -> Bool
> or a b = a||b

> not :: Bool -> Bool
> not True = False
> not False = True

> nand :: Bool -> Bool -> Bool
> nand a b = not (a `and` b)

> nor :: Bool -> Bool -> Bool
> nor a b = not (a `and` b)

> xor :: Bool -> Bool -> Bool
> xor a b = (a `or` b) `and` (not (a `and` b))