Successfully reported this slideshow.
Your SlideShare is downloading. ×

Learn Haskell The Easy Way

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 26 Ad

More Related Content

Recently uploaded (20)

Advertisement

Learn Haskell The Easy Way

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

×