haskell!. i ntroduction w hat is h askell ? haskell is a computer programming language. in...

14
HASKELL!

Upload: erick-wilkerson

Post on 03-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: HASKELL!. 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: HASKELL!. 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: HASKELL!. 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: HASKELL!. 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: HASKELL!. 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: HASKELL!. 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: HASKELL!. 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: HASKELL!. 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: HASKELL!. 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: HASKELL!. 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: HASKELL!. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,

TRY IT!

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

TRY HASKELL! (SEATWORK #5)

Go to: http://tryhaskell.org

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

FINISH UNTIL YOU GET THIS FROM CHAPTER 4

When done save a screen shot and name it “FirstnameLastname – SWtry haskell.jpg”

Upload it to your wiki folder and update your wiki page

Page 14: HASKELL!. 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