33
S Learn Haskell Team 404: Team not found By: Rodney Anderson & Jeff Klarfeld

Learn Haskell

  • Upload
    aira

  • View
    72

  • Download
    1

Embed Size (px)

DESCRIPTION

Learn Haskell. Team 404: Team not found By: Rodney Anderson & Jeff Klarfeld. What is Haskell ?!?!?. - PowerPoint PPT Presentation

Citation preview

Page 1: Learn Haskell

S

Learn HaskellTeam 404: Team not found

By: Rodney Anderson&

Jeff Klarfeld

Page 2: Learn Haskell

What is Haskell ?!?!?

Haskell is a computer programming language. In particular, it is a polymorphically, statically typed, lazy, and a purely functional language. The language is named after Haskell Brooks Curry. Haskell is based on the lambda calculus, hence the Haskell logo.

Page 3: Learn Haskell

Haskell Brooks Curry

An American mathematician and logician. He is best known for his work in combinatory logic, as well as Curry’s paradox and the Curry-Howard correspondence. There are 3 programming languages named after him Haskell, Brooks, and Curry, as well as the concept of currying (a technique used for transforming functions in mathematics and computer science). He died 1982.

Page 4: Learn Haskell

History Background

In 1985 interest in lazy functional languages grew, and by 1987, more than a dozen non-strict, purely functional programming languages existed. A meeting was held at a conference on Functional Programming Languages and Computer Architecture in Portland. The purpose of the committee was to consolidate the current functional languages into a common one that would serve as a basis for future research in functional language design.

The first version of Haskell (Haskell 1.0) was defined in 1990.

Page 5: Learn Haskell

Haskell Compilers

Glasgow Haskell Compiler (GHC) Utrecht Haskell Compiler (UHC) Haskell Dialects Disciple (DDC) York Haskell Compiler (YHC) Helium

Page 6: Learn Haskell

Haskell Variable Types

IntegerEx: 1,2,3

CharEx: ‘A’, ‘B’, ‘C’

BoolEx: True, False

StringEx: [‘b’, ‘o’, ‘y’] or “boy”

FloatEx: 1.0 , 1.2, 78.4

TupleEx: ( 1 , True)

Page 7: Learn Haskell

Main Features

Lazy Evaluation Pattern Matching List Comprehension Type classes Type polymorphism

Page 8: Learn Haskell

Lists

Syntax[] – This is a empty list[1] – This is a list containing only one element “1”[1,2] – This is a list containing two elements[1..10] – This is a list from 1 to 10[1..] – An infinite list starting from 1 to infinity[2,4 ..]- List of even numbers[[1,2],[3,4]] – A List of lists[‘h’,’e’, ‘l’, ‘l’, ‘o’] – A list of Char A string “hello”

Lists in functions let func (a:as) = [1,2,3] 1:[2,3] 1:2:[3] 1:2:3:[]

Page 9: Learn Haskell

Lazy Evaluation

Lazy Evaluation means that expressions are not evaluated when they are bound to variables, but their evaluation is deferred until their results are needed by other computations.

Haskell Example: Let x = [1..] – this is an infinite list starting from 1

Page 10: Learn Haskell

Pattern Matching

In pattern matching, we attempt to match values against patterns and, if so desired, bind variables to successful matches.

Example: fibonocci fib :: Integer -> Integerfib 0 = 1fib 1 = 1fib (n+2) = fib n + fib (n+1)

-- note n+k pattern only matches a value m >= k, and if it succeeds it binds n to m- k

Page 11: Learn Haskell

Pattern Matching Example

patternMatch :: Char -> StringpatternMatch ‘a’ = “This is the letter a”patternMatch ‘b’ = “This is the letter b”patternMatch ‘c’ = “This is the letter c”

Hep :: [ [Char] ] -> [ [Char]]Hep (w:ws) = w:Hep(ws)Hep (“you”:ws) = “u”:Hep(ws);Hep(“are”:ws) = “r”: Hep(ws);Hep ([]) = [];

Page 12: Learn Haskell

List Comprehension

List comprehension is similar to set comprehension or set builder notation, in Haskell this notation returns a “set” known as a list. Set comprehension;

S = { 2 * x | x >0 , and x <= 10} where x is an integer = { 2, 4, 6, 8 , 10, 12, 14, 16, 18, 20}

In Haskell the above looks like S :: [Integer] -> [Integer]S (xs) = [ 2 * x | x <- xs, x > 0, x <= 10]

Page 13: Learn Haskell

Haskell Programs of List Comprehension

-- This Haskell program adds 1 to a list of IntegersaddOne :: [Integer] -> [Integer] - - declares function addOneaddOne (a:as) = [a +1 | a<- (a:as) ] - - List comprehension

-- map’ this is map primemap’ :: (a -> b) -> [a] -> [b]map’ (f) (as) = [f a | a<- as]

Page 14: Learn Haskell

Polymorphism

A value is polymorphic if there is more than one type it can have.

Haskell Example:The function id:: a -> a contains an unconstrained type variable “a” in its type, and so can be used in a context requiring Char->Char or Integer -> Integer or (Integer -> Bool) -> (Integer -> Bool)

Page 15: Learn Haskell

