Sep 8, 2010

Haskell programming language

Haskell is an advance purely functional language. This language is not like java or c.

Install haskell compiler in fedora
$yum install ghc

After the installation you can type
$ghci
to run haskell in interactive prompt.

Writing your own functions
You can write Haskell source code and load it into the interpreter. To do that you should;
1. Write a Haskell code in the working directiry including 'import Data.Maybe'at the top of your code if that code need modules.
2. Sav the code with the extention of .hs.
3. Run ghci command and at the prompt type ":l .sh " (l= L in lowercase)

Writing a basic function
In c ---->
int foo(int bar){
return bar*10-4
}
In Haskell -------->
foo :: Int->Int
foo bar = bar * 10 +4

This means that foo is a integer and it returns a integer value.

Lists
let list=[2,3,4,5,7]
This is a list defined in Haskell. You can add values to the front of the list by doing
23:14:list
This will add 23 and 14 to the list and its output will like this.
[23,14,2,3,4,5,7]

Note:- If you need to get the file type of a variable and function you can type ':t'like this;
:t list
list - zipping and unzipping
This will take two lists zips them and returns a tupples.
zip [1,2,3]["aaa","bbb","ccc"]
the output will be like this,
[(1,"aaa"),(2,"bbb"),(3,"ccc")]

List - tali
This will alter the first item in the list. If you type as;
let list2=[1,2,3]
tail list2
This will give the output as;
[2,3]
In Haskell it is not updating the values, it only computes and give the output.

List - !!
Try this:
list2 !! 0
list2 !! 1
This will give the output as
1
2
!! is the sign of index operator in Haskell.

No comments:

Post a Comment

Your comments are always welcome ...