Download Example - bYTEBoss

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Lambda calculus definition wikipedia , lookup

Lambda calculus wikipedia , lookup

Lambda lifting wikipedia , lookup

Closure (computer programming) wikipedia , lookup

Currying wikipedia , lookup

Combinatory logic wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Standard ML wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
Haskell
By:
Renan Pereira
Jim Hiatt
Alexis Rodriguez
Outline







History
Language Features
Haskell Traits
Implementations/Compilers
Haskell Basics
Examples
Conclusion
2
History of Haskell




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
History of Haskell

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
History of Haskell


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
Language Features
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
Language Features

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
Language Features

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
Language Features

Guards - Boolean expression that must evaluate to true if
the program execution is to continue
Example:
f x … input to f will be pattern-matched
| x > 0 = 1 …Guard 1
| otherwise = 0…Guard 2
against x
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
Language Features

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.
11
Haskell is Lazy…


Haskell uses lazy evaluation by default.
No expression is evaluated until its value is truly needed.

Ex:
>let testFunction x = “x is Not Evaluated”
>testFunction (2+2)
>“x is Not Evaluated”


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:
>seq testFunction (2+2)
>4

The seq function forces the value of argument x to be evaluated.
12
Haskell is Lazy…(Cont.)

Shared expressions are evaluated only once:


If the expression is evaluated, the result is shared with all expression callers.
Ex:
>let square = 3*3
> square * square
>81


The value of square will only be evaluated once, and will be shared with both
Calls.
Calculations are made over graphs instead of trees:
Tree:
Graph:
(*)
(*)
(3*3)
(3*3)
(3*3)
13
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)
14
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.
15
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.
16
Haskell Basics: Instructions

Interpreter can be used for simple calculations:
ghci> 2 + 15
17
ghci> 49 * 100
4900
ghci> 5 / 2
2.5

Can also be used for Boolean tests:
ghci> True && False
False
ghci> True || False
True
ghci> 5==5
True
17
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.
ghci> let l = [1,2,3,4]
ghci>l
[1,2,3,4]

Haskell treats list of characters as string:
ghci> let l = [‘h’,’e’,’l’,’l’,’o’]
ghci>l
hello

We can concatenate lists using ‘++’
ghci> let l = [‘h’,’e’,’l’,’l’,’o’]
ghci>l ++ [‘ ‘, ‘w’, ‘o’, ‘r’, ‘l’, ‘d’]
hello world

We can also define list ranges using ‘..’
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
19
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]
20
Haskell Basics: Functions

We can define functions as follows:
let function_name parameters = expression

Ex:
let s x = x+1

We can also define lambda expressions:
ghci> let f n = (\x -> x+1) n
ghci>f 20
21

To relate to what we’ve learned in class, this is equivalent to:
(λx.x + 1)20
21
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.
22
Haskell
By:
Renan Pereira
Jim Hiatt
Alexis Rodriguez