36
Haskell being lazy with class Tiago Babo PPRO@FEUP 2012

Haskell - Being lazy with class

Embed Size (px)

DESCRIPTION

Presentation about Haskell.

Citation preview

Page 1: Haskell - Being lazy with class

Haskellbeing lazy with class

Tiago BaboPPRO@FEUP 2012

Page 2: Haskell - Being lazy with class

Lisp/Scheme1959

ML/OCaml1979

Miranda1985

Haskell 982003

functional language

statictyping

lazyevaluation

History

Page 3: Haskell - Being lazy with class

There are two main Haskell implementations:

▹ GHC. Probably the most popular, compiles to native code on a number of different architectures.

▹ Hugs. It’s a bytecode interpreter and the most portable and lightweight of the Haskell implementations.

History

Page 4: Haskell - Being lazy with class

Related languages

There are some languages inspired by Haskell:

▹ Different type system: Epigram, Agda.

▹ JVM-based: Frege, Jaskell.

▹ Other related languages: Curry.

▹ Testbed for many new ideas: Parallel Haskell, Eager Haskell, Generic Haskell, O’Haskell, Hume, Scotch, Disciple...

Page 5: Haskell - Being lazy with class

haskell

Haskell is a pure functional language. It means that:

▹ Variables never change after definition.

▹ Functions don’t have side effects.

▹ Functions always return the same output given the same input.

Page 6: Haskell - Being lazy with class

haskell

What haskell offer to the programmer?

▹ Purity. It doesn’t allow any side-effects.

▹ Laziness (non-strict). Nothing is evaluated until it has to be evaluated.

▹ Strong static typing. The compiler automatically infers a precise type for all values and it permits no implicit type conversions.

▹ Elegance. Stuff just work like you’d expect it to.

Page 7: Haskell - Being lazy with class

haskell and bugs

Haskell programs have fewer bugs because Haskell is:

▹ Pure. There are no side effects.

▹ Strongly typed. There can be no dubious use of types.

▹ Concise. Programs are shorter which make it easier to “take it all” at once.

Page 8: Haskell - Being lazy with class

haskell and bugs

Haskell programs have fewer bugs because Haskell is:

▹ High Level. Haskell programs most often reads out almost exactly like the algorithm description.

▹ Memory Managed. There’s no worrying about dangling pointers, the Garbage Collector takes care of all that.

▹ Modular. Haskell offers stronger and more “glue” to compose your program from already developed modules.

Page 9: Haskell - Being lazy with class

Haskell has some disadvantages:

▹ Hard to learn and master. It’s even harder without a proper computer science background.

▹ You can’t write haskell-like code in other programming languages.

▹ Lacks libraries and support from who mantain and improve them.

haskell cons

Page 10: Haskell - Being lazy with class

code examples

Page 11: Haskell - Being lazy with class

fibonacci1 1 2 3 5 8 13 21 34 ..

Page 12: Haskell - Being lazy with class

List <int> fib(int i) {List <int> seq = new ArrayList(n);seq[0] = 1;seq[1] = 1;for(int i = 2; i < n; i++) {seq[i] = seq[i-2] + seq[i-1];}return seq;}

Java

Page 13: Haskell - Being lazy with class

fib = 1:1:zipWith (+) fib (tail fib)

Haskell

Page 14: Haskell - Being lazy with class

List <int> seq = new ArrayList(n);seq[0] = 1;seq[1] = 1;for(int i = 2; i < n; i++) {seq[i] = seq[i-2] + seq[i-1];

}

Java VS haskell

fib = 1:1:zipWith (+) fib (tail fib)

Page 15: Haskell - Being lazy with class

List <int> seq = new ArrayList(n);seq[0] = 1;seq[1] = 1;for(int i = 2; i < n; i++) {seq[i] = seq[i-2] + seq[i-1];

}

Java VS haskell

fib = 1:1:zipWith (+) fib (tail fib)

Page 16: Haskell - Being lazy with class

List <int> seq = new ArrayList(n);seq[0] = 1;seq[1] = 1;for(int i = 2; i < n; i++) {seq[i] = seq[i-2] + seq[i-1];

}

Java VS haskell

fib = 1:1:zipWith (+) fib (tail fib)

Page 17: Haskell - Being lazy with class

