SlideShare una empresa de Scribd logo
1 de 38
Descargar para leer sin conexión
From Ruby to Haskell
                            Kansai Yamiruby Kaigi
                                Nov 13, 2011
                              Tatsuhiro Ujihisa




Sunday, November 13, 2011
Agenda
               • Haskell (introduction)
               • Haskell (for beginners)
               • Self-introduction
               • Haskell (for average people)
               • Haskell (for pro)

Sunday, November 13, 2011
Haskell (1/3)



Sunday, November 13, 2011
Haskell
               • Programming language
               • 1990~
               • GHC



Sunday, November 13, 2011
Haskell
               • Strong static-typed
               • Interpreter & native compiler
               • Lazy evaluation

                            main = print "hello"




Sunday, November 13, 2011
FizzBuzz in Ruby
 1 (1..100).each do |i|               1   def main
                                      2    puts fizzbuzz
 2 puts i % 15 == 0 ? 'FizzBuzz' :
                                      3   end
 3 i % 5 == 0 ? 'Buzz' :
                                      4
 4 i % 3 == 0 ? 'Fizz' :              5   def fizzbuzz
 5 i                                  6    (1..100).map {|i|
 6 end                                7      i % 15 == 0 ? 'FizzBuzz' :
                                      8        i % 5 == 0 ? 'Buzz' :
                                      9        i % 3 == 0 ? 'Fizz' :
                                     10        i
                                     11    }
                                     12   end
                                     13
                                     14   main

Sunday, November 13, 2011
FizzBuzz in Ruby
            1     def main                         1   def main
            2      puts fizzbuzz                    2    puts fizzbuzz
            3     end                              3   end
            4                                      4
            5     def fizzbuzz                      5   def fizzbuzz
            6      f = -> i do                     6    (1..100).map {|i|
            7        i % 15 == 0 ? 'FizzBuzz' :    7      i % 15 == 0 ? 'FizzBuzz' :
            8          i % 5 == 0 ? 'Buzz' :       8        i % 5 == 0 ? 'Buzz' :
            9          i % 3 == 0 ? 'Fizz' :       9        i % 3 == 0 ? 'Fizz' :
           10          i                          10        i
           11      end                            11    }
           12      (1..100).map &f                12   end
           13     end                             13
           14                                     14   main
           15     main
Sunday, November 13, 2011
FizzBuzz in Haskell
            1     def main                      1 main = print fizzbuzz
            2      puts fizzbuzz                 2
            3     end                           3 fizzbuzz = map f [1..100]
            4                                   4 where f n
            5     def fizzbuzz                   5     | n `rem` 15 == 0 = "FizzBuzz"
            6      f = -> i do                  6     | n `rem` 5 == 0 = "Buzz"
            7        i % 15 == 0 ? 'FizzBuzz' : 7     | n `rem` 3 == 0 = "Fizz"
            8          i % 5 == 0 ? 'Buzz' :    8     | otherwise = show n
            9        i % 3 == 0 ? 'Fizz' :
           10        i
           11      end
           12      (1..100).map &f
           13     end
           14
           15     main
Sunday, November 13, 2011
Haskell (2/3)



Sunday, November 13, 2011
1 main = print (a + b)                        1   main = print (a + b)
       2 a=1                                         2   a = 1 :: Int
       3 b=2                                         3   b :: Int
                                                     4   b=2




         1    main = print (a + b)
         2    a = 1 :: Int
                           1 a.hs:4:5:
         3    b :: Int     2 No instance for (Fractional Int)
         4    b = 2.1      3    arising from the literal `2.1'
                            4   Possible fix: add an instance declaration for (Fractional Int)
                            5   In the expression: 2.1
                            6   In an equation for `b': b = 2.1
                            7


Sunday, November 13, 2011
Type and type-class
               • type (in Haskell) =~ class (in Ruby)
               • type class (in Haskell) =~
                    module (in Ruby)


               • Int type =~ Fixnum class
               • Num type-class =~ Numeric module