Type Classes

A type class is a type system construct that supports ad-hoc polymorphism. This first appeared in Haskell.

Define a type class Eq:Class Eq a where (==) :: a -> a -> Bool(/=) :: a -> a -> Bool

Define a function member of Class Eqmember :: (Eq a) => a-> [a] -> Boolmember y [] = Falsemember y (x:xs) = ( x == y) || member y xs

Page 16: Learn Haskell

Static Typing

Static typing has the advantage that errors can be caught before the program runs. Note the programming language C is strongly statically typed.

Page 17: Learn Haskell

Haskell Syntactic Sugar

-- This Haskell program takes a list of functions and apply --them to the list .Notice no parenthesis for parameters. applyFunc :: [(a->Bool)] -> [a] -> [Bool] applyFunc fs as = [f a | f <- fs, a<- as]

X `elem` List = elem X List F _ [] : The “_” means the function takes in

anything else that the pattern doesn’t match.

Page 18: Learn Haskell

Haskell Recursion

Factorial :: Integer -> Integerfactorial 0 = 1factorial n = n * factorial (n – 1)

factorial (3) = 3 * factorial(2) = 3 * ( 2 * factorial (1) ) = 3 * (2 * (1 * factorial (0) )) = 3 * (2 * (1 * 1)) = 3 * (2 * 1) = 3 * 2 = 6

Page 19: Learn Haskell

Haskell Recursion Programs

-- This Haskell program adds 1 to a list of IntegersaddOne :: [Integer] -> [Integer] - - declares function addOneaddOne [] = []addOne (a:as) = a+1: addOne(as)

--map’ this is map primemap’ :: (a -> b) -> [a] -> [b]map’ _ [] = []map’ (f) (a:as) = f (a): map’ (f) (as)

Page 20: Learn Haskell

Lambda Functions

A lambda function is an anonymous function, meaning it’s a function without a name.

Haskell Example: -- This function takes in x and y and adds them\x y -> x + y

Use: (\x y -> x + y) 3 5Result Displayed = 8

-- This is a lambda function to add one to a list\(as) -> [a + 1| a <- as]

Page 21: Learn Haskell

If Then Else Statements

if – then- else statement syntax:

if a then b else c

Haskell program examplefunct :: Char -> Boolfunct (a) = if a == ‘c’ then True else False

Page 22: Learn Haskell

Case

Syntax :case Expression of pattern -> result

pattern -> result pattern -> result

Example:Head’ :: [a] -> a Head’ xs = case xs of [] -> error “No head of list”

(x:_) -> x

Page 23: Learn Haskell

Currying

Currying is the process of transforming a function that takes multiple arguments into a function that takes just a single argument and returns another function if any arguments are still needed.

Example: f :: a -> b -> c g is the Curried form => g :: (a , b) -> c

In Haskell all functions are considered curried meaning that all functions take in one argument.

Page 24: Learn Haskell

Curry Function

Example:Define: Add2 :: a -> a -> a Add2 (a) (b) = a + b

Add1 :: a -> aAdd1 (a) = Add2 3Use: Add1 4Display: 7

Example 2: max 4 5 when curried is ( max 4) 5

Page 25: Learn Haskell

Create Your own Data Type

Data Bool = True | False Data Shape = Circle Float Float Float | Rectangle

Float Haskell Example

surface :: Shape -> Floatsurface ( Circle _ _ r) = pi * r^2surface ( Rectangle x1) = (abs x1)

Page 26: Learn Haskell

To write an actual program

Module codeName where import otherCodeName

--define functions Example: myShapes.hs

module myShapes whereimport Shapes

f :: Shapes -> Intf (Circle w x y z ) = 1f (Rectangle x) = 2

Page 27: Learn Haskell

Glasgow Haskell Compiler (GHC)

Page 28: Learn Haskell

Glasgow Haskell Compiler (GHC)

Page 29: Learn Haskell

Why use Haskell ?

Haskell Offers you 1) Substantially increased programmer productivity.2) Shorter, clearer, and more maintainable code.3) Fewer errors, higher reliability4) A smaller “semantic gap” between the programmer and the language.

Page 30: Learn Haskell

Why use Haskell ?

Smaller programs: ex: quicksort

quicksort ::Ord a => [a] -> [a] quicksort [] = [] quicksort (p:xs) = ( quicksort lesser) +

+ [p] ++ (quicksort greater)where

lesser = filter (<p) xs greater = filter (>= p) xs

Page 31: Learn Haskell

Why use Haskell

Quick Sort in C 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;qsot( a, lo, l-1);qsort( a, l +1, hi);}}

Page 32: Learn Haskell

Haskell Applications in Industry

Many Companies have used Haskell for a range of projects:

Google: Internal IT infrastucture

Facebook: Tools

AT&T: Network Security

Page 33: Learn Haskell

References

Haskell Applications in Industry http://www.haskell.org/haskellwiki/Haskell_in_industry

Learn how to use Haskellhttp://learnyouahaskell.com/starting-out

Research paper on Haskellhttp://research.microsoft.com/en-us/um/people/simonpj/papers/history-of-haskell/history.pdf

Download Haskellhttp://www.haskell.org/haskellwiki/Haskell