Hi Haskell

Download Hi Haskell

Post on 06-May-2015

2.573 views

Category:

Education

13 download

Embed Size (px)

DESCRIPTION

Haskell HaskellLambdaCurry Algebraic Data TypeType Class PurityLazy Evaluation Haskell

TRANSCRIPT

<ul><li>1.Hi Haskell</li></ul> <p>2. </p> <ul><li> A language that doesnt effect how you think about programming is not worth learning </li></ul> <p>Alan Perlis 3. </p> <ul><li> Haskell </li></ul> <ul><li><ul><li>Lambda Curry </li></ul></li></ul> <ul><li><ul><li>Algebraic Data Type Type Class </li></ul></li></ul> <ul><li><ul><li>Purity, Lazy Evaluation </li></ul></li></ul> <ul><li> (optional) </li></ul> <ul><li>Haskell </li></ul> <p>4. (Programming Paradigm) </p> <ul><li> -- </li></ul> <p>5. </p> <ul><li>Imperative </li></ul> <ul><li>Declarative </li></ul> <ul><li>Object-Oriented </li></ul> <ul><li>Aspect-Oriented </li></ul> <ul><li>Event-Driven </li></ul> <ul><li>Functional </li></ul> <ul><li>Generic </li></ul> <ul><li>Logic </li></ul> <ul><li>Concurrent </li></ul> <p>6. 7. Functional Programming </p> <ul><li> FP (evaluation) (mutable) FP (application) </li></ul> <p>8. </p> <ul><li> ,Haskell </li></ul> <ul><li>pure ) </li></ul> <ul><li> FP </li></ul> <p>9. Lambda </p> <ul><li>(x y-&gt;x*10+y) 5 9 </li></ul> <ul><li>--59 </li></ul> <ul><li>//JavaScript </li></ul> <ul><li>(function (x,y){ </li></ul> <ul><li>return x*10 + y </li></ul> <ul><li>})(5,9) </li></ul> <p>10. First-Class Function &amp; High Order Function </p> <ul><li> Int Float </li></ul> <p>11. Currying(Partial Application)&amp;High Order</p> <ul><li>-- f::Int-&gt;Int-&gt;Int </li></ul> <ul><li>-- f::Int-&gt;(Int-&gt;Int) </li></ul> <ul><li>f x y=x*10 + y </li></ul> <ul><li>f5 = f 5 </li></ul> <ul><li>f7 = f 7 </li></ul> <ul><li>f5 9 -- 59 </li></ul> <ul><li>f5 1 -- 51 </li></ul> <ul><li>f7 9 -- 79 </li></ul> <ul><li>f7 1 -- 71 </li></ul> <ul><li>//JavaScript </li></ul> <ul><li>function f(x) { </li></ul> <ul><li>return function(y) { </li></ul> <ul><li>return x * 10 + y </li></ul> <ul><li>} </li></ul> <ul><li>} </li></ul> <ul><li>f5 = f(5) </li></ul> <ul><li>f7 = f(7) </li></ul> <ul><li>f5(9) // 59 </li></ul> <ul><li>f7(1) // 71 </li></ul> <p>12. - 3D </p> <ul><li>(R,R) -&gt; (R,R,R) </li></ul> <ul><li> (R,R,R) -&gt; R, </li></ul> <ul><li> (R,R) -&gt; R </li></ul> <ul><li>2D : R -&gt; (R,R) </li></ul> <ul><li> : (R,R,R) -&gt; (R,R,R) </li></ul> <ul><li> : (R,R) -&gt; Color </li></ul> <p>13. Example -FieldTrip http://www.haskell.org/haskellwiki/FieldTrip </p> <ul><li>talk is cheap </li></ul> <ul><li>show me the running code </li></ul> <p>14. Example -FieldTrip </p> <ul><li>circle :: Curve2 </li></ul> <ul><li>semiCircle :: Curve2 </li></ul> <ul><li>-- Z </li></ul> <ul><li>revolve :: Curve2 -&gt; ParamSurf </li></ul> <ul><li>torus :: R -&gt; R -&gt; ParamSurf </li></ul> <ul><li>-- sr cr: </li></ul> <ul><li>torus sr cr = revolve (const (sr,0) ^+^ const cr *^ circle) </li></ul> <ul><li>sphere = revolve semiCircle </li></ul> <p>15. Example - 3D </p> <ul><li> 3D </li></ul> <p>16. Algebraic Data Type </p> <ul><li>data </li></ul> <ul><li> constructor </li></ul> <ul><li> constructor </li></ul> <ul><li>data Maybe a = </li></ul> <ul><li>Just a </li></ul> <ul><li>| Nothing </li></ul> <ul><li>data Tree a = </li></ul> <ul><li>Empty </li></ul> <ul><li>| Leaf a </li></ul> <ul><li>| Node(Tree a)(Tree a) </li></ul> <p> Constructor Constructor 17. Algebraic Data Type </p> <ul><li> Maybe Maybe Int </li></ul> <ul><li><ul><li>Just 20 </li></ul></li></ul> <ul><li><ul><li>Nothing </li></ul></li></ul> <ul><li> Maybe Int </li></ul> <p>18. Pattern Matching </p> <ul><li>constructor </li></ul> <ul><li> pattern </li></ul> <ul><li>tree =</li></ul> <ul><li>Node (Leaf 4) (Node Empty (Leaf 5)) </li></ul> <ul><li>accum Empty = 0 </li></ul> <ul><li>accum (Leaf a) = a </li></ul> <ul><li>accum (Node left right) = (accum left) + (accum right) </li></ul> <ul><li>accum tree -- 9 </li></ul> <p>19. </p> <ul><li> list </li></ul> <p>20. Type Classes </p> <ul><li>classEq a where </li></ul> <ul><li>(==) :: a -&gt; a -&gt; Bool </li></ul> <ul><li>elem :: Eq a =&gt; a-&gt;[a]-&gt;Bool </li></ul> <ul><li>elem k (x:xs) </li></ul> <ul><li>| k == x = True </li></ul> <ul><li>| otherwise = elem k xs </li></ul> <ul><li>elem k [] = False </li></ul> <p>OO class 21. Instance of Type Classes </p> <ul><li>instanceEq (Maybe a) where </li></ul> <ul><li>Just v1 == Just v2 = v1 == v2 </li></ul> <ul><li>Nothing == Nothing = True </li></ul> <ul><li>_ == _= False </li></ul> <p>22. Lazy Evaluation </p> <ul><li>a = [1..] -- infinite list </li></ul> <ul><li>take 3 a -- [1,2,3] </li></ul> <p>23. Purity </p> <ul><li> Functional </li></ul> <p>24. Purity </p> <ul><li>variable </li></ul> <ul><li>a = 3 </li></ul> <ul><li>a = 19 </li></ul> <p>25. Purity </p> <ul><li> side effect </li></ul> <p>26. Referential Transparent </p> <ul><li> 2 * f(x) f(x) + f(x) </li></ul> <ul><li>int y = 10; </li></ul> <ul><li>int f(int i) </li></ul> <ul><li>{ </li></ul> <ul><li>return i + y++; </li></ul> <ul><li>} </li></ul> <ul><li>f(5) + f(5); // 15 + 16 = 31 </li></ul> <ul><li>2 * f(5);// 2 * 15 = 30</li></ul> <p>27. </p> <p>28. IO Action </p> <ul><li>IO () IO first-class IO IO main IO </li></ul> <ul><li> IO </li></ul> <ul><li> IO main </li></ul> <p>--putStrLn :: String -&gt; IO () --main :: IO () main = putStrLn "Hi Haskell!" 29. Monad </p> <ul><li> IO Action Monad </li></ul> <ul><li> Monster </li></ul> <ul><li>Haskell Monad Monad "warm fuzzy thing" ( )!</li></ul> <ul><li><ul><li>--Simon Peyton Jones Haskell </li></ul></li></ul> <p>30. Monad </p> <ul><li>class Monad m where</li></ul> <ul><li>bind:: m a -&gt; (a -&gt; m b) -&gt; m b </li></ul> <ul><li>inject:: a -&gt;m a </li></ul> <p>Monad m a bind inject a (a -&gt; m b) 31. Example - </p> <ul><li> DSL </li></ul> <ul><li> Haskell EDSL </li></ul> <ul><li><ul><li>Algebraic Data Type </li></ul></li></ul> <p>32. Example - , Hommage Haskell Offline Music Manipulation And Generation EDSL http://substitut-fuer-feinmotorik.net/projects/haskellommage 33. </p> <p>34. </p> <ul><li>forkIO :: IO () -&gt; IO ThreadId </li></ul> <ul><li>forkIO (writeFile "temp.txt" "haskell thread") </li></ul> <p>35. Shared Mutable State Concurrency </p> <ul><li>Race conditions due to forgotten locks </li></ul> <ul><li>Deadlocks resulting from inconsistent lock ordering </li></ul> <ul><li>Corruption caused by uncaught exceptions </li></ul> <ul><li>Lost wakeups induced by omitted notifications </li></ul> <p>36. Software Transactional Memory(STM) </p> <ul><li> transaction </li></ul> <ul><li> a b </li></ul> <ul><li><ul><li>transaction </li></ul></li></ul> <ul><li><ul><li>transaction </li></ul></li></ul> <p>37. STM Sample </p> <ul><li>import Control.Concurrent.STM </li></ul> <ul><li>type Bal =TVarFloat </li></ul> <ul><li>transfer::Float-&gt;Bal-&gt;Bal-&gt;STM () </li></ul> <ul><li>transfer qty fromBal toBal = do</li></ul> <ul><li>fromQty STM a </li></ul> <ul><li>catchSTM :: STM a -&gt; (exception-&gt;STM a) -&gt; STM a </li></ul> <ul><li>instance Monad STM </li></ul> <ul><li>complexAction = action1 `orElse` action2 </li></ul> <ul><li>main = atomically complexAction </li></ul> <p>40. </p> <ul><li>-- </li></ul> <ul><li>sort :: (Ord a) =&gt; [a] -&gt; [a] </li></ul> <ul><li>sort (x:xs) = lesser ++ x:greater</li></ul> <ul><li>where lesser = sort [y | y [a] -&gt; [a] </li></ul> <ul><li>parSort (x:xs) =forcegreater`par`</li></ul> <ul><li>( forcelesser`pseq`(lesser ++ x:greater)) </li></ul> <ul><li>where lesser = parSort [y | y (a -&gt; b) -- map function </li></ul> <ul><li>-&gt; Strategy c -- evaluation strategy for reduction</li></ul> <ul><li>-&gt; ([b] -&gt; c) -- reduce function </li></ul> <ul><li>-&gt; [a] -- list to map over</li></ul> <ul><li>-&gt; c </li></ul> <ul><li>mapReduce mapStrat mapFunc reduceStrat reduceFunc input</li></ul> <ul><li>= mapResult `pseq` reduceResult </li></ul> <ul><li>where mapResult = parMap mapStrat mapFunc input</li></ul> <ul><li>reduceResult = reduceFunc mapResult `using` reduceStrat </li></ul> <p>45. </p> <ul><li> GPU </li></ul> <ul><li> CPU </li></ul> <ul><li> GPU Shader CUDA OpenCL </li></ul> <ul><li>GPU FP </li></ul> <ul><li>GPipe EDSL GPU , Haskell GPU Haskell OpenGL GLSL </li></ul> <p>46. Example - GPU </p> <ul><li>GPipe </li></ul> <ul><li>http://www.haskell.org/haskellwiki/GPipe </li></ul> <p>47. Parser Combinator </p> <ul><li> parse </li></ul> <ul><li> parse </li></ul> <ul><li> ( Lex Yacc Flex Bison Antlr) parse </li></ul> <ul><li>Haskell parser Parser Combinator </li></ul> <p>48. Example - Parser Combinator for JavaScript </p> <ul><li>parser :String -&gt; Result </li></ul> <ul><li>Result 3 </li></ul> <ul><li><ul><li>remaining: parse </li></ul></li></ul> <ul><li><ul><li>matched: parser parse </li></ul></li></ul> <ul><li><ul><li>ast: parse </li></ul></li></ul> <ul><li> parser </li></ul> <ul><li><ul><li>range("0", "9") </li></ul></li></ul> <ul><li><ul><li>token("begin") </li></ul></li></ul> <ul><li>parser combinator ( )parser </li></ul> <ul><li><ul><li>repeat1(alternate(sequence(parser1, parser2), parser3)) </li></ul></li></ul> <p>49. EDSL </p> <ul><li> first-class </li></ul> <ul><li> combinator </li></ul> <p>50. </p> <ul><li>-- </li></ul> <ul><li>TheComputerLanguageBenchmarksGame </li></ul> <ul><li><ul><li>http://shootout.alioth.debian.org/ </li></ul></li></ul> <ul><li><ul><li> Haskell C 1/5 </li></ul></li></ul> <ul><li> 2D </li></ul> <ul><li><ul><li>http://blog.csdn.net/soloist/archive/2009/04/09/4060081.aspx </li></ul></li></ul> <p>51. 52. 53. </p> <ul><li> Haskell </li></ul> <ul><li>FP Scala Haskell Java JVM </li></ul> <ul><li> FP Haskell FP programmer </li></ul> <p>54. </p> <ul><li>[email_address] </li></ul> <ul><li>http://blog.csdn.net/soloist </li></ul> <ul><li> FP </li></ul> <p>55. </p>