Sunday, November 13, 2011
Literals
               • Ruby: only one type
                • 1 (always Fixnum)
                • "hello" (always String)
               • Haskell: compiler infers
                • 1 (Int? Integer? Float? Rational?)
                • "a" (String? Test? ByteString?)
Sunday, November 13, 2011
1 main = print (a + b)
                      error      2 a = 1 :: Int
                                 3 b = 2.1




                                 1 main = print (a + b)
                            ok   2 a=1
                                 3 b = 2.1


                b and (a+b) can be Float, Double or Rational


Sunday, November 13, 2011
Lists
               • [1, 2, 3]
               • 1 : 2 : 3 : []
               • ["a", "b", "c"]
               • [1, 2, "a"] ERROR


Sunday, November 13, 2011
Functions
               • call
                • fa
                • gab
               • define
                • f a = something
                • g a b = something
Sunday, November 13, 2011
Operators
               • call
                • 1+2
                • (+) 1 2
                • 1 `g` 2
               • define
                • a + b = something
Sunday, November 13, 2011
Interesting operators
               •$
                • f (g (h a b)))
                • f$g$hab
               • a : b : c : [] == [a, b, c]
               • expected @=# actual

Sunday, November 13, 2011
Combining actions
                             main = print [1, 2, 3] >> print "hi"


                                       1 main = do
                                       2 print [1, 2, 3]
                                       3 print "hi"



                            main = do { print [1, 2, 3]; print "hi" }




Sunday, November 13, 2011
only for side effect
                                  • Ruby
                                   • puts 'hi'
                                     puts 'yo'
     • Haskell                     • puts 'hi'; puts 'yo'
      • do
                  putStrLn "hi"
                  putStrLn "yo"
          • putStrLn "hi" >> putStrLn "yo"
Sunday, November 13, 2011
side effect to get a val
               • Ruby            •Haskell
                • a = gets        • do
                            pa       a <- getContent
                                     print a
                                  • getContent >>= print


Sunday, November 13, 2011
self-introduction



Sunday, November 13, 2011
Tatsuhiro Ujihisa
               • @ujm
               • Vancouver
               • HootSuite media inc
               • Vim


Sunday, November 13, 2011
• http://ujihisa.blogspot.com
               • http://vim-users.jp
               • http://github.com/ujihisa



Sunday, November 13, 2011
Haskell (3/3)



Sunday, November 13, 2011
re: FizzBuzz in Haskell
            1     def main                      1 main = print fizzbuzz
            2      puts fizzbuzz                 2
            3     end                           3 fizzbuzz = map f [1..100]
            4                                   4 where f n
            5     def fizzbuzz                   5     | n `rem` 15 == 0 = "FizzBuzz"
            6      f = -> i do                  6     | n `rem` 5 == 0 = "Buzz"
            7        i % 15 == 0 ? 'FizzBuzz' : 7     | n `rem` 3 == 0 = "Fizz"
            8          i % 5 == 0 ? 'Buzz' :    8     | otherwise = show n
            9        i % 3 == 0 ? 'Fizz' :
           10        i
           11      end
           12      (1..100).map &f
           13     end
           14
           15     main
