• View
218

1

Embed Size (px)

• Slide 1
• Slide 2
• Slide 3
• Haskell isa functional language Imperative (C, C++) Program is a sequence of steps Subroutines are called in a specific order How to calculate Functional (Lisp, ML) Program is a single function evaluation Sub-expressions are evaluated when needed What is calculated
• Slide 4
• a pure functional language No side effects!
• Slide 5
• Types in Haskell num = 2 inc x = 1 + x add x y = x + y num :: Int inc :: Int Int add :: Int Int Int Could be Int Int Int or could be Int (Int Int) a function returning a function!
• Slide 6
• Slide 7
• Other types Bool, Char [] are lists [Bool] or [Char] (String = [Char]) (,) (,,) and so on are pairs, triples, n-tuples (Bool, Int) or (Bool, [Char], Int) [(Bool, [Char Int], [[(Char,Bool)]])]
• Slide 8
• is a functional language (2) Functions are first-order objects! Functions can be passed as arguments: sort :: [Int] [Int] sortBy :: (Int Int Bool) [Int] [Int] sortBy () sortBy (customOrdering)
• Slide 9
• is polymorphically typed sortBy :: (a a Bool) [a] [a] a can be any type: sortBy :: (Int Int Bool) [Int] [Int] sortBy :: (CharCharBool) String String sortBy :: ((Int, Int) (Int, Int) Bool) [(Int, Int)] [(Int, Int)] sortBy (
• Example: quicksort qsort :: [a] [a] qsort [] = [] qsort (x:xs) = (qsort lt) ++ [x] ++ (qsort gt) wherelt = filter (x) xs We could also write qsortBy, replacing (
• Monads provide a way to incorporate side effects eg. I/O operations putStr :: String IO () getStr :: IO String (getStr >>= \str putStr str) :: IO () (do { str getStr; putStr str; }) :: IO ()
• Slide 18
• Monads This looks like imperative code, but 1.Side-effects are precisely controlled 2.These are first-order objects! map putStr [one, two, three, ] is actually a list of I/O operations [IO ()], ([IO String], Bool IO ()) sequence [do {str getStr; putStr str; }, (putStr foo), ]
• Slide 19
• Monads and HaskellVV use a monad MeshOp: prevToOp :: vtxLabelvtxLabelMeshOp vtxLabel setNBOp :: vtxLabel [vtxLabel] MeshOp () MeshOp is just an operation: executeMeshOp :: MeshOp () mesh mesh
• Slide 20
• Example: insertVertex insertVertex :: vtxLabel vtxLabel MeshOp vtxLabel insertVertex p q = dolbl newVertexOp setNBOp lbl [p,q] replaceWithOp p q lbl replaceWithOp q p lbl return lbl
• Slide 21
• Monads and HaskellVV Some things are easy (like forall): map someOperation (listVertices mesh) map insertVertex (listNeighbours mesh) However, many vv programs require two (or more) passes through a forall statement Hmmmmaybe Haskell can help here?
• Slide 22
• Delay monads The Delay monad lets you arbitrarily delay an operation: synchronize ( do { delay a; b; }) do { b; a; } A vertex or pair can be dealt with in one go: synchronize (map doSomething (getVertices mesh))
• Slide 23
• Example: delayedInsertVertex dInsertVertex :: vtxLabel vtxLabel MeshOp vtxLabel dInsertVertex p q = dolbl newVertexOp delay (do setNBOp lbl [p,q] replaceWithOp p q lbl replaceWithOp q p lbl) return lbl
• Slide 24
• Example, continued An operation can then be someOperation = synchronize ( map handleOne (getNeighbours mesh)) where handleOne (p,q) = dolbl delayInsertVertex p q someOtherOperation p q someOtherOperation uses all of the old neighbourhoods!
• Slide 25
• Conclusion Whats good about Haskell (and HaskellVV?) Delayed operations Strict semantics (no side-effects) Easier to understand the code Operations are implementation independent Haskell code can be as fast as C / C++ Windowing support, OpenGL libraries,
• Slide 26
• Conclusion Whats bad about Haskell (and HaskellVV?) Haskell is functional and lazy Expressions are stored until evaluated (or garbage collected) If youre not careful, the heap can fill up with unevaluated expressions Current implementation of vv is slow
• Slide 27
• What have I left out? Type classes define what functions may be applied to a type for instance, (

Recommended Documents ##### Lazy evaluation illustrated - GitHub Pages One of the mental models for Haskell program main = exp aa
Documents ##### Haskell â€“ An Haskell is statically and strongly typed Uses type inference Hindley-Milner type system
Documents ##### A History of Haskell: Being Lazy With Class History of Haskell: Being Lazy With Class April 16, 2007
Documents Documents ##### Haskell - cs. Haskell Another cool, typed, func1onal programming language â€¢ Like OCaml in that: â€“
Documents     