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
280
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
チームでロジカルシンキングに改めて向き合っている話 〜学習環境と実践⽅法〜
sansantech
PRO
3
3.2k
【基本】データベース設計
oracle4engineer
PRO
2
130
Microsoft Intune 勉強会 第 2 回目
tamaiyutaro
2
370
[新卒向け研修資料] テスト文字列に「うんこ」と入れるな(2024年版)
infiniteloop_inc
4
18k
JSON攻略法.pdf
miyakemito
8
5.2k
Microsoft for Startups Founders Hub_20240429 update
daikikanemitsu
1
2.4k
地理空間データ可視化・解析・活用ソリューション Pacific Spatial Solutions (PSS)
pacificspatialsolutions
0
330
データベース02: データベースの概念
trycycle
0
180
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
4
37k
MLOpsの「壁」を乗り越える、LINEヤフーの Data Quality as Code
lycorptech_jp
PRO
8
610
Cloud Service Mesh に触れ合う
phaya72
1
150
いいたいことちゃんという
tkengo
0
230
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
13
8.3k
Bash Introduction
62gerente
605
210k
Git: the NoSQL Database
bkeepers
PRO
423
63k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
275
13k
A designer walks into a library…
pauljervisheath
201
23k
Rebuilding a faster, lazier Slack
samanthasiow
74
8.2k
Automating Front-end Workflow
addyosmani
1357
200k
The Cost Of JavaScript in 2023
addyosmani
20
3.9k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
242
1.2M
The Illustrated Children's Guide to Kubernetes
chrisshort
32
46k
Designing with Data
zakiwarfel
96
4.8k
The Straight Up "How To Draw Better" Workshop
denniskardys
228
130k
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