Beginning with haskell

Hi all,

First of all I am back after a long absence (almost one and half months). I tried to push myself and keep writing the blog regularly but couldn’t do it due to various reasons. Anyway instead of discussing that I thought best would be to complete the task left incomplete – writing about haskell and functional programming.

Second thing is; as I am writing after a long time this is going to be a long post. I being myself a blog reader understand it is not good to write tiring post but excuse me for this one.

So before beginning lets see what you will need – obviously a haskell compiler. ghc is the widely used compiler for haskell and is easy to install. I use fedora as my OS and it provides it in its repository so fedora users should run this command – ‘yum install ghc’  with super user privilleges. For others download it from here http://www.haskell.org/ghc/download_ghc_7_LOG IN4_1. All haskell files should bear a .hs file extension. If you want you may run it in interactive mode like python by typing ghci in terminal.

Then any text editor will do – vim, emacs, gedit. That’s all you will be needing

Let’s begin now – instead of taking the approach of learning the language in a trivial way like how to declare a variable and all I prefer it with examples. Let’s take a simple example of finding square of entered integer.

mysquare x = x*x 2     –this is a oneliner for calculating square of entered number

Note: — is used to represent comment till end of the line while {- ….. -} is a nestable comment.

Just save it in some file square.hs and load it in ghci like this

Prelude> :l square.hs
[1 of 1] Compiling Main ( square.hs, interpreted )
Ok, modules loaded: Main.

*Main> mysquare 4
16
*Main> mysquare 4.2
17.64

*Main> mysquare (mysquare 2)
16

–:l <program name> loads the script in interpreted way. We will see compiling in detail later

This helps us to learn a lot. First of all look closely at the syntax of defining a function.

<function name> <parameter 1> <parameter 2> …. = <function definition>

some more simple functions could be mycube x = x*x*x or myadd x y = x + y

Now look at the sample output I have provided mysquare accepted 4 as input and returned 16 as output while also it did accept 4.2 and returned 17.64 correctly. This is something called as ‘type inference’. As mysquare uses ‘ * ‘ operation x is inferred to be a number. But as multiplication is for both integers as well as floating point numbers our program works for both. {- C programmers note this -} So basically no need to declare x as a number; haskell would figure it out. Look at this –

*Main> mysquare “hi”

<interactive>:1:1:
No instance for (Num [Char])
arising from a use of `mysquare’

Since hi is a string it won’t accept the input since x is inferred to be a number.

Moving on check out the mysquare (mysquare 2) part – this is what I call syntactic sugar. To write this in C it should be mysquare(mysquare(2)). This simplifies things and syntax becomes much easier.

Now as many of you must have understood by now basic arithmetic operators are same. Most of the operators in C are the same in haskell. Haskell also provides in built function for many more such simple things which we will look in future. Now considering we have gained some knowledge lets go to writing a little more complex program – finding out factorial of a given number

factorial x = if x == 0 || x == 1 then 1 else x*factorial (x-1)

save this one liner in some factorial.hs

sample output:

Prelude> :l factorial.hs
[1 of 1] Compiling Main ( factorial.hs, interpreted )
Ok, modules loaded: Main.
*Main> factorial 0
1
*Main> factorial 1
1
*Main> factorial 2
2
*Main> factorial 13
6227020800

Just look at how elegant the syntax is. I am sure that this example is sufficient to check out the if-else control structure syntax. This also marks the first introduction of recursion something that needs to be used more and more during functional programming.

I assume that this gives a basic idea of haskell programming and its syntax. Let’s dive a little deeper into simple arithmetic problems and have a introduction to list handling next time.

Till then keep your brains functioning 😀

– Payas

Advertisements

One response to “Beginning with haskell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s