Introduction To Haskell
Lecture 1
An Unexpected Journey™
Using These Slides
Every slide has a secret note.
On Chrome : press F12, then click Console
On IE : press F12, then click Console
On Firefox : Ctrl+Shift+k
Shortcut Keys:
↓, PgDn, n, j
next slide
↑, PgUp, p, k
prev slide
Esc
enables ctrl+f globally
Hi there! This is a secret lecture note. Every slide has a little blurb of text like this!
Instructor
Nishant Shukla
ns4av@virginia.edu
Watchful Guardian
Professor Jack Davidson
jwd@virginia.edu
Email Nishant for questions on homework, class structure, and general class related things. Email Professor Davidson for anything else. If you would like to consider CS reserach, please feel free to ask Professor Davidson for advice. His areas of interest include computer security, compilers, code generation, optimization, embedded systems, and computer architecture.
Sponsored by...
The Engineering Student Council at UVa helped setup and advertise this class.
Quite a Turnout
The class started out with just 30 open seats. Enrollment filled up quickly and we didn't expect this level of interest, so we increased enrollment to 45. Two days later the class was full again, so we finally increased enrollment to 60, doubling the original size. This class is at max capacity.
There are 1012 people signed up as of 1/14/13.
Over 121 countries, and over 50 universities:
UC Berkeley
Cambridge
Georgia Tech
RPI
University of Florida
Oxford
NYU
University of Waterloo
University of Florida
Cornell
USC
Virginia Tech
MIT
Columbia
University of Maryland
Duke
Carnegie Mellon University
UT Dallas
Wesleyan University
Caltech
Imperial College
Brown University
IIT
... and more than 30 others
If the previous slide didn't persuade you about the popularity of Haskell I hope these will prove sufficient.
Within 2 Days of Traffic
This is further evidence of Haskell's recent growing popularity.
This chart is from September 2012, and it shows Haskell's popularity compared to many other languages.
Java is enterprisey, C is swift, PHP gets the job done, Ruby has a great community, and Haskell makes you think.
Syllabus
Find it on the class website
(Slides, homework, and announcements)
Meet once a week, Tuesday 5:30pm
Attendance: 40pts (5pts/day)
Homework: 30pts
Project: 30pts
These lectures are based off 'Real World Haskell' and 'Learn You a Haskell.' For the most effective use of these lectures, you should follow along with these two books.
More Than a Language
I know why you're here. ...why you hardly sleep, why night after night, you sit by your computer.
Where have you seen Haskell before? What made you come here?
History
Alonzo Church invented λ-calculus
Church made fundamental contributions to theoretical computer science. He's also known for the Church-Turing thesis, and for proving the undecidability of the Halting problem.
Then John McCarthy invented Lisp
Lisp had one of the first implementation of lambda-calculus introduced by Church. Applied computer science met theoretical computer science.
The Origin
Haskell was made by a committee of really smart people to define an open standard*
More than 20 years old
MIT
Chalmers University
Mitre Corp
Victoria University of Wellington
Simon Fraser University
University of Cambridge
Yale University
University of Glasgow
Microsoft Research Ltd
Most research languages don't catch on. It's been more than 20 years and Haskell is still relevant today.
Features
Purely functional
Statically typed
Lazy
These words may look confusing, but it's important you understand them in the coming slides.
1. Purely functional
Every input has a corresponding output
f(x) = x² + 1
Powerful function compositions
g(x) = x - 1
g(f(x)) = x²
PURE
That means no side effects
A function will never modify a global variable
Order doesn't matter!
Easy concurrency
Notice the emphasis on PURE. Haskell is not just a functional language, but it is a purely functional language!
Let's be a little formal
f is function from a set A to a set B.
f :: A → B
What's the domain, codomain, and range?
domain(f) =
codomain(f) =
range(f) ⊆
Answer:
domain(f) = A
codomain(f) = B
range(f) ⊆ B
Haskell functions behave similarly to mathematical functions
Functional:
Haskell, Lisp, ML, Scheme, Erlang
Focuses on the high-level "what"
Imperative:
C++, Java, Python, Pascal
Focuses on the low-level "how"
Functional languages only care about 'what' is done. Imperative languages often deal with 'how' memory is managed or 'how' data is organized.
What does this code do?
void f(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;
f( a, lo, l-1 );
f( a, l+1, hi );
}
}
*
This is a quicksort in C. Notice the low level array manipulations.
Sort in Haskell
qsort :: Ord a => [a] -> [a]
qsort [] = []
qsort (p:xs) = (qsort lesser) ++ [p] ++ (qsort greater)
where
lesser = filter (< p) xs
greater = filter (>= p) xs
No variable assignments,
No array indices,
No memory management!
Haskell is elegant. This is the first time you've seen Haskell code in lecture, so don't worry about understanding it. Just awe at the readability of the code :)
No Side Effects
Haskell code:
count :: List -> Int
When this function runs on a List, we get back an Int. No more, no less.
C++ code:
int count( List l ) { ... }
C++ however doesn't promise integrity. Maybe it's doing file IO or updating a global variable. You can't trust the code won't burn down your house.
Haskell promises that your code will not burn down your house while computing the output. There are no surprises.
2. Statically Typed
f x = x² + 1
f :: Int → Int
There is never confusion about types
(Bool, Int, Char, etc)
Strong formalism. The proof is the code.
If your code compiles, you're 99% done
The compiler infers the type of every function before the code is run. In other words, the compiler catches a lot of your bugs for you.
Types
Every function in haskell has a Type signature.
foo :: Int -> String
I don't know what foo means,
but I know what it does!
As long as the type signature is appropriate, you can use the function anywhere. You might be thinking of methods in Java or C++ - that's a fine analogy for now.
3. Lazy
Haskell is lazy... in a good way!
Lazy?
Nothing is evaluated unless necessary
head (sort ls)
The list will only be sorted enough to find the minimum
Allows infinite data structures
[1..]
This allows Haskell to be pretty fast without manipulating low-level code.
Who uses Haskell? *
Technical recruiters are impressed by Haskell. By the end of these lectures you'll have the confidence to put Haskell on your resume.
Language shapes the way we think, and determines what we
can think about.
- Benjamin Lee Whorf
The limits of my language mean the limits of my world.
- Ludwig Wittgenstein
A language that doesn’t affect the way you think about programming, is not worth knowing.
- Alan Perlis
Slide from ed.ac.uk
Takes these quotes to heart.