learn haskell the easy way
TRANSCRIPT
Learn Haskell The Easy Way
大貓 (@miaout17)
兩星期前• 來辦函數語言聚會吧…• 可應該很冷門 (?)• 至少會有六七個人…吧
︽⊙_⊙︽
大貓 (@miaout17)
2004 2007 2010
C, C++Java, Assembly…
C++
2008 2009
高中數學
用高中數學及遊戲的方式介紹純函數語言的一些基礎元素
Haskell?
Static Typed
Single Assignment
Pure Functional
Pattern Matching
Higher-OrderFunction
Applicative
PersistenceData Structure
First-Class FunctionType Class
ArrowCategoryLazy Evaluation
Functor
Monad
Curried function
Haskell
• 比起指令式語言,更像(高中)數學式– 純函數– 單一指派– 樣式比對
函數• 國中數學考題
下式何者可能為 y=f(x) 的函數圖形?
• 上列何者可能為 f:R->R, y=f(x) 的函數圖形
純函數1. 給與相同參數,得到相同結果2. 無副作用– 改變狀態– 輸入輸出
以 C 為例 – 非純函數
See also: • rand• strtok• printf• …
以 C 為例 – 純函數
See also: • sin• strlen• encrypt• …
Haskell – 純函數
See also:• Curried function• Type class• IO monad
單一指派
單一指派 (Haskell)
樣式比對• 記得嗎?高中就學過遞迴喔!
• 也可以寫成:
數學式 V.S. Haskell 源碼(Pattern Matching)
• Haskell (pattern matching) :
• 數學式:
數學式 V.S. Haskell 源碼(Case expression)
• Haskell (case expression) :
• 數學式:
數學式 V.S. Haskell 源碼(guard)
• Haskell (guard) :
• 數學式:
數學式 V.S. Haskell 源碼(tail recursion)
• Haskell (tail recursion) :
• 數學式: • 展開範列:
數學式 V.S. Haskell 源碼(higher-order function)
• Haskell (higher-order function) :
• 你可能已經用過 fold 了– Python: reduce– Ruby: Array#inject– Underscore(JS): _.reduce
• 以上介紹了 Haskell 部份的基礎元素• 可以做什麼?• 來玩遊戲吧!
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
HaskellWarrior
• action : StageState -> Action • 定義一個純函數– Domain: StageState ( 關卡狀態 ) – Codomain: Action ( 戰士的行動 )
• Haskell code:
Live Demo!
未完成的傳說…• https://github.com/miaout17/haskell-warrior-
prototype• OK for demo, but not playable• 擇日砍掉重練