Misc Links
Forum Archive
News Archive
File DB
 

Ads
 

Advertisement
 

Latest Forum Topics
wow 56 k modems are
Posted by Red Squirrel
on Oct 14 2013, 11:52:23 pm

I Need A Program
Posted by rovingcowboy
on Sep 23 2013, 5:37:59 pm

having trouble witn lan
Posted by rovingcowboy
on Sep 23 2013, 5:40:56 pm

new problem for me
Posted by rovingcowboy
on Sep 23 2013, 5:54:09 pm

RBC Royal Bank
Posted by Red Squirrel
on Aug 13 2013, 6:48:08 pm

 

Introduction to Haskell
Functional programming with Haskell
By Chris Dutton


Disclaimer

This is yet another attempt to bring the ideas of functional programming to the masses here, and an experiment in finding ways to make it easy and interesting to follow.

Your feedback would be good as a means of judging my progress.

Why should you care?

Functional programming is fundamentally a very different way of thinking about programming. After all, we can learn several languages and pat ourselves on the back, but if the only real difference is syntactic, then we're not really being challenged.

What do I need?

You'll need either a Haskell interpreter or compiler. For learning purposes the Hugs Haskell interpreter is fine.

You can download it from: http://www.haskell.org/hugs/

It installs as easily as any other Windows program, and an entry will be created in the Start menu under: Start -> Programs -> Hugs98.

If you're running Windows 98 or ME, it would probably be wise to restart your computer, just to make sure everything the installer did takes effect. Windows 2000 and XP are pretty good about immediately applying the changes.

Oh, and you'll want a good text editor. I suggest TextPad with the Haskell syntax coloring file. Directions for installing the syntax file are available.

A quick look at Hugs

Start -> Programs -> Hugs98 -> Nov2003 -> Hugs (Haskell98 mode)

So, at startup of Hugs we've got some ASCII art, copyright information, some basic usage tips, and a prompt. This is a good start.

What can we do with this?

Well, we can evaluate expressions and see their results.

What's an expression?

An expression is a little bit of code that takes one or more values and gives you a new value.

Consider this very simple example.

Moving on

But really, we could do basic math all day and be bored out of our skulls, so let's look at putting together a source file where we can build more complex things.

A Haskell source file is just a text file containing Haskell code. The extension we use is ".hs".

So, what will our source file contain? A simple hello world program.

module Main where

main = putStrLn "Hello, world!"

What do we have here?

Well, first of all we have to deal with the fact that Haskell code is organized into modules. Modules allow us to easily reuse code in other programs, and they allow the actual language itself to be relatively simple. The name of the file should match the name of the module. Here the module is named "Main".

Next we have the "main" function, the center of activity, as in many other programming languages. The ease of creating this function shouldn't come as any surprise given the fact that Haskell focuses on functions.

putStrLn "Hello, world!"

Here we simply use the putStrLn function to print a string to the screen on its own line. The similar putStr function does the same, but doesn't automatically skip to a new line.

Testing the code

So, how do we run the code in this file?

Well, open up your trusty Command Prompt window and "cd" to the directory where you saved your Main.hs file.

Once you're there, start Hugs by simply typing "hugs" and hitting enter. Again we're back to:

To load the "Main" module we simply:

Prelude> :load Main
Main>

The prompt has changed to indicate we're now in the Main module, rather than the Prelude module.

And to run the main function:

Main> main
Hello, world!

Main>

So, we've seen a little bit of Haskell

Is it scary?

If you say yes, that's not bad. New things can be scary. You'll get over it.

The real question, though, is: where do we go from here?

Well, since Haskell is a functional programming language, I'm thinking it might be good to see some more functions.





Next Page
spacer
19892 Hits Pages: [1] [2] [3] [4] [5] [6] 6 Comments
spacer


Latest comments (newest first)
Posted by genea on December 12th 2005 (13:14)
Very good article, with the part about type classes and the ability for a data type derived from a given class being able to automatically inherit the method (functions) from the class definition. I guess I missed that in the mountain of literature I had read and had been up till now missing the benefit of this info... I liked dobbing about with HASKELL and this just makes me believe, that it will have a bright future ahead, or one of the languages derived from it, such as CLEAN.
Thanks again.. and I too would like to see the next six!!
gene

spacer
Posted by wtd on June 06th 2005 (19:10)
QUOTE (HHH @ Jun 4 2005, 12:40 PM)
Hi,
how can I do a line skip in a String. I mean that one String ouput looks like this:

Hello, (line skip)
....

CODE
putStrLn "Hello"


The "Ln" means "line".

If you just want to skip a line...

CODE
putStrLn ""


Or you could make that a function.

CODE
skipLine = putStrLn ""

spacer
Posted by wtd on January 01th 2005 (16:44)
QUOTE (fred laforge @ Jan 11 2005, 10:03 AM)
great tutorial -- when will you post the next 6 lessons? :-)

Heh. That one took a bit out of me, so there might just be smaller updates for a while. smile.gif.

spacer
Posted by wtd on January 01th 2005 (02:12)
In my introduction to Haskell I createded a simple function greeting, which takes a name as a string and formulates a greeting.

CODE
greeting :: String -> String
greeting name = "Hello, " ++ name ++ "!"


And then, I created a greet function which takes a name and prints the greeting for it.

CODE
greet :: String -> IO ()
greet name = putStrLn (greeting name)


The latter function I rewrote as:

CODE
greet :: String -> IO ()
greet name = putStrLn $ greeting name


Now, the argument "name" should appear quite redundant in that last example. It is. What if, instead, we could simply combine the two functions, "greeting" and "putStrLn".

Haskell provides an easy mechanism for doing so.

CODE
greet :: String -> IO ()
greet = putStrLn . greeting


Now, we can make a similar observation about the greeting function.

CODE
greeting :: String -> String
greeting name = "Hello, " ++ name ++ "!"


This is really two functions, since operators are just functions. Given the order of evaluation, this could be rewritten:

CODE
greeting :: String -> String
greeting name = "Hello, " ++ (name ++ "!")


Now, since we can partially apply functions - give them one argument, and get back a function which takes another argument and gives us the rest - we can rewrite this as:

CODE
greeting :: String -> String
greeting = ("Hello, " ++) . (++ "!")


Just as in the original, the function takes a string, appends "!" to it, then prepends "Hello, " to that.

spacer
Posted by wtd on January 01th 2005 (19:54)
Oh, and when you get around to writing something in Haskell, and you have questions, feel free to ask. smile.gif
spacer
View all comments
Post comment


Top Articles Latest Articles
- What are .bin files for? (669062 reads)
- Text searching in linux with grep (161180 reads)
- Big Brother and Ndisuio.sys (150471 reads)
- PSP User's Guide (139547 reads)
- SPFDisk (Special Fdisk) Partition Manager (117240 reads)
- How to Use MDADM Linux Raid (188 reads)
- What is Cloud Computing? (1225 reads)
- Dynamic Forum Signatures (version 2) (8769 reads)
- Successfully Hacking your iPhone or iTouch (18714 reads)
- Ultima Online Newbie Guide (35906 reads)
corner image

This site best viewed in a W3C standard browser at 800*600 or higher
Site design by Red Squirrel | Contact
© Copyright 2014 Ryan Auclair/IceTeks, All rights reserved