2010 03 21 Dan - Vasicek.functional Programming Using Haskell
2010 03 21 Dan - Vasicek.functional Programming Using Haskell
Haskell
Dan Vasicek
2010 – 03 – 21
Functional Programming in Haskell
• Haskell Information • Fundamental Data types
Sources –Boolian
• Fundamental concepts –Numbers
– Functional –Characters
Programming • Compound data types
– Sessions, modules, •Tuples
& scripts •Lists
•User Defined Types
– Polymorphic types oEnumerations
– Order of evaluation • Efficiency
– Patterns –Evaluation order
– Lazy evaluation –Lazy Evaluation
– Side Effects –Space
•Monads
•Examples
Sources of Information
• Book - “Introduction to Functional Programming
Using Haskell”, Richard Bird, Pearson Education
Limited, England, Prentice Hall Europe 1998
• Tutorial -
http://www.cs.utah.edu/~hal/docs/daume02yaht
.pdf
• tail :: [a] -> [a]
tail (x:xs) = xs
• Both fail if presented with an empty list
• Both work for lists of anything, even lists of
empty lists and are Polymorphic
• Examples of the Hindley-Milner type system
Order of Evaluation
• Order of evaluation (simplification, or reduction)
is not specified in a functional program
• Define: sq x = x*x
• sq(3+4) could be simplified as
– sq(7) 7*7 49
– (3+4)*(3+4) 7*(3+4) 7*749
• Both orders produce the same result
• The independence of the result from the order is
a characteristic feature functional programs
• The OS is free to choose the “best” order
Lazy Evaluation
• let three x = 3
• let infinity = infinity +1
• Now simplify the expression
– three infinity
– Simplification of infinity first gives
• Three(infinity +1 +1 +1 and so on)
• which does not terminate
– Simplification of three first,
• three infinity = 3
• the expression terminates in one step
• Some simplification orders may terminate while others do not
• In GHCi three infinity =3
• In general, some simplification orders will be more efficient than
others
Lazy Evaluation
• Guarantees termination whenever
termination is possible
• Allows the OS to choose an “efficient”
evaluation order
Side Effects
• A side effect is essentially something that happens in the course of
executing a function that is not related to the output produced by
that function.
• A pure function simply returns a value
• A pure function has no internal state
• A pure function cannot modify the input data
• Given the same arguments a pure function will always produce the
same result
• In GHCi values may be displayed by the interactive environment
• Monadic programming allows functional programs to mimic
imperative programs
• Monads provide a way to execute “Commands” and display values
Monads
• Haskell uses monads to isolate all impure (not
functional) computations from the rest of the
program and perform them in the “safe” way
• The execution order of a functional program is
entirely determined by the operating system.
And this applies to the order of execution of I/O
as well
• Thus, the order of I/O can not be preserved by a
functional program
Example of Scrambled I/O Order
• “Thus, the order of I/O can not be preserved
by a functional program”
• Suppose that your functional program wrote
the words in the following order:
• “be preserved a functional program the order
of I/O can not by Thus,”
Imperative Constructs are NOT
Functional
• x=x+1 – is not allowed!
• All ghci commands are imperative.
• The interactive environment is imperative
• http://www.haskell.org/haskellwiki/Functiona
l_programming
• http://www.haskell.org/all_about_monads/ht
ml/class.html
Haskell Fundamental Data Types
• Bool: True or False
• Char: ‘a’ , '\n', '\x05e0‘, ‘\122’
a newline α z
• Number:
–1
– 2.718
Compound Data types
Tuples
• (‘a’, “Daniel”, 3.14159) is valid
• (1, map) is a valid tuple. But you will have to define
an I/O Monad to “Show” it.
• The functions for extracting the first and second
element of a pair are defined in the standard Haskell
environment
– fst(x,y) = x
– snd(x,y) = y
• fst(1,2,3) is not defined in the standard environment
Lists
• Lists – a list is enclosed in square brackets
• The empty list is []
• The cons operator is “:”
• 1:2:3:[] is [1,2,3]
• “Daniel” is ‘D’:’a’:’n’:’i’:’e’:’l’:[] =[‘D’,’a’,’n’,’i’,’e’,’l’]
• ‘D’:”an” = “Dan”
• All elements of a list must be of the same type
• [[1,2],[1]] is a valid list
Comments in Haskell Code
• Single line comments are preceded by ``--''
and continue to the end of the line. For
example:
suc n = n + 1 -- this is a successor function
• Multiline and nested comments begin with {-
and end with -}. Thus
{- can be used to inactivate a block of code -}
Literate Programming
• A “literate code file” is a file with suffix .lhs
instead of .hs (Literate Haskell)
• Two styles for literate code:
– LaTeX Style : \begin{code} … \end{code}
– “Bird” Style: prefix code lines with the “>”
character
• Compiler flags allow for reconfiguration of the
literate style
Example: LaTeX Literate Style
Here is a simple example of a literate script
for defining the quicksort function:
\begin{code}
tsort [] = []
tsort (x:xs) = tsort [y | y<-xs, y<x] ++ [x] ++ tsort [y | y<-
xs, y>=x]
\end{code}
Notice that this definition is very inefficient for a
sorted list.
Example: Richard Bird Literate
Style
In Bird-style a blank line is required before the code
• http://code.haskell.org/utf8-string/
Unicode table
Unicode Experiment
• Create a list of byte codes for some Hebrew
characters:
• hebrew = ['\n', '\x05d0', '\x05d1', '\x05d2', '\x05d3',
'\x05d4', '\x05d5', '\x05d6', '\x05d7', '\x05d8',
'\x05d9','\x5da','\x5db','\x5dc','\x5de','\x5df', '\x05e0',
'\x0e1', '\x05e2', '\x05e3', '\x05e4', '\x05e5', '\x05e6',
'\x05e7', '\x05e8', '\x05e9' , '\x05ea', '\x05eb', '\x05ec',
'\x05ed', '\x05ee', '\x05ef' , '\n','\n‘]
• putStr hebrew
• Result on next slide
Result of “putStr hebrew”
Unicode Greek
αβ Γ Π Σ σμτΦΘΩδ
• (v) Disjunction
• (v) :: Bool Bool Bool
• True v x = True
• False v x = x
More Functions
• (!!) List indexing
• (!!) :: [a] Int a
• []!!n = error “(!!): Index too large”
• (x:xs)!!0 = x
• (x:xs)!!(n+1) = xs!!n