Sunday, November 13, 2011
output
   1 main = print fizzbuzz                  ["1","2","Fizz","4","Buzz","Fiz
   2                                       z","7","8","Fizz","Buzz","11","
   3 fizzbuzz = map f [1..100]             Fizz","13","14","FizzBuzz","16
   4 where f n                            ","17","Fizz","19","Buzz","Fizz
                                          ","22","23","Fizz","Buzz","26",
   5     | n `rem` 15 == 0 = "FizzBuzz"
                                          "Fizz","28","29","FizzBuzz","3
   6     | n `rem` 5 == 0 = "Buzz"
                                          1","32","Fizz","34","Buzz","Fiz
   7     | n `rem` 3 == 0 = "Fizz"        z","37","38","Fizz","Buzz","41
   8     | otherwise = show n             ","Fizz","43","44","FizzBuzz","
                                          46","47","Fizz","49","Buzz","F
                                          izz","52","53","Fizz","Buzz","5
                                          6","Fizz","58","59","FizzBuzz"
                                          ,"61","62","Fizz","64","Buzz","
                            like p        Fizz","67","68","Fizz","Buzz","
                                          71","Fizz","73","74","FizzBuzz
                                          ","76","77","Fizz","79","Buzz",
Sunday, November 13, 2011
                                          "Fizz","82","83","Fizz","Buzz",
output
                                          1
   1 main = mapM_ putStrLn fizzbuzz        2
   2                                      Fizz
                                          4
   3 fizzbuzz = map f [1..100]             Buzz
   4 where f n                            Fizz
                                          7
   5     | n `rem` 15 == 0 = "FizzBuzz"   8
   6     | n `rem` 5 == 0 = "Buzz"        Fizz
                                          Buzz
   7     | n `rem` 3 == 0 = "Fizz"        11
                                          Fizz
   8     | otherwise = show n             13
                                          14
                                          FizzBuzz
                                          16
                                          17
                                          Fizz
                                          19
                            like puts     Buzz
                                          Fizz
                                          22
                                          23
                                          Fizz
Sunday, November 13, 2011
mapM_
               • f x >> f y >> f z
               • do
                       fx
                       fy
                       fz
               • mapM_ f [x, y, z]

Sunday, November 13, 2011
mapM_
               • ((f x) >> f y) >> f z
               • foldl1 (>>) $ map f [x, y, z]
               • f x >> (f y >> (f z))
               • foldr1 (>>) $ map f [x, y, z]
               • sequence_ (map f [x, y, z])
Sunday, November 13, 2011
1 main = mapM_ putStrLn fizzbuzz        1 main = mapM_ putStrLn fizzbuzz
   2                                      2
   3 fizzbuzz = map f [1..100]             3 fizzbuzz = map f [1..100]
   4 where f n                            4 where f n = case () of
   5     | n `rem` 15 == 0 = "FizzBuzz"   5     n `rem` 15 == 0 -> "FizzBuzz"
   6     | n `rem` 5 == 0 = "Buzz"        6     n `rem` 5 == 0 -> "Buzz"
   7     | n `rem` 3 == 0 = "Fizz"        7     n `rem` 3 == 0 -> "Fizz"
   8     | otherwise = show n             8     otherwise -> show n




Sunday, November 13, 2011
map, flip, and for
               • map f list
               • flip map list f
               • mapM_ f list
               • flip mapM_ list f
               • forM_ list f
Sunday, November 13, 2011
1 main = mapM_ putStrLn fizzbuzz
                                       2
                                       3 fizzbuzz = map f [1..100]
                                       4 where f n = case () of _
                                       5     | n `rem` 15 == 0 -> "FizzBuzz"
                                       6     | n `rem` 5 == 0 -> "Buzz"
                                       7     | n `rem` 3 == 0 -> "Fizz"
   1 import Control.Monad (forM_)
                                       8     | otherwise -> show n
   2 main = forM_ [1..100] f
   3 where
   4 f n = putStrLn $ case () of _
   5                  | n `rem` 15 == 0 -> "FizzBuzz"
   6                  | n `rem` 5 == 0 -> "Buzz"
   7                  | n `rem` 3 == 0 -> "Fizz"
   8                  | otherwise -> show n



Sunday, November 13, 2011
1 import Control.Monad (forM_)
                                   2 main = forM_ [1..100] $ n ->
                                   3 putStrLn $ case () of _
                                   4               | n `rem` 15 == 0 -> "FizzBuzz"
                                   5               | n `rem` 5 == 0 -> "Buzz"
                                   6               | n `rem` 3 == 0 -> "Fizz"
   1   import Control.Monad (forM_)7               | otherwise -> show n
   2   main = forM_ [1..100] f
   3    where
   4     f n = putStrLn $ case () of _
   5                    | n `rem` 15 == 0 -> "FizzBuzz"
   6                    | n `rem` 5 == 0 -> "Buzz"
   7                    | n `rem` 3 == 0 -> "Fizz"
   8                    | otherwise -> show n



Sunday, November 13, 2011
Haskell as shell scripts
               • Succinct expressions
               • Powerful list-manipulation
               • Powerful text-manipulation
               • Reasonably fast to boot
               • Healthy

Sunday, November 13, 2011
Parsec
               • Parser combinators
               • Standard library
               • No need for handling state
                    explicitly




Sunday, November 13, 2011
Haskell web framework
               • Yesod
               • Snap (framework)
               •



Sunday, November 13, 2011
Appendix
               • IDE-integration
                • run on the IDE
                • keyword completion
                • Prelude type/function completion
                • module name completion
                • third party t/f completion
                • pragma completion
Sunday, November 13, 2011
quickrun.vim
               • DEMO
               • runghc
               • ghc



Sunday, November 13, 2011

Más contenido relacionado

Destacado

[WebCamp2014] Towards functional web
[WebCamp2014] Towards functional web[WebCamp2014] Towards functional web
[WebCamp2014] Towards functional webBlaž Repas
 
Web programming in Haskell
Web programming in HaskellWeb programming in Haskell
Web programming in Haskellchriseidhof
 
Functional programming seminar (haskell)
Functional programming seminar (haskell)Functional programming seminar (haskell)
Functional programming seminar (haskell)Bikram Thapa
 
Haskell in the Real World
Haskell in the Real WorldHaskell in the Real World
Haskell in the Real Worldosfameron
 
Building a website in Haskell coming from Node.js
Building a website in Haskell coming from Node.jsBuilding a website in Haskell coming from Node.js
Building a website in Haskell coming from Node.jsNicolas Hery
 
Online movie ticket booking
Online movie ticket bookingOnline movie ticket booking
Online movie ticket bookingmrinnovater007
 

Destacado (6)

[WebCamp2014] Towards functional web
[WebCamp2014] Towards functional web[WebCamp2014] Towards functional web
[WebCamp2014] Towards functional web
 
Web programming in Haskell
Web programming in HaskellWeb programming in Haskell
Web programming in Haskell
 
Functional programming seminar (haskell)
Functional programming seminar (haskell)Functional programming seminar (haskell)
Functional programming seminar (haskell)
 
Haskell in the Real World
Haskell in the Real WorldHaskell in the Real World
Haskell in the Real World
 
Building a website in Haskell coming from Node.js
Building a website in Haskell coming from Node.jsBuilding a website in Haskell coming from Node.js
Building a website in Haskell coming from Node.js
 
Online movie ticket booking
Online movie ticket bookingOnline movie ticket booking
Online movie ticket booking
 

Más de ujihisa

vimconf2013
vimconf2013vimconf2013
vimconf2013ujihisa
 
KOF2013 Minecraft / Clojure
KOF2013 Minecraft / ClojureKOF2013 Minecraft / Clojure
KOF2013 Minecraft / Clojureujihisa
 
Keynote ujihisa.vim#2
Keynote ujihisa.vim#2Keynote ujihisa.vim#2
Keynote ujihisa.vim#2ujihisa
 
vimshell made other shells legacy
vimshell made other shells legacyvimshell made other shells legacy
vimshell made other shells legacyujihisa
 
Text Manipulation with/without Parsec
Text Manipulation with/without ParsecText Manipulation with/without Parsec
Text Manipulation with/without Parsecujihisa
 
CoffeeScript in hootsuite
CoffeeScript in hootsuiteCoffeeScript in hootsuite
CoffeeScript in hootsuiteujihisa
 
HootSuite Dev 2
HootSuite Dev 2HootSuite Dev 2
HootSuite Dev 2ujihisa
 
Ruby Kansai49
Ruby Kansai49Ruby Kansai49
Ruby Kansai49ujihisa
 
Hootsuite dev 2011
Hootsuite dev 2011Hootsuite dev 2011
Hootsuite dev 2011ujihisa
 
LLVM Workshop Osaka Umeda, Japan
LLVM Workshop Osaka Umeda, JapanLLVM Workshop Osaka Umeda, Japan
LLVM Workshop Osaka Umeda, Japanujihisa
 
RubyConf 2009 LT "Termtter"
RubyConf 2009 LT "Termtter"RubyConf 2009 LT "Termtter"
RubyConf 2009 LT "Termtter"ujihisa
 
Hacking parse.y (RubyConf 2009)
Hacking parse.y (RubyConf 2009)Hacking parse.y (RubyConf 2009)
Hacking parse.y (RubyConf 2009)ujihisa
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)ujihisa
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisaujihisa
 
