Haskell - proglan
Download
Report
Transcript Haskell - proglan
HASKELL!
INTRODUCTION
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.
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.
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 generalpurpose 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)
FUNCTIONAL VS. IMPERATIVE
void qsort(int a[], int lo, int hi)
Haskell
{
int h, l, p, t;
if (lo < hi) {
quicksort :: Ord a => [a] -> [a]
l = lo;
quicksort []
p = a[hi];
h = hi;
= []
do {
quicksort (p:xs) = (quicksort
lesser) ++ [p] ++ (quicksort
greater)
while ((l < h) && (a[l] <= p))
l = l+1;
while ((h > l) && (a[h] >= p))
h = h-1;
if (l < h) {
where
t = a[l];
lesser = filter (< p) xs
a[l] = a[h];
a[h] = t;
greater = filter (>= p) xs
}
} while (l < h);
a[hi] = a[l];
a[l] = p;
qsort( a, lo, l-1 );
qsort( a, l+1, hi );
}
}
C
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.
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.
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.
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
TRY IT!
TRY HASKELL! (SEATWORK #9)
Go to:
http://tryhaskell.org
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
HASKELL
End of Session 1