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
12 Days of OpenAIから読み解く、生成AI 2025年のトレンド
shunsukeono_am
0
1.1k
Bring Your Own Container: When Containers Turn the Key to EDR Bypass/byoc-avtokyo2024
tkmru
0
560
Oracle Base Database Service:サービス概要のご紹介
oracle4engineer
PRO
1
16k
スケールし続ける事業とサービスを支える組織とアーキテクチャの生き残り戦略 / The survival strategy for Money Forward’s engineering.
moneyforward
0
240
The key to VCP-VCF
mirie_sd
0
160
最近のSfM手法まとめ - COLMAP / GLOMAPを中心に -
kwchrk
8
1.8k
30分でわかるデータ分析者のためのディメンショナルモデリング #datatechjp / 20250120
kazaneya
PRO
17
4.1k
#TRG24 / David Cuartielles / Post Open Source
tarugoconf
0
430
デジタルアイデンティティ技術 認可・ID連携・認証 応用 / 20250114-OIDF-J-EduWG-TechSWG
oidfj
2
340
知っててうれしい HTTP Cookie を使ったセッション管理について
greendrop
1
110
The future we create with our own MVV
matsukurou
0
1.5k
20240513 - 框裡框外_文學院學生如何在AI世代安身立命 @ 淡江大學
dpys
0
630
Featured
See All Featured
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
19
2.3k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
2
160
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Typedesign – Prime Four
hannesfritz
40
2.5k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.2k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
940
Producing Creativity
orderedlist
PRO
343
39k
Automating Front-end Workflow
addyosmani
1366
200k
A Tale of Four Properties
chriscoyier
157
23k
Making the Leap to Tech Lead
cromwellryan
133
9k
GitHub's CSS Performance
jonrohan
1030
460k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.8k
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