Ruby Kansai #35 About RubyKaigi2009 ujihisa
Ruby Kansai #35 About RubyKaigi2009 ujihisaRuby Kansai #35 About RubyKaigi2009 ujihisa
Ruby Kansai #35 About RubyKaigi2009 ujihisaujihisa
 
Kof2008 Itll
Kof2008 ItllKof2008 Itll
Kof2008 Itllujihisa
 
All About Metarw -- VimM#2
All About Metarw -- VimM#2All About Metarw -- VimM#2
All About Metarw -- VimM#2ujihisa
 
Itc2008 Ujihisa
Itc2008 UjihisaItc2008 Ujihisa
Itc2008 Ujihisaujihisa
 
Agile Web Posting With Ruby / Ruby Kaigi2008
Agile Web Posting With Ruby / Ruby Kaigi2008Agile Web Posting With Ruby / Ruby Kaigi2008
Agile Web Posting With Ruby / Ruby Kaigi2008ujihisa
 
Agile Web Posting with Ruby (lang:ja)
Agile Web Posting with Ruby (lang:ja)Agile Web Posting with Ruby (lang:ja)
Agile Web Posting with Ruby (lang:ja)ujihisa
 

Más de ujihisa (20)

vimconf2013
vimconf2013vimconf2013
vimconf2013
 
