Post on 06-May-2015

2.574 views

Category:

## Education

Embed Size (px)

DESCRIPTION

Haskell语言的概览性介绍，内容有： 若干语言特性（是什么让Haskell如此独特？）：Lambda，Curry， Algebraic Data Type，Type Class， Purity，Lazy Evaluation； 对并行与并发的支持； 若干有启发性的例子以及性能问题； Haskell在工业界的应用；

TRANSCRIPT

2.

• A language that doesnt effect how you think about programming is not worth learning

Alan Perlis 3.

• Lambda Curry
• Algebraic Data Type Type Class
• Purity, Lazy Evaluation
• (optional)

• --

5.

• Imperative
• Declarative
• Object-Oriented
• Aspect-Oriented
• Event-Driven
• Functional
• Generic
• Logic
• Concurrent

6. 7. Functional Programming

• FP (evaluation) (mutable) FP (application)

8.

• pure )
• FP

9. Lambda

• (x y->x*10+y) 5 9
• --59
• //JavaScript
• (function (x,y){
• return x*10 + y
• })(5,9)

10. First-Class Function & High Order Function

• Int Float

11. Currying(Partial Application)&High Order

• -- f::Int->Int->Int
• -- f::Int->(Int->Int)
• f x y=x*10 + y
• f5 = f 5
• f7 = f 7
• f5 9 -- 59
• f5 1 -- 51
• f7 9 -- 79
• f7 1 -- 71
• //JavaScript
• function f(x) {
• return function(y) {
• return x * 10 + y
• }
• }
• f5 = f(5)
• f7 = f(7)
• f5(9) // 59
• f7(1) // 71

12. - 3D

• (R,R) -> (R,R,R)
• (R,R,R) -> R,
• (R,R) -> R
• 2D : R -> (R,R)
• : (R,R,R) -> (R,R,R)
• : (R,R) -> Color

• talk is cheap
• show me the running code

14. Example -FieldTrip

• circle :: Curve2
• semiCircle :: Curve2
• -- Z
• revolve :: Curve2 -> ParamSurf
• torus :: R -> R -> ParamSurf
• -- sr cr:
• torus sr cr = revolve (const (sr,0) ^+^ const cr *^ circle)
• sphere = revolve semiCircle

15. Example - 3D

• 3D

16. Algebraic Data Type

• data
• constructor
• constructor
• data Maybe a =
• Just a
• | Nothing
• data Tree a =
• Empty
• | Leaf a
• | Node(Tree a)(Tree a)

Constructor Constructor 17. Algebraic Data Type

• Maybe Maybe Int
• Just 20
• Nothing
• Maybe Int

18. Pattern Matching

• constructor
• pattern
• tree =
• Node (Leaf 4) (Node Empty (Leaf 5))
• accum Empty = 0
• accum (Leaf a) = a
• accum (Node left right) = (accum left) + (accum right)
• accum tree -- 9

19.

• list

20. Type Classes

• classEq a where
• (==) :: a -> a -> Bool
• elem :: Eq a => a->[a]->Bool
• elem k (x:xs)
• | k == x = True
• | otherwise = elem k xs
• elem k [] = False

OO class 21. Instance of Type Classes

• instanceEq (Maybe a) where
• Just v1 == Just v2 = v1 == v2
• Nothing == Nothing = True
• _ == _= False

22. Lazy Evaluation

• a = [1..] -- infinite list
• take 3 a -- [1,2,3]

23. Purity

• Functional

24. Purity

• variable
• a = 3
• a = 19

25. Purity

• side effect

26. Referential Transparent

• 2 * f(x) f(x) + f(x)
• int y = 10;
• int f(int i)
• {
• return i + y++;
• }
• f(5) + f(5); // 15 + 16 = 31
• 2 * f(5);// 2 * 15 = 30

27.

28. IO Action

• IO () IO first-class IO IO main IO
• IO
• IO main

--putStrLn :: String -> IO () --main :: IO () main = putStrLn "Hi Haskell!" 29. Monad

• IO Action Monad
• Monster
• --Simon Peyton Jones Haskell

• class Monad m where
• bind:: m a -> (a -> m b) -> m b
• inject:: a ->m a

Monad m a bind inject a (a -> m b) 31. Example -

• DSL
• Algebraic Data Type

32. Example - , Hommage Haskell Offline Music Manipulation And Generation EDSL http://substitut-fuer-feinmotorik.net/projects/haskellommage 33.

34.

• forkIO :: IO () -> IO ThreadId

35. Shared Mutable State Concurrency

• Race conditions due to forgotten locks
• Deadlocks resulting from inconsistent lock ordering
• Corruption caused by uncaught exceptions
• Lost wakeups induced by omitted notifications

36. Software Transactional Memory(STM)

• transaction
• a b
• transaction
• transaction

37. STM Sample

• import Control.Concurrent.STM
• type Bal =TVarFloat
• transfer::Float->Bal->Bal->STM ()
• transfer qty fromBal toBal = do
• fromQty STM a
• catchSTM :: STM a -> (exception->STM a) -> STM a
• instance Monad STM
• complexAction = action1 `orElse` action2
• main = atomically complexAction

40.

• --
• sort :: (Ord a) => [a] -> [a]
• sort (x:xs) = lesser ++ x:greater
• where lesser = sort [y | y [a] -> [a]
• parSort (x:xs) =forcegreater`par`
• ( forcelesser`pseq`(lesser ++ x:greater))
• where lesser = parSort [y | y (a -> b) -- map function
• -> Strategy c -- evaluation strategy for reduction
• -> ([b] -> c) -- reduce function
• -> [a] -- list to map over
• -> c
• mapReduce mapStrat mapFunc reduceStrat reduceFunc input
• = mapResult `pseq` reduceResult
• where mapResult = parMap mapStrat mapFunc input
• reduceResult = reduceFunc mapResult `using` reduceStrat

45.

• GPU
• CPU
• GPU Shader CUDA OpenCL
• GPU FP
• GPipe EDSL GPU , Haskell GPU Haskell OpenGL GLSL

46. Example - GPU

• GPipe

47. Parser Combinator

• parse
• parse
• ( Lex Yacc Flex Bison Antlr) parse
• Haskell parser Parser Combinator

48. Example - Parser Combinator for JavaScript

• parser :String -> Result
• Result 3
• remaining: parse
• matched: parser parse
• ast: parse
• parser
• range("0", "9")
• token("begin")
• parser combinator ( )parser
• repeat1(alternate(sequence(parser1, parser2), parser3))

49. EDSL

• first-class
• combinator

50.

• --
• TheComputerLanguageBenchmarksGame
• http://shootout.alioth.debian.org/
• Haskell C 1/5
• 2D
• http://blog.csdn.net/soloist/archive/2009/04/09/4060081.aspx

51. 52. 53.