- By
**badu** - Follow User

- 242 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' Haskell' - badu

**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

Outline

- History
- Language Features
- Haskell Traits
- Implementations/Compilers
- Haskell Basics
- Examples
- Conclusion

- Haskell was named after Haskell Curry, an American mathematician
- In 1987, at a conference on Functional Programming Languages and Computer Architecture, a group of individuals agreed that there was a need to combine existing functional languages into a common one (Haskell)
- Haskell is a modern, purely-functional programming language
- It provides features including polymorphic typing, lazy evaluation, and higher-order functions

3

- In 1988, the first physical meeting was held at Yale where the establishment of the language’s goals were built:1. It should be suitable for teaching, research, and applications, including building large systems.2. It should be completely described via the publication of a formal syntax and semantics.
- 3. It should be freely available. Anyone should be permitted to implement the language and distribute it to whomever they please.
- 4. It should be usable as a basis for further language research.
- 5. It should be based on ideas that enjoy a wide consensus.
- 6. It should reduce unnecessary diversity in functional programming languages.

4

- 1990 – Haskell 1.0 was released
- Late 1997 – Haskell 98 a stable version, which was a collaboration of all the former versions including a standard library for teaching was released (Presently the most current version)

5

Pattern Matching – Searching for the elements of a given pattern. If there is a match, then the function moves forward.

Example:let f 0 = 1… Pattern declaration>f 0… When f is 0 as the argument the pattern matches and the function returns 1

> f 5… Function fails

6

- Currying - process of transforming a function that takes multiple arguments into a function that takes just a single argument.Example:
- add_1:: (Int,Int) → Int
- add_2::Int → (Int → Int)These two functions produce the same result, but add_1 takes its two arguments at the same time, whereas add_2 takes them one at a time (Curried Function)

7

- List Comprehension – used to derive a new list from an existing list.
- Example 1:Input:[odd x|x <- [1..9]] Output:[True,False,True,False,True,False,True,False,True]
- Example 2:
- Input: [x*2 | x <- [1..10], x*2 >= 12]
- Output: [12,14,16,18,20]

8

- Guards - Boolean expression that must evaluate to true if the program execution is to continueExample:f x …input to f will be pattern-matched against x | x > 0 = 1 …Guard 1 | otherwise = 0…Guard 2 Guards are evaluated in the order they appear. If no guards match, an error will be produced at runtime, so it\'s always a good idea to leave an \'otherwise\' guard in there to handle the "But this can\'t happen!" case.Equivalent to:

9

- Single Assignment - Does not allow the reassignment of variables within a scope Example:
- (let n = 10 in (print n >> (let n = 15 in print n)))A new nested scope was created where the n in the inner scope is a different variable that is shadowing the n in the outer scope

10

Haskell’s Traits

- Haskell uses lazy evaluation.
- Haskell is a purely functional programming language.

Haskell is Lazy…

- Haskell uses lazy evaluation by default.
- No expression is evaluated until its value is truly needed.
- Ex:
- The value of x is not evaluated , since the result of the function does not depend on it.
- This can be overridden by the seq function in Haskell.
- Ex:
- The seq function forces the value of argument x to be evaluated.

- >let testFunction x = “x is Not Evaluated”
- >testFunction (2+2)
- >“x is Not Evaluated”

- >seqtestFunction (2+2)
- >4

Haskell is Lazy…(Cont.)

- Shared expressions are evaluated only once:
- If the expression is evaluated, the result is shared with all expression callers.
- Ex:
- The value of square will only be evaluated once, and will be shared with both Calls.
- Calculations are made over graphs instead of trees:

- >let square = 3*3
- > square * square
- >81

Tree:

Graph:

(*)

(*)

(3*3)

(3*3)

(3*3)

Haskell is Functional

- Haskell is a purely functional programming language.
- Variables or states are not manipulated.
- Focus entirely on constants and functions.
- We are able to maintain states by using recursion:
- Ex:

- >let fact n = if n == 0 then 1 else n * fact (n-1)

Implementations/Compilers

- GHC:
- Originally developed at the University of Glasgow in 1989.
- Glasgow Haskell Compiler
- GHC has extensive optimization capabilities.
- Has support for concurrency and parallelism.
- GHC is itself written in Haskell.
- Hugs:
- Haskell User’s Gofer System
- Written in C, which makes it very portable, being able to run on almost any machine.,
- Complies quickly, but its run-time performance is no match for GHC.

Implementations/Compilers

- Helium
- Developed at Utrecht University (Netherlands)
- Focuses on making learning Haskell easier
- Uses very clear and concise error messages.
- However, in order to make debugging easier, it has actually disabled many features of the language.

Haskell Basics: Instructions

- Interpreter can be used for simple calculations:
- Can also be used for Boolean tests:

- ghci> 2 + 15
- 17
- ghci> 49 * 100
- 4900
- ghci> 5 / 2
- 2.5

- ghci> True && False
- False
- ghci> True || False
- True
- ghci> 5==5
- True

Haskell Basics: Types & Operators

- Basic Haskell Types:
- Int
- Integer
- Float
- Double
- Char
- Boolean
- Haskell uses type inference, which means you do not have to specify a constant’s/function’s type, unless you want to.
- Haskell operators are very similar to other languages (+,-,*,/,**,==,||,&&,etc…)

18

Haskell Basics: Lists

- Data structure which stores items of the same type.
- Haskell treats list of characters as string:
- We can concatenate lists using ‘++’
- We can also define list ranges using ‘..’

- ghci> let l = [1,2,3,4]
- ghci>l
- [1,2,3,4]

- ghci> let l = [‘h’,’e’,’l’,’l’,’o’]
- ghci>l
- hello

- ghci> let l = [‘h’,’e’,’l’,’l’,’o’]
- ghci>l ++ [‘ ‘, ‘w’, ‘o’, ‘r’, ‘l’, ‘d’]
- hello world

- ghci> [1..20]
- [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
- ghci> [a..z]
- abcdefghijklmnopqrstuvwxyz

Haskell Basics: List Operators

- head: takes a list and returns its first element.
- tail: takes a list and returns its tail. In other words, it chops off a list\'s head.
- last: takes a list and returns its last element.
- init: takes a list and returns everything except its last element.
- length: returns the list’s length.
- filter: filters a list based on certain criteria:

- ghci> filter (> 2) [1 .. 5]
- [3,4,5]

Haskell Basics: Functions

- We can define functions as follows:
- Ex:
- We can also define lambda expressions:
- To relate to what we’ve learned in class, this is equivalent to:

- let function_name parameters = expression

- let s x = x+1

- ghci> let f n = (\x -> x+1) n
- ghci>f 20
- 21

- (λx.x + 1)20

Conclusion

- Even though imperative programming seems to be the dominant form of programming today, functional programming offers some advantages, such as more predictable results, and a more mathematical approach to solving problems.
- Haskell is currently considered by many as THE choice for a purely functional programming language.
- We advise all CS students to spend at least one afternoon learning about Haskell. Even though you will probably not use it in your career, it will still give you an alternate view of programming and problem solving.
- We hope you enjoyed this introduction.

Download Presentation

Connecting to Server..