- By
**aira** - Follow User

- 106 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about 'Learn Haskell' - aira

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

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.

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.

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.

Haskell Compilers

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

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)

Main Features

- Lazy Evaluation
- Pattern Matching
- List Comprehension
- Type classes
- Type polymorphism

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:[]

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

Pattern Matching

- In pattern matching, we attempt to match values against patterns and, if so desired, bind variables to successful matches.
- Example: fibonoccifib :: 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

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 ([]) = [];

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]

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]

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)

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

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.

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]applyFuncfs as = [f a | f <- fs, a<- as]
- X `elem` List = elemX List
- F _ [] : The “_” means the function takes in anything else that the pattern doesn’t match.

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

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)

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 + yUse: (\x y -> x + y) 3 5Result Displayed = 8
- -- This is a lambda function to add one to a list\(as) -> [a + 1| a <- as]

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

Case

- Syntax :case Expression of pattern -> result pattern -> result pattern -> result
- Example:Head’ :: [a] -> a Head’ xs = casexsof [] -> error “No head of list” (x:_) -> x

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.

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

Create Your own Data Type

- DataBool = True | False
- Data Shape = Circle Float Float Float | Rectangle Float
- Haskell Example surface :: Shape -> Floatsurface ( Circle _ _ r) = pi * r^2surface ( Rectangle x1) = (abs x1)

To write an actual program

- ModulecodeNamewhereimportotherCodeName--define functions
- Example: myShapes.hsmodulemyShapeswhereimport Shapesf :: Shapes -> Intf (Circle w x y z ) = 1f (Rectangle x) = 2

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.

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

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);}}

Haskell Applications in Industry

- Many Companies have used Haskell for a range of projects:Google: Internal IT infrastuctureFacebook: ToolsAT&T: Network Security

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

Download Presentation

Connecting to Server..