Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
San Diego Ruby: A Tour of Haskell
Search
xrl
July 05, 2012
Technology
2
310
San Diego Ruby: A Tour of Haskell
A Rubyist's Take on Functional Programming
xrl
July 05, 2012
Tweet
Share
Other Decks in Technology
See All in Technology
UI State設計とテスト方針
rmakiyama
3
800
WACATE2024冬セッション資料(ユーザビリティ)
scarletplover
0
330
エンジニアカフェ忘年会2024「今年やらかしてしまったこと!」
keropiyo
0
100
20241220_S3 tablesの使い方を検証してみた
handy
4
700
レンジャーシステムズ | 会社紹介(採用ピッチ)
rssytems
0
280
【re:Invent 2024 アプデ】 Prompt Routing の紹介
champ
0
160
プロダクト開発を加速させるためのQA文化の築き方 / How to build QA culture to accelerate product development
mii3king
1
290
Qiita埋め込み用スライド
naoki_0531
0
5.3k
12 Days of OpenAIから読み解く、生成AI 2025年のトレンド
shunsukeono_am
0
140
成果を出しながら成長する、アウトプット駆動のキャッチアップ術 / Output-driven catch-up techniques to grow while producing results
aiandrox
0
380
OCI技術資料 : ファイル・ストレージ 概要
ocise
3
11k
サービスでLLMを採用したばっかりに振り回され続けたこの一年のあれやこれや
segavvy
2
550
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
427
64k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
1
110
How to train your dragon (web standard)
notwaldorf
88
5.7k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
170
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
How STYLIGHT went responsive
nonsquared
96
5.2k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Side Projects
sachag
452
42k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Transcript
A Tour of Haskell A Rubyist’s Take on Functional Programming
Xavier Lange
Xavier • Ruby hacker • Detour to Erlang hacking •
Drank the functional Kool-Aid • Went back to Ruby • Tried some Haskell
You can never understand a language until you understand at
least two. – Ron Searle
• You manipulate values not state • Functions are values
• Recursion is idiomatic Well, a language is functional if...
Haskell is a functional language and... • All the pieces
fit together before it runs • “strongly-typed” • Work is only done when you need it • “lazy”, “demand-driven” • Don’t need all the arguments • “currying”
Other features of Haskell • The Glasgow Haskell Compiler •
optimizing compiler • emits standalone executables • or libraries
Other features of Haskell (cont.) • Hackage • Large library
ecosystem • Cabal build system • Standardized • Ubiquitous • Manage dependencies
Ruby http://avatarbriefs.blogspot.com/2012/06/duck-friday_15.html
Haskell http://playducation.blogspot.com/2010/05/wooden-shape-sorters-are-favourite.html
Haskell Makes a Quiet Introduction
#!/usr/bin/env runhaskell main = do putStrLn "hello SDRuby!"
Interpreter #!/usr/bin/env runhaskell main = do putStrLn "hello SDRuby!"
Defines a function #!/usr/bin/env runhaskell main = do putStrLn "hello
SDRuby!"
Please, ignore :) #!/usr/bin/env runhaskell main = do putStrLn "hello
SDRuby!"
A list of chars #!/usr/bin/env runhaskell main = do putStrLn
"hello SDRuby!"
Function Application #!/usr/bin/env runhaskell main = do putStrLn "hello SDRuby!"
Easy, right?
Some Types 3 :: Integer "3" :: String "3" ::
[Char] 1.0 :: Float True :: Bool False :: Bool [1,2,3] :: [Integer]
Learn by Starting Over
data Bool = True | False
data Bool = True | False Type
data Bool = True | False Value Constructor
data Bool = True | False Value Constructor
not :: Bool -> Bool not True = False not
False = True
(&&) :: Bool -> Bool -> Bool False && _
= False True && x = x
Your first container data List a = Nil | Cons
a (List a)
data List a = Nil | Cons a (List a)
cons :: a -> List a -> List a cons x xs = Cons x xs
data List a = Nil | Cons a (List a)
Nil => Nil cons 1 Nil => Cons 1 Nil cons 2 (cons 1 Nil) => Cons 2 (Cons 1 Nil) cons 3 (cons 2 (cons 1 Nil)) => Cons 3 (Cons 2 (Cons 1 Nil))
length :: List a -> Integer
data List a = Nil | Cons a (List a)
length :: List a -> Integer length Nil = 0 length Cons _ rest = 1 + (length rest)
length (Cons 3 (Cons 2 (Cons 1 Nil))) 1 +
(length (Cons 2 (Cons 1 Nil))) 1 + 1 + (length (Cons 1 Nil)) 1 + 1 + 1 + (length Nil) 1 + 1 + 1 + 0 2 + 1 + 0 3 + 0 3
A Note On Laziness • In ruby • Elements of
list must be built first • In Haskell • Elements of list are built when needed
Case in Point zipWith and lazy lists
[1..] :: (Enum t, Num t) => [t]
zipWith :: (a -> b -> c) -> [a] ->
[b] -> [c]
zipWith :: (a -> b -> c) -> [a] ->
[b] -> [c] (+) :: Num a => a -> a -> a zipWith (+) [10,11,12] [1..] [11,13,15] zipWith (,) [10,11,12] [1..] [(10,1),(11,2),(12,3)]
zipWith (+) [10,11,12] [1,1..] [11,12,13] zipWith (,) [10,11,12] [1,1..] [(10,1),(11,1),(12,1)]
OK, Where Does Haskell Really Shine?
Where Does It Sparkle and Boom?
data FireShow = Expl ExplType Dur | Rest Dur --
sequential | FireShow :+: FireShow -- parallel | FireShow :=: FireShow
type Dur = Integer data ExplType = Sparkle | Bang
| Red | White | Blue | SmileyFace
sanDiegoShow :: FireShow sanDiegoShow = (((Expl Sparkle 5) :=: (Expl
Red 10)) :=: ((Expl Bang 7) :=: (Expl Sparkle 4))) :=: (((Rest 10) :=: (Rest 10))) :=: ((Expl Red 10) :=: (Expl White 10))
Did You See $125k Problem? http://www.foxnews.com/us/2012/07/05/san-diego-fireworks-malfunction-in-big-fast-flash/
@benballer, 7/4/2012
data FireShow = Expl ExplType Dur | Rest Dur --
sequential | FireShow :+: FireShow -- parallel | FireShow :=: FireShow duration :: FireShow -> Dur duration (Expl _ dur) = dur duration (Rest dur) = dur duration (lShow :+: rShow) = (duration lShow) + (duration rShow) duration (lShow :=: rShow) = greaterOf (duration lShow) (duration rShow)
-- greaterOf :: Dur -> Dur -> Dur greaterOf lDur
rDur | lDur > rDur = lDur | otherwise = rDur
sanDiegoShow :: FireShow sanDiegoShow = (((Expl Sparkle 5) :=: (Expl
Red 10)) :=: ((Expl Bang 7) :=: (Expl Sparkle 4))) :=: (((Rest 10) :=: (Rest 10))) :=: ((Expl Red 10) :=: (Expl White 10)) duration sanDiegoShow => 10
xaviersShow :: FireShow xaviersShow = (((Expl Sparkle 5) :=: (Expl
Red 10)) :+: ((Expl Bang 7) :=: (Expl Sparkle 4))) :+: (((Rest 10) :=: (Rest 10))) :+: ((Expl Red 10) :=: (Expl White 10)) duration xaviersShow => 37
#BigBayBoom fail averted with Haskell? News at 11.
In Any Language You Model Your Problem Domain
Functional Modeling is Value-Oriented
Recursive Values Are Your Friend
Is laziness worth it? • Instead of doing unused work
your runtime does book-keeping • Now in Ruby 1.9.2+ • Not quite as idiomatic identity = lambda{|x,y| x+y}.curry.(0)
Haskell is a big subject • Takes time to sink
in • Be patient • And remember your goal...
Thanks • Feel free to email me,
[email protected]
• Or
say hi @tureus