Learning Haskell – Tutorial 1


Haskell is the functional programming language (FP). FP allows users to write code in high level abstraction without considering too much implementation details. For example, the recursion is generally considered inefficiency in most programming languages, but Haskell handles recursions quite well (e.g. Tail Recursion) and the recursion is used to implement the loops in Haskell.

Download the Haskell interpreter and launch it, you will have a greeting (prompt) e.g. Prelude> if you don’t like it, you can change it using :set prompt Haskell>.

Like other programming languages, the expressions you type in in the interactive mode will be evaluated immediately.

1
2
3
4
Haskell> 355/113
3.1415929203539825
Haskell> "HelloACM.com"
"HelloACM.com"
Haskell> 355/113
3.1415929203539825
Haskell> "HelloACM.com"
"HelloACM.com"

Then, you can use infix expression, which means, putting a function/operator first and then parameters.

1
2
3
4
5
6
Haskell> (+) 1 2
3
Haskell> (*) 5 6
30
Haskell> (^) 2 3
8
Haskell> (+) 1 2
3
Haskell> (*) 5 6
30
Haskell> (^) 2 3
8

The string is concatenated using ++ sign:

1
2
Haskell> putStrLn ("ab"++"bb")
abbb
Haskell> putStrLn ("ab"++"bb")
abbb

At the prompt, type in :edit “frac.hs” to start editing file frac.hs in the editor.

1
2
3
4
5
6
7
8
module Frac where
-- hello this is a comment from HelloACM.com
{- 
   multiple line comment
-}
frac :: Int -> Int
frac 0 = 1
frac n = n * frac (n - 1)
module Frac where
-- hello this is a comment from HelloACM.com
{- 
   multiple line comment
-}
frac :: Int -> Int
frac 0 = 1
frac n = n * frac (n - 1)

And then in the command prompt, invoke the function frac

1
2
3
4
Haskell> frac 3
6
Haskell> frac 4
24
Haskell> frac 3
6
Haskell> frac 4
24

As we see from the output, the frac function is defined to compute the fractal of a given positive integer. The line comment starts with minus sign and the block comments are enclosed by {- and -}.

The frac :: Int -> Int says the frac takes a integer input and returns a integer. The Int type starts with capital letters and if you need a bigger data type, use Integer.

The recursion definition is very clear, no brackets, no if statements. One termination condition frac 0 = 1 and the recursive formula frac n = n * frac (n – 1)

The other way to write this in a similar form like other programming languages is to define everything inside a function, the following is similar to Python.

1
2
3
4
5
6
module Frac where
-- hello this is a comment from HelloACM.com
{- 
   multiple line comment
-}
frac n = if n == 0 then 1 else n * frac (n - 1)
module Frac where
-- hello this is a comment from HelloACM.com
{- 
   multiple line comment
-}
frac n = if n == 0 then 1 else n * frac (n - 1)

You could use command ghc to compile Haskell Programs from the command line, and even on windows, it can compile into a EXE.

1
2
3
4
5
6
7
D:\>type a.hs
main = print "hello, world"
D:\>ghc --make a.hs
[1 of 1] Compiling Main             ( a.hs, a.o )
Linking a.exe ...
D:\>a.exe
"hello, world"
D:\>type a.hs
main = print "hello, world"
D:\>ghc --make a.hs
[1 of 1] Compiling Main             ( a.hs, a.o )
Linking a.exe ...
D:\>a.exe
"hello, world"

In this sense, Haskell is both compiled and interpreted language, like Go (from google).

With Haskell, you write less code but achieve a greater abstraction (ie.. FP allows the programmer to work at a higher level of abstraction). Also, FP enforces clean programme design. FP has important links to formal methods in computer science and FP programs are generally shorter, more elegant and more robust than imperative programmes (e.g. C, Pascal). In FP, the system can do more to spot the errors in your code before it runs (once you make FP programs run, it does pretty much the same as you intend). Programmers using FP beat those using other paradigms in competition.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
659 words
Last Post: Code Snippet Auto Complete on Switch Statement using enum in Visual Studio
Next Post: PageViews and Search Queries in Weekends Plus Traffic Sources Analysis

The Permanent URL is: Learning Haskell – Tutorial 1

Leave a Reply