H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In...

20
HASKELL

Transcript of H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In...

Page 1: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

HASKELL

Page 2: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

INTRODUCTION

Page 3: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

WHAT IS HASKELL?

Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy, purely functional language, quite different from most other programming languages.

The language is named for Haskell Brooks Curry, whose work in mathematical logic serves as a foundation for functional languages.

Haskell is based on the lambda calculus, hence the lambda is used as its logo.

Page 4: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

FUNCTIONAL PROGRAMMING

In imperative languages you get things done: by giving the computer a sequence of tasks and then

it executes them. While executing them, it can change state. For instance, you set variable a to 5 and then do

some stuff and then set it to something else. You have control flow structures for doing some

action several times. In purely functional programming:

you don't tell the computer what to do as such but rather you tell it what stuff is.

The factorial of a number is the product of all the numbers from 1 to that number, the sum of a list of numbers is the first number plus the sum of all the other numbers, and so on.

Page 5: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

WHAT IS GOOD ABOUT FUNCTIONAL PROGRAMMING? Spreadsheets and SQL are both fairly specialized

languages. Functional programming languages take the same ideas and move them into the realm of general-purpose programming. To get an idea of what a functional program is like, and the expressiveness of functional languages, look at the following quicksort programs. They both sort a sequence of numbers into ascending order using a standard method called "quicksort". The first program is written in Haskell and the second in C.

Whereas the C program describes the particular steps the machine must make to perform a sort -- with most code dealing with the low-level details of data manipulation -- the Haskell program encodes the sorting algorithm at a much higher level, with improved brevity and clarity as a result (at the cost of efficiency unless compiled by a very smart compiler)

Page 6: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

FUNCTIONAL VS. IMPERATIVE

quicksort :: Ord a => [a] -> [a]

quicksort [] = []

quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)

where

lesser = filter (< p) xs

greater = filter (>= p) xs

void qsort(int a[], int lo, int hi) { int h, l, p, t; if (lo < hi) { l = lo; h = hi; p = a[hi]; do { while ((l < h) && (a[l] <= p)) l = l+1; while ((h > l) && (a[h] >= p)) h = h-1; if (l < h) { t = a[l]; a[l] = a[h]; a[h] = t; } } while (l < h); a[hi] = a[l]; a[l] = p; qsort( a, lo, l-1 ); qsort( a, l+1, hi ); }}

Haskell C

Page 7: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

LAZY

Haskell is lazy. That means that unless specifically told otherwise, Haskell won't execute functions and calculate things until it's really forced to show you a result.

That goes well with referential transparency and it allows you to think of programs as a series of transformations on data.

Page 8: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

STATICALLY TYPED

Haskell is statically typed. When you compile your program, the compiler knows which piece of code is a number, which is a string and so on.

Page 9: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

ELEGANT AND CONCISE

Haskell is elegant and concise. Because it uses a lot of high level concepts, Haskell programs are usually shorter than their imperative equivalents. And shorter programs are easier to maintain than longer ones and have less bugs.

Page 10: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

DOES ANYONE USE FUNCTIONAL PROGRAMMING? Software AG, a major German software company, market an expert system (Natural Expert)

which is programmed in a functional language. Their users find it easy to develop their applications in this language, through which they gain access to an underlying database system. It all runs on an IBM mainframe.

Ericsson have developed a new functional language, Erlang, to use in their future telephony applications. They have already written 130k-line Erlang applications, and find them very much shorter and faster to develop.

Amoco ran an experiment in which they re-coded in Miranda, a lazy functional language, a substantial fraction of their main oil-reservoir simulation code, a critical application. The resulting program was vastly shorter, and its production revealed a number of errors in the existing software. Amoco subsequently transcribed the functional program into C++ with encouraging results.

A researcher at the MITRE corporation is using Haskell to prototype his digital signal-processing applications.

Researchers at Durham University used Miranda, and later Haskell, in a seven-year project to build LOLITA, a 30,000-line program for natural-language understanding.

Query is the query language of the O2 object-oriented database system. O2Query is probably the most sophisticated commercially-available object-oriented database query language and it is a functional language.

ICAD Inc market a CAD system for mechanical and aeronautical engineers. The language in which the engineers describe their design is functional, and it uses lazy evaluation extensively to avoid recomputing parts of the design which are not currently visible on the screen. This results in substantial performance improvements.

An incestuous example: the Glasgow Haskell compiler is written in Haskell: a 100,000-line application.

Pugs, the leading perl6 implementation is written in Haskell As is Darcs, a cutting edge distributed revision control system

Page 11: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

DIVING INHaskell Basics

Page 12: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

SIMPLE ARITHMETIC

Try the following expressions: 2 + 15   49 * 100   1892 - 1472   5 / 2   (50 * 100) - 4999   50 * 100 - 4999   50 * (100 - 4999)  

Page 13: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

BOOLEAN ALGEBRA

Try the following expressions: True && False   True && True   False || True   not False   not (True && True)   False

Page 14: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

TESTING FOR EQUALITY

5 == 5   True   1 == 0   False   5 /= 5   False   5 /= 4   True   "hello" == "hello"   True

Page 15: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

EXPRESSION ERRORS

Try the following expressions: 5 + "llama“ 5 == True

What do you get?

Page 16: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

FUNCTION CALLS

succ 8   9   

min 9 10   min 3.4 3.2   max 100 101 

Page 17: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

FUNCTION PRECEDENCE

ghci> succ 9 + max 5 4 + 1   16   ghci> (succ 9) + (max 5 4) + 1   16

Page 18: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

MAKING YOU OWN FUNCTIONS doubleMe x = x + x 

ghci> doubleMe 9   18   ghci> doubleMe 8.3   16.6  

doubleUs x y = x*2 + y*2 ghci> doubleUs 4 9   26   ghci> doubleUs 2.3 34.2   73.0   ghci> doubleUs 28 88 + doubleMe 123   478    

Page 19: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

MORE ON FUNCTIONS

Call functions from other functions: * doubleUs x y = doubleMe x + doubleMe y

More complex functions: ** doubleSmallNumber x = if x > 100                          then x                          else x*2   

Page 20: H ASKELL. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

HASKELL End of Session 1