learn haskell the easy way

26
Learn Haskell The Easy Way 大大 (@miaout17)

Upload: yc-ling

Post on 12-May-2015

5.450 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Learn Haskell The Easy Way

Learn Haskell The Easy Way

大貓 (@miaout17)

Page 2: Learn Haskell The Easy Way

Slides available online

http://blog.miaout17.net大貓共和國

Page 3: Learn Haskell The Easy Way

兩星期前• 來辦函數語言聚會吧…• 可應該很冷門 (?)• 至少會有六七個人…吧

Page 4: Learn Haskell The Easy Way

︽⊙_⊙︽

Page 5: Learn Haskell The Easy Way

大貓 (@miaout17)

2004 2007 2010

C, C++Java, Assembly…

C++

2008 2009

高中數學

Page 6: Learn Haskell The Easy Way

用高中數學及遊戲的方式介紹純函數語言的一些基礎元素

Page 7: Learn Haskell The Easy Way

Haskell?

Static Typed

Single Assignment

Pure Functional

Pattern Matching

Higher-OrderFunction

Applicative

PersistenceData Structure

First-Class FunctionType Class

ArrowCategoryLazy Evaluation

Functor

Monad

Curried function

Page 8: Learn Haskell The Easy Way

Haskell

• 比起指令式語言,更像(高中)數學式– 純函數– 單一指派– 樣式比對

Page 9: Learn Haskell The Easy Way

函數• 國中數學考題

下式何者可能為 y=f(x) 的函數圖形?

• 上列何者可能為 f:R->R, y=f(x) 的函數圖形

Page 10: Learn Haskell The Easy Way

純函數1. 給與相同參數,得到相同結果2. 無副作用– 改變狀態– 輸入輸出

Page 11: Learn Haskell The Easy Way

以 C 為例 – 非純函數

See also: • rand• strtok• printf• …

Page 12: Learn Haskell The Easy Way

以 C 為例 – 純函數

See also: • sin• strlen• encrypt• …

Page 13: Learn Haskell The Easy Way

Haskell – 純函數

See also:• Curried function• Type class• IO monad

Page 14: Learn Haskell The Easy Way

單一指派

Page 15: Learn Haskell The Easy Way

單一指派 (Haskell)

Page 16: Learn Haskell The Easy Way

樣式比對• 記得嗎?高中就學過遞迴喔!

• 也可以寫成:

Page 17: Learn Haskell The Easy Way

數學式 V.S. Haskell 源碼(Pattern Matching)

• Haskell (pattern matching) :

• 數學式:

Page 18: Learn Haskell The Easy Way

數學式 V.S. Haskell 源碼(Case expression)

• Haskell (case expression) :

• 數學式:

Page 19: Learn Haskell The Easy Way

數學式 V.S. Haskell 源碼(guard)

• Haskell (guard) :

• 數學式:

Page 20: Learn Haskell The Easy Way

數學式 V.S. Haskell 源碼(tail recursion)

• Haskell (tail recursion) :

• 數學式: • 展開範列:

Page 21: Learn Haskell The Easy Way

數學式 V.S. Haskell 源碼(higher-order function)

• Haskell (higher-order function) :

• 你可能已經用過 fold 了– Python: reduce– Ruby: Array#inject– Underscore(JS): _.reduce

Page 22: Learn Haskell The Easy Way

• 以上介紹了 Haskell 部份的基礎元素• 可以做什麼?• 來玩遊戲吧!

Page 23: Learn Haskell The Easy Way

RubyWarrior

• A game designed to teach the Ruby language and artificial intelligence in a fun, interactive way.

• Ruby Tuesday #16: Learn Ruby with RubyWarrior– http://www.slideshare.net/miaout17/learning-ruby-with-

rubywarrior

Page 24: Learn Haskell The Easy Way

HaskellWarrior

• action : StageState -> Action • 定義一個純函數– Domain: StageState ( 關卡狀態 ) – Codomain: Action ( 戰士的行動 )

• Haskell code:

Live Demo!

Page 25: Learn Haskell The Easy Way

未完成的傳說…• https://github.com/miaout17/haskell-warrior-

prototype• OK for demo, but not playable• 擇日砍掉重練

Page 26: Learn Haskell The Easy Way

ThanksAny Question?

http://blog.miaout17.net大貓共和國