SlideShare ist ein Scribd-Unternehmen logo
1 von 26
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
                       大貓共和國

Weitere ähnliche Inhalte

Andere mochten auch

Fotos graciosas 33657
Fotos graciosas 33657Fotos graciosas 33657
Fotos graciosas 33657serviojapon
 
Haskell in the Real World
Haskell in the Real WorldHaskell in the Real World
Haskell in the Real Worldosfameron
 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskellgoncharenko
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskellfaradjpour
 
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git민태 김
 
Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Bryan O'Sullivan
 

Andere mochten auch (7)

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

Learn Haskell The Easy Way