SlideShare a Scribd company logo
Learn Haskell The Easy Way

      大貓 (@miaout17)
Slides available online




 http://blog.miaout17.net
        大貓共和國
兩星期前
• 來辦函數語言聚會吧…
• 可應該很冷門(?)
• 至少會有六七個人…吧
︽⊙_⊙︽
大貓(@miaout17)

                            C++



     C, C++
Java, Assembly…



              2004        2007    2008   2009   2010
                          高中
                          數學
用高中數學及遊戲的方式
介紹純函數語言的一些基礎元素
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
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
• 擇日砍掉重練
Thanks
Any Question?
                http://blog.miaout17.net
                       大貓共和國

More Related Content

PPTX
那些函數語言Tutorial沒有教我的事
PPT
Java SE 8 技術手冊第 3 章 - 基礎語法
PPT
Java SE 8 技術手冊第 15 章 - 通用API
PDF
Java SE 7 技術手冊投影片第 03 章 - 基礎語法
PDF
Java SE 7 技術手冊投影片第 06 章 - 繼承與多型
PPTX
[OSDC12]相依性管理 - 以Ruby開發為例
PDF
淺談Ruby process fork應用
PPTX
El Protocolo de Kioto
那些函數語言Tutorial沒有教我的事
Java SE 8 技術手冊第 3 章 - 基礎語法
Java SE 8 技術手冊第 15 章 - 通用API
Java SE 7 技術手冊投影片第 03 章 - 基礎語法
Java SE 7 技術手冊投影片第 06 章 - 繼承與多型
[OSDC12]相依性管理 - 以Ruby開發為例
淺談Ruby process fork應用
El Protocolo de Kioto

Viewers also liked (7)

PPS
Fotos graciosas 33657
PDF
Haskell study 8
PDF
Haskell in the Real World
PDF
Functional Programming by Examples using Haskell
PDF
Functional programming with haskell
PDF
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
PDF
Real World Haskell: Lecture 1
Fotos graciosas 33657
Haskell study 8
Haskell in the Real World
Functional Programming by Examples using Haskell
Functional programming with haskell
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
Real World Haskell: Lecture 1
Ad

Learn Haskell The Easy Way