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
業務ツールをAIエージェントとつなぐ - Composio
knishioka
0
110
2025/1/29 BigData-JAWS 勉強会 #28 (re:Invent 2024 re:Cap)/new-feature-preview-q-in-quicksight-scenarios-tried-and-tested
emiki
0
310
インフラコストとセキュリティ課題解決のためのリアーキテクチャリング / srekaigi2025
hgsgtk
3
4.2k
横断SREの立ち上げと、AWSセキュリティへの取り組みの軌跡
rvirus0817
3
4.5k
Zenn のウラガワ ~エンジニアのアウトプットを支える環境で Google Cloud が採用されているワケ~ #burikaigi #burikaigi_h
kongmingstrap
18
6.8k
日本語プログラミングとSpring Bootアプリケーション開発 #kanjava
yusuke
2
340
エラーバジェット枯渇の原因 - 偽陽性との戦い -
phaya72
1
100
ココナラのセキュリティ組織の体制・役割・今後目指す世界
coconala_engineer
0
220
オーティファイ会社紹介資料 / Autify Company Deck
autifyhq
10
120k
Platform EngineeringがあればSREはいらない!? 新時代のSREに求められる役割とは
mshibuya
2
3.9k
Fin-JAWS第38回reInvent2024_全金融系セッションをライトにまとめてみた
mhrtech
1
100
Grid表示のレイアウトで Flow layoutsを使う
cffyoha
1
150
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
40
2.5k
Navigating Team Friction
lara
183
15k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.3k
Building an army of robots
kneath
302
45k
Become a Pro
speakerdeck
PRO
26
5.1k
A Tale of Four Properties
chriscoyier
157
23k
4 Signs Your Business is Dying
shpigford
182
22k
Building Your Own Lightsaber
phodgson
104
6.2k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
GraphQLとの向き合い方2022年版
quramy
44
13k
Agile that works and the tools we love
rasmusluckow
328
21k
KATA
mclloyd
29
14k
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