List <int> seq = new ArrayList(n);seq[0] = 1;seq[1] = 1;for(int i = 2; i < n; i++) {seq[i] = seq[i-2] + seq[i-1];

}

Java VS haskell

fib = 1:1:zipWith (+) fib (tail fib)

Page 18: Haskell - Being lazy with class

How it works?

fib = 1:1:zipWith (+) fib (tail fib)

take 2 fib -> ?

fib = 1 : 1 : ..tail fib = 1 : ..zipWith (+) fib (tail fib) = ..

take 2 fib -> [1,1]

lazy evaluation

Page 19: Haskell - Being lazy with class

fib = 1:1:zipWith (+) fib (tail fib)

take 3 fib -> ?

fib = 1 : 1 : 2 : ..tail fib = 1 : 2 : ..zipWith (+) fib (tail fib) = 2 : ..

take 3 fib -> [1,1,2]

How it works?

Page 20: Haskell - Being lazy with class

fib = 1:1:zipWith (+) fib (tail fib)

take 10 fib -> ?

fib = 1 : 1 : 2 : 3 : ..tail fib = 1 : 2 : 3 : ..zipWith (+) fib (tail fib) = 2 : 3 : ..

...

fib = 1 : 1 : 2 : 3 : 5 ..tail fib = 1 : 2 : 3 : 5 : ..zipWith (+) fib (tail fib) = 2 : 3 : 5 : ..

take 10 fib -> [1,1,2,3,5,8,13,21,34,55]

How it works?

Page 21: Haskell - Being lazy with class

and how about the types?

fib = 1:1:zipWith (+) fib (tail fib)

Page 22: Haskell - Being lazy with class

and how about the types?

int

int

fib = 1:1:zipWith (+) fib (tail fib)

Page 23: Haskell - Being lazy with class

and how about the types?

int

intList<int>

fib = 1:1:zipWith (+) fib (tail fib)

Page 24: Haskell - Being lazy with class

and how about the types?

int

intList<int>

List<int>

List<int>

fib = 1:1:zipWith (+) fib (tail fib)

Page 25: Haskell - Being lazy with class

and how about the types?

int

intList<int>

List<int>

List<int>List<int>

fib = 1:1:zipWith (+) fib (tail fib)

Page 26: Haskell - Being lazy with class

Quicksort[3,2,1,4] -> [1,2,3,4]

Page 27: Haskell - Being lazy with class

// To sort array a[] of size n: qsort(a,0,n-1)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 ); }}

C

Page 28: Haskell - Being lazy with class

qsort(p:xs) = (qsort lesser) ++ [p] ++ (qsort greater) where lesser = filter (< p) xs greater = filter (>= p) xs

Haskell

Page 29: Haskell - Being lazy with class

qsort(p:xs) = qsort [x | x<-xs, x<p] ++ [p] ++ qsort [x | x<-xs, x>=p]

Haskell

Page 30: Haskell - Being lazy with class

Factorial5! = 1x2x3x4x5

Page 31: Haskell - Being lazy with class

fac 0 = 1fac n | n > 0 = n * fac (n-1)

Haskellpattern

matching

fac n = product [1..n]

fac n = foldr1 (*) [1..n]

fac n = if n == 1 then 1 else n * fac (n-1)

Page 32: Haskell - Being lazy with class

fac n = case n of0 -> 1n -> n * fac (n-1)

Haskell

facs = scanl (*) 1 [1..]

fac n = facs !! n

Page 33: Haskell - Being lazy with class

haskell in industry

▹ Aerospace, defense, finance, web startups, and hardware design firms.

Haskell is used in many areas:

Page 34: Haskell - Being lazy with class

haskell in industry

automate processing of internet abuse complaints

programmatically manipulating a PHP code base

procedural city generation and simulation market

analysis of cryptographic protocols

Page 35: Haskell - Being lazy with class

haskell in industry

measure the counterparty risk on portfolios of financial derivates

job scheduling and brand matching

implement mathematical models and other complex

handwriting recognition system

Page 36: Haskell - Being lazy with class

Haskellbeing lazy with class

Tiago BaboPPRO@FEUP 2012

Links:http://haskell.org/ - Haskell official homepagehttp://youtu.be/cXY4fSA7DnM - Stanford Tutorialhttp://haifux.org/lectures/173/ - An overview of Haskell (Haggai Eran)http://en.wikipedia.org/wiki/Haskell_(programming_language) - Haskell History