Haskell
Haskell
Haskell
Eric Alzheimer
What is Haskell?
Pure Functional Language
Powerful, polymorphic, strong and
strict type system
... with type inference!
Lazily evaluated
mathy syntax with significant
whitespace (like Python)
Emphasizes minimizing side-effects
History
1970s: ML by Robert Milner; Used in
academia
1980s: ML had many (~12) pure,
lazily evaluated descendents
1990: Committee formed to unify
these languages into one creates
Haskell.
Named after logician Haskell Curry
Software in Haskell
GHC -> Production quality Haskell
optimizing compiler and interpreter
Pugs -> Perl 6 interpreter (first
one, actually)
Darcs -> Distributed version control
system
Bluespec SystemVerilog ->
Synthesizable hardware description
language.
Language Basics
Functions
Everything is a function
thisIsTwo = 2
sum x y = x + y
foo x y z = z sum x y
>>> foo 1 2 3
--> 0
Data Structures
Collections of data
Can have multiple internal
structures
Can be recursive
Data Structures
data Point = Point Int Int
data Shape = Rect Point Point
| Circle Point Int
data Colors = Red|Green|Blue
type Points = [Point]
myCircle = Circle (Point 2 4) 9
Types?????
All of the functions a few slides
ago are strictly typed
... But we had no type declarations.
This is because Haskell has type
inference.
The compiler/interpreter analyzes
your code and automatically assigns
the most general possible type.
However, it is common to specify the
type of top level declarations, to
ensure correctness and document
functionality
Types!!!!!
myNum :: Int
myNum = 2
myOtherNum :: Float
myOtherNum = 2.0
Lists
myList = [1, 2, 3]
longer = 0 : myList
(cons 0 my-list)
head my-list
(car my-list)
tail my-list
(cdr my-list)
[1, 2] ++ [3, 4]
(append (1 2) (3 4))
QWER : [2, 3, 4]
[1, 2] :: [Int]
ERROR
Branching
1. Pattern Matching
2. Guards
Pattern Matching
Multiple function definitions
Chosen based on value and structure
of arguments
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
isTwo 2 = True
isTwo _ = False
(Rect w h)
= w * h
area
(Circle r)
= pi * r * r
width (Rect w _)
= w
width (Circle r)
= 2 * r
Guards
Like cond in Scheme/Lisp
Series of conditional expressions
randomFunction x y
| x <= y
= (x + y) / 2
| (y 2) > 7
= y x
| odd (x + y)
= x + y
| otherwise
= 42
Looping
Recursion
As a functional language, recursion
is the most basic looping construct.
factorial 0 = 1
factorial n = n * factorial (n 1)
Local Variables
myFunction a b = x y
where x = a + b
y = a * b
myOtherFunction thing = let a = getFoo thing
b = getBar thing
in a + b
Unusual Features
1. Lazy Evaluation
2. Monadic IO
3. Partial Evaluation
Lazy Evaluation
Values arent evaluated until they
are forced to be evaluated.
In C/Java/Scheme, arguments are
evaluated before call. In Haskell,
they are only evaluated if used.
myIf True x y = x
myIf False x y = y
>>> myIf (2 == 3) [1..] [1, 2]
--> [1, 2]
Monadic IO
Monad --> Abstract type used to
model sequence, state, and side
effects.
Do notation
getName :: IO String
getName = do putStrLn Enter your name:
name <- getLine
if name == then do
putStrLn No name entered...
getName - Try again
else do
return name
Partial Functions
sum :: Int -> Int -> Int
sum x y = x + y
-- sum :: Int -> (Int -> Int)
addFive = sum 5
-- What type?
addFive :: Int -> Int
>>> addFive 3
--> 8
Sample Program
Really simple polynomial
derivative calculator.
*SimpleDeriv> main
Enter a polynomial like "2x^4 + 3x + 7" to get a derivative
Enter "q" to exit
>>> 2x^9 + 3x + 4x^2 + 6
==> 18x^8 + 3 + 8x
>>> 2
==>
>>> q
*SimpleDeriv>
-- Convert a
termToString
termToString
termToString
termToString
termToString
termToString
term to a string
:: TermInfo -> String
(Constant value) = show value
(Term 1 1)
= "x"
(Term 1 expo)
= "x^" ++ (show expo)
(Term coef 1)
= (show coef) ++ "x"
(Term coef expo) =
(show coef) ++ "x^" ++ (show expo)
main :: IO ()
main = do
putStrLn "Enter a polynomial like \"2x^4 + 3x + 7\" to get a derivative"
putStrLn "Enter \"q\" to exit"
mainloop
mainloop :: IO ()
mainloop = do
putStr ">>> "
line <- getLine
if line == "q"
then return ()
else do
putStr "==> "
putStrLn (polyToString (derivative (polyFromString line)))
mainloop
Resources
www.haskell.org
Real World
Haskell
End.
quicksort []
= []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
where lesser = filter (< p) xs
greater = filter (>= p) xs