KOF2013 Minecraft / Clojure
KOF2013 Minecraft / ClojureKOF2013 Minecraft / Clojure
KOF2013 Minecraft / Clojure
 
Keynote ujihisa.vim#2
Keynote ujihisa.vim#2Keynote ujihisa.vim#2
Keynote ujihisa.vim#2
 
vimshell made other shells legacy
vimshell made other shells legacyvimshell made other shells legacy
vimshell made other shells legacy
 
Text Manipulation with/without Parsec
Text Manipulation with/without ParsecText Manipulation with/without Parsec
Text Manipulation with/without Parsec
 
CoffeeScript in hootsuite
CoffeeScript in hootsuiteCoffeeScript in hootsuite
CoffeeScript in hootsuite
 
HootSuite Dev 2
HootSuite Dev 2HootSuite Dev 2
HootSuite Dev 2
 
Ruby Kansai49
Ruby Kansai49Ruby Kansai49
Ruby Kansai49
 
Hootsuite dev 2011
Hootsuite dev 2011Hootsuite dev 2011
Hootsuite dev 2011
 
LLVM Workshop Osaka Umeda, Japan
LLVM Workshop Osaka Umeda, JapanLLVM Workshop Osaka Umeda, Japan
LLVM Workshop Osaka Umeda, Japan
 
RubyConf 2009 LT "Termtter"
RubyConf 2009 LT "Termtter"RubyConf 2009 LT "Termtter"
RubyConf 2009 LT "Termtter"
 
Hacking parse.y (RubyConf 2009)
Hacking parse.y (RubyConf 2009)Hacking parse.y (RubyConf 2009)
Hacking parse.y (RubyConf 2009)
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisa
 
Ruby Kansai #35 About RubyKaigi2009 ujihisa
Ruby Kansai #35 About RubyKaigi2009 ujihisaRuby Kansai #35 About RubyKaigi2009 ujihisa
Ruby Kansai #35 About RubyKaigi2009 ujihisa
 
Kof2008 Itll
Kof2008 ItllKof2008 Itll
Kof2008 Itll
 
All About Metarw -- VimM#2
All About Metarw -- VimM#2All About Metarw -- VimM#2
All About Metarw -- VimM#2
 
Itc2008 Ujihisa
Itc2008 UjihisaItc2008 Ujihisa
Itc2008 Ujihisa
 
Agile Web Posting With Ruby / Ruby Kaigi2008
Agile Web Posting With Ruby / Ruby Kaigi2008Agile Web Posting With Ruby / Ruby Kaigi2008
Agile Web Posting With Ruby / Ruby Kaigi2008
 
Agile Web Posting with Ruby (lang:ja)
Agile Web Posting with Ruby (lang:ja)Agile Web Posting with Ruby (lang:ja)
Agile Web Posting with Ruby (lang:ja)
 

Último

Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 

Último (20)

Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 

Kansai Ruby Conference Talk on Haskell

  • 1. From Ruby to Haskell Kansai Yamiruby Kaigi Nov 13, 2011 Tatsuhiro Ujihisa Sunday, November 13, 2011
  • 2. Agenda • Haskell (introduction) • Haskell (for beginners) • Self-introduction • Haskell (for average people) • Haskell (for pro) Sunday, November 13, 2011
  • 4. Haskell • Programming language • 1990~ • GHC Sunday, November 13, 2011
  • 5. Haskell • Strong static-typed • Interpreter & native compiler • Lazy evaluation main = print "hello" Sunday, November 13, 2011
  • 6. FizzBuzz in Ruby 1 (1..100).each do |i| 1 def main 2 puts fizzbuzz 2 puts i % 15 == 0 ? 'FizzBuzz' : 3 end 3 i % 5 == 0 ? 'Buzz' : 4 4 i % 3 == 0 ? 'Fizz' : 5 def fizzbuzz 5 i 6 (1..100).map {|i| 6 end 7 i % 15 == 0 ? 'FizzBuzz' : 8 i % 5 == 0 ? 'Buzz' : 9 i % 3 == 0 ? 'Fizz' : 10 i 11 } 12 end 13 14 main Sunday, November 13, 2011
  • 7. FizzBuzz in Ruby 1 def main 1 def main 2 puts fizzbuzz 2 puts fizzbuzz 3 end 3 end 4 4 5 def fizzbuzz 5 def fizzbuzz 6 f = -> i do 6 (1..100).map {|i| 7 i % 15 == 0 ? 'FizzBuzz' : 7 i % 15 == 0 ? 'FizzBuzz' : 8 i % 5 == 0 ? 'Buzz' : 8 i % 5 == 0 ? 'Buzz' : 9 i % 3 == 0 ? 'Fizz' : 9 i % 3 == 0 ? 'Fizz' : 10 i 10 i 11 end 11 } 12 (1..100).map &f 12 end 13 end 13 14 14 main 15 main Sunday, November 13, 2011
  • 8. FizzBuzz in Haskell 1 def main 1 main = print fizzbuzz 2 puts fizzbuzz 2 3 end 3 fizzbuzz = map f [1..100] 4 4 where f n 5 def fizzbuzz 5 | n `rem` 15 == 0 = "FizzBuzz" 6 f = -> i do 6 | n `rem` 5 == 0 = "Buzz" 7 i % 15 == 0 ? 'FizzBuzz' : 7 | n `rem` 3 == 0 = "Fizz" 8 i % 5 == 0 ? 'Buzz' : 8 | otherwise = show n 9 i % 3 == 0 ? 'Fizz' : 10 i 11 end 12 (1..100).map &f 13 end 14 15 main Sunday, November 13, 2011
  • 10. 1 main = print (a + b) 1 main = print (a + b) 2 a=1 2 a = 1 :: Int 3 b=2 3 b :: Int 4 b=2 1 main = print (a + b) 2 a = 1 :: Int 1 a.hs:4:5: 3 b :: Int 2 No instance for (Fractional Int) 4 b = 2.1 3 arising from the literal `2.1' 4 Possible fix: add an instance declaration for (Fractional Int) 5 In the expression: 2.1 6 In an equation for `b': b = 2.1 7 Sunday, November 13, 2011
  • 11. Type and type-class • type (in Haskell) =~ class (in Ruby) • type class (in Haskell) =~ module (in Ruby) • Int type =~ Fixnum class • Num type-class =~ Numeric module Sunday, November 13, 2011
  • 12. Literals • Ruby: only one type • 1 (always Fixnum) • "hello" (always String) • Haskell: compiler infers • 1 (Int? Integer? Float? Rational?) • "a" (String? Test? ByteString?) Sunday, November 13, 2011
  • 13. 1 main = print (a + b) error 2 a = 1 :: Int 3 b = 2.1 1 main = print (a + b) ok 2 a=1 3 b = 2.1 b and (a+b) can be Float, Double or Rational Sunday, November 13, 2011
  • 14. Lists • [1, 2, 3] • 1 : 2 : 3 : [] • ["a", "b", "c"] • [1, 2, "a"] ERROR Sunday, November 13, 2011
  • 15. Functions • call • fa • gab • define • f a = something • g a b = something Sunday, November 13, 2011
  • 16. Operators • call • 1+2 • (+) 1 2 • 1 `g` 2 • define • a + b = something Sunday, November 13, 2011
  • 17. Interesting operators •$ • f (g (h a b))) • f$g$hab • a : b : c : [] == [a, b, c] • expected @=# actual Sunday, November 13, 2011
  • 18. Combining actions main = print [1, 2, 3] >> print "hi" 1 main = do 2 print [1, 2, 3] 3 print "hi" main = do { print [1, 2, 3]; print "hi" } Sunday, November 13, 2011
  • 19. only for side effect • Ruby • puts 'hi' puts 'yo' • Haskell • puts 'hi'; puts 'yo' • do putStrLn "hi" putStrLn "yo" • putStrLn "hi" >> putStrLn "yo" Sunday, November 13, 2011
  • 20. side effect to get a val • Ruby •Haskell • a = gets • do pa a <- getContent print a • getContent >>= print Sunday, November 13, 2011
  • 22. Tatsuhiro Ujihisa • @ujm • Vancouver • HootSuite media inc • Vim Sunday, November 13, 2011
  • 23. • http://ujihisa.blogspot.com • http://vim-users.jp • http://github.com/ujihisa Sunday, November 13, 2011
  • 25. re: FizzBuzz in Haskell 1 def main 1 main = print fizzbuzz 2 puts fizzbuzz 2 3 end 3 fizzbuzz = map f [1..100] 4 4 where f n 5 def fizzbuzz 5 | n `rem` 15 == 0 = "FizzBuzz" 6 f = -> i do 6 | n `rem` 5 == 0 = "Buzz" 7 i % 15 == 0 ? 'FizzBuzz' : 7 | n `rem` 3 == 0 = "Fizz" 8 i % 5 == 0 ? 'Buzz' : 8 | otherwise = show n 9 i % 3 == 0 ? 'Fizz' : 10 i 11 end 12 (1..100).map &f 13 end 14 15 main Sunday, November 13, 2011
  • 26. output 1 main = print fizzbuzz ["1","2","Fizz","4","Buzz","Fiz 2 z","7","8","Fizz","Buzz","11"," 3 fizzbuzz = map f [1..100] Fizz","13","14","FizzBuzz","16 4 where f n ","17","Fizz","19","Buzz","Fizz ","22","23","Fizz","Buzz","26", 5 | n `rem` 15 == 0 = "FizzBuzz" "Fizz","28","29","FizzBuzz","3 6 | n `rem` 5 == 0 = "Buzz" 1","32","Fizz","34","Buzz","Fiz 7 | n `rem` 3 == 0 = "Fizz" z","37","38","Fizz","Buzz","41 8 | otherwise = show n ","Fizz","43","44","FizzBuzz"," 46","47","Fizz","49","Buzz","F izz","52","53","Fizz","Buzz","5 6","Fizz","58","59","FizzBuzz" ,"61","62","Fizz","64","Buzz"," like p Fizz","67","68","Fizz","Buzz"," 71","Fizz","73","74","FizzBuzz ","76","77","Fizz","79","Buzz", Sunday, November 13, 2011 "Fizz","82","83","Fizz","Buzz",
  • 27. output 1 1 main = mapM_ putStrLn fizzbuzz 2 2 Fizz 4 3 fizzbuzz = map f [1..100] Buzz 4 where f n Fizz 7 5 | n `rem` 15 == 0 = "FizzBuzz" 8 6 | n `rem` 5 == 0 = "Buzz" Fizz Buzz 7 | n `rem` 3 == 0 = "Fizz" 11 Fizz 8 | otherwise = show n 13 14 FizzBuzz 16 17 Fizz 19 like puts Buzz Fizz 22 23 Fizz Sunday, November 13, 2011
  • 28. mapM_ • f x >> f y >> f z • do fx fy fz • mapM_ f [x, y, z] Sunday, November 13, 2011
  • 29. mapM_ • ((f x) >> f y) >> f z • foldl1 (>>) $ map f [x, y, z] • f x >> (f y >> (f z)) • foldr1 (>>) $ map f [x, y, z] • sequence_ (map f [x, y, z]) Sunday, November 13, 2011
  • 30. 1 main = mapM_ putStrLn fizzbuzz 1 main = mapM_ putStrLn fizzbuzz 2 2 3 fizzbuzz = map f [1..100] 3 fizzbuzz = map f [1..100] 4 where f n 4 where f n = case () of 5 | n `rem` 15 == 0 = "FizzBuzz" 5 n `rem` 15 == 0 -> "FizzBuzz" 6 | n `rem` 5 == 0 = "Buzz" 6 n `rem` 5 == 0 -> "Buzz" 7 | n `rem` 3 == 0 = "Fizz" 7 n `rem` 3 == 0 -> "Fizz" 8 | otherwise = show n 8 otherwise -> show n Sunday, November 13, 2011
  • 31. map, flip, and for • map f list • flip map list f • mapM_ f list • flip mapM_ list f • forM_ list f Sunday, November 13, 2011
  • 32. 1 main = mapM_ putStrLn fizzbuzz 2 3 fizzbuzz = map f [1..100] 4 where f n = case () of _ 5 | n `rem` 15 == 0 -> "FizzBuzz" 6 | n `rem` 5 == 0 -> "Buzz" 7 | n `rem` 3 == 0 -> "Fizz" 1 import Control.Monad (forM_) 8 | otherwise -> show n 2 main = forM_ [1..100] f 3 where 4 f n = putStrLn $ case () of _ 5 | n `rem` 15 == 0 -> "FizzBuzz" 6 | n `rem` 5 == 0 -> "Buzz" 7 | n `rem` 3 == 0 -> "Fizz" 8 | otherwise -> show n Sunday, November 13, 2011
  • 33. 1 import Control.Monad (forM_) 2 main = forM_ [1..100] $ n -> 3 putStrLn $ case () of _ 4 | n `rem` 15 == 0 -> "FizzBuzz" 5 | n `rem` 5 == 0 -> "Buzz" 6 | n `rem` 3 == 0 -> "Fizz" 1 import Control.Monad (forM_)7 | otherwise -> show n 2 main = forM_ [1..100] f 3 where 4 f n = putStrLn $ case () of _ 5 | n `rem` 15 == 0 -> "FizzBuzz" 6 | n `rem` 5 == 0 -> "Buzz" 7 | n `rem` 3 == 0 -> "Fizz" 8 | otherwise -> show n Sunday, November 13, 2011
  • 34. Haskell as shell scripts • Succinct expressions • Powerful list-manipulation • Powerful text-manipulation • Reasonably fast to boot • Healthy Sunday, November 13, 2011
  • 35. Parsec • Parser combinators • Standard library • No need for handling state explicitly Sunday, November 13, 2011
  • 36. Haskell web framework • Yesod • Snap (framework) • Sunday, November 13, 2011
  • 37. Appendix • IDE-integration • run on the IDE • keyword completion • Prelude type/function completion • module name completion • third party t/f completion • pragma completion Sunday, November 13, 2011
  • 38. quickrun.vim • DEMO • runghc • ghc Sunday, November 13, 2011