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
Data and AI Governance: Existing Challenges and Emerging Trends
scotthsieh825
0
160
Databricks における 『MLOps』
databricksjapan
2
140
DevOpsメトリクスとアウトカムの接続にトライ!開発プロセスを通して計測できるメトリクスの活用方法
ham0215
2
200
Cloud Native Java with Spring Boot (CNCF Aarhus, April 2024)
thomasvitale
1
130
クラウドサインにおけるプロダクトマネージャーの役割と開発プロセス / 20240410_cloudsign-PdM
bengo4com
1
680
4年前、あるじゃん老害エンジニアLT合戦に登壇、米国西海岸コンピュータ歴史博物館体験記の続編
toshi_atsumi
0
200
検証を通して見えてきたTiDBの性能特性
lycorptech_jp
PRO
6
3.4k
Tableau事例紹介 / Tableau Case Study of Eureka
kazuya_araki_tokyo
1
170
Databricks におけるデータエンジニアリング
databricksjapan
0
380
複雑な構成要素を持つUIとの向き合い方 〜新・支出グラフでの実例〜 / B43 TECH TALK
nakamuuu
0
110
テストプロセスで大事にしていること #jasstnano
makky_tyuyan
0
130
ここが嬉しいABAC ここが辛いよABAC #再解説+補足編
masahirokawahara
1
230
Featured
See All Featured
The Pragmatic Product Professional
lauravandoore
24
5.8k
Stop Working from a Prison Cell
hatefulcrawdad
266
19k
A Tale of Four Properties
chriscoyier
150
22k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
240
1.2M
Building Applications with DynamoDB
mza
88
5.6k
Fireside Chat
paigeccino
20
2.6k
The Illustrated Children's Guide to Kubernetes
chrisshort
29
46k
What's in a price? How to price your products and services
michaelherold
237
11k
Building a Modern Day E-commerce SEO Strategy
aleyda
16
6.4k
The MySQL Ecosystem @ GitHub 2015
samlambert
242
12k
The Cost Of JavaScript in 2023
addyosmani
14
3.8k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
119
38k
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