NETB131 Programming Project
Programming language Haskell


Ivailo Vasilev Hristov, F31854

 

1. History and Special Features

Haskell is an advanced purely functional programming language of the Lisp family. Functional programming means that programs are executed by evaluating expressions. Haskell is based on a logical theory of computable functions called the lambda calculus:

            “Lambda calculus is a formal language capable of expressing arbitrary computable functions. In combination with types it forms a compact way to denote on the one hand functional programs and on the other hand mathematical proofs.” (Barendregt)

Features of functional languages:

- Higher-order functions: those are functions that take other functions as their arguments.

- Purity: some functional languages allow expressions to yield actions in addition to return values.

- Lazy evaluation: since pure computations are referentially transparent they can be performed at any time and still yield the same result. This makes it possible to defer the computation of values until they are needed, that is to compute them lazily.

- Recursion: recursion is heavily used in functional programming as it is the canonical and often the only way to iterate. Loops are naturally expressed using tail recursion.

History of Haskell:

• In the 1930s, Alonzo Church developed the lambda calculus, a simple but powerful mathematical theory of functions.

• In the 1950s, John McCarthy developed Lisp (“LISt Processor”), generally regarded as being the first functional programming language. Lisp had some influences from the lambda calculus, but still adopted variable assignments as a central feature of the language.

• In the 1960s, Peter Landin developed ISWIM (“If you See What I Mean”), the first pure functional programming language, based strongly on the lambda calculus and having no variable assignments.

• In the 1970s, John Backus developed FP (“Functional Programming”), a functional programming language that particularly emphasised the idea of higher-order functions and reasoning about programs.

• Also in the 1970s, Robin Milner and others developed ML (“Meta-Language”), the first of the modern functional programming languages, which introduced the idea of polymorphic types and type inference.

• In the 1970s and 1980s, David Turner developed a number of lazy functional programming languages, culminating in the commercially produced language Miranda (meaning “admirable”).

• In 1987, an international committee of researchers initiated the development of Haskell (named after the logician Haskell Curry), a standard lazy functional programming language.

• In 2003, the committee published the Haskell Report, which defines a long-awaited stable version of Haskell, and is the culmination of fifteen years of work on the language by its designers.

2. "Hello World" Program

 

main = putStrLn "Hello, world!"

 

3. Data Types and Assignment Operator

 

Basic types:

 

—       Bool - logical values. This type contains the two logical values False and True.

—       Char - single characters.

—       String - strings of characters. This type contains all sequences of characters, such as "abc", "1+2=3", and the empty string "". Strings of characters must be enclosed in double quotes " ".

—       Int - fixed-precision integers. This type contains integers such as в€’100, 0, and 999.

—       Integer - arbitrary-precision integers. This type contains all integers, with as much memory are necessary being used for their storage, thus avoiding the imposition of lower and upper limits on the range of numbers. For example, evaluating 2 ↑ 31 :: Integer using any Haskell system will produce the correct result.

—       Float - single-precision floating-point numbers. This type contains numbers with a decimal point, such as в€’12.34, 1.0, and 3.14159, with a fixed amount of memory being used for their storage.

 

List types:

 

A list is a sequence of elements of the same type, with the elements being enclosed in square parentheses and separated by commas.

 

[False, True, False] :: [Bool ]

[’a’, ’b’, ’c’, ’d’] :: [Char ]

["One", "Two", "Three"] :: [String ]

 

Tuple types:

 

A tuple is a finite sequence of components of possibly different types, with the components being enclosed in round parentheses and separated by commas.

 

(False, True) ::(Bool , Bool )

(False, ’a’, True) :: (Bool , Char, Bool )

("Yes", True, ’a’) :: (String, Bool , Char)

 

Function types:

 

A function is a mapping from arguments of one type to results of another type.

 

not :: Bool -> Bool

isDigit :: Char -> Bool

 

Assignment Operator:

 

In Haskell there are no variables, Haskell uses one-time assignment, where a function or a program receives a value once, and that value does no change throughout the course of its life. Computation is not done by changing stored values in variables.

4. Basic Control Flow

 

Conditional operator: В In Haskell, there is only an if expression and no if statement, the else part is compulsory.

 

if expression1 then expression2 else expression3

 

A conditional expression has the form if e1 then e2 else e3 and returns the value of e2 if the value of e1 is True, e3 if e1 is False, and _|_ otherwise.

 

 

main = do putStrLn "How much is 2 + 2?"

В В В В В В В В В  x <- readLn

В В В В В В В В В  if x == 4

В В В В В В В В В В В В В  then putStrLn "You're right!"

В В В В В В В В В В В В В  else putStrLn "You're wrong!"

 

 

Haskell provides no special syntax for looping. All looping is achieved via recursion, where a function calls itself.

 

Task: Input an integer number n and output the sum: 1+22+32+...+n2. Use input validation for n to be positive.

 

 

module Main where

 

main = do putStrLn "Enter an integer:"

В В В В В В В В В  n <- readLn

В В В В В В В В В  if n < 0 

В В В В В В В В В В В В В В then putStrLn "Bad input, you should enter a positive number!"

В В В В В В В В В В В В В  else print (sum.squares $ [1..n])

В  where squares = map (^2)

 

 

 

5. Functions

 

A simple function definition is formed by the name of the functions followed by zero or more parameters, an equals sign and then an expression. Paradigm:

 

name parameter1 parameter2 ... parametern = expression

 

Examples:

 

quadRoot a b c = (-b + sqrt d) / (2 * a)

     where d = b^2 – 4*a*c

 

 

add :: Integer -> Integer -> Integer

add x y = x + y

 

6. Arrays

Haskell supports just one array constructor type, namely Array, which gives immutable boxed arrays. For example calculating the squares of numbers from 1 to 100 can be done the following way:

 

module Array where

 

import Data.Array

 

squares :: Array Int Int

squares = array (1,100) [(i, i*i) | i <- [1..100]]

 

7. Compilers

 

The most widely used it Glasgow Haskell Compiler, there are also nhc98, the Essential Haskell Compiler, Helium, Jhc, York Haskell Compiler (not finished yet), HBI and HBC the latter is no longer supported is for Haskell 1.4

8. Projects and Software Written in
Haskell

The following software is written in Haskell:

—       Glasgow Haskell Compiler

—       nhc98

—       Hoogle (a search engine)

—       Xtract ("grep"-like command-line tool for searching XML and HTML documents)

—       HaXml

—       HsColour

—       lp2fp

—       House (an operating system)

—       Yi (editor)

—       Cabal

—       Haddock

 

9. Standard

 

Haskell 98, last revision was published by Cambridge University Press, as a book “Haskell 98 language and libraries: the Revised Report” in January 2003

10. References

 

1.       http://haskell.org

2.       Wikipedia

3.       Doets, K. & van Eijck, J., 2004. The

Haskell Road
to Logic, Math and Programming. College Publications

4.       Hutton, G., 2005. Programming in Haskell. Cambridge University Press

5.       http://www.cs.mu.oz.au/fpu/haskell-tutorial-1.4-html

6.       http://faculty.uaeu.ac.ae/~brucem/haskell/index.html