-
Notifications
You must be signed in to change notification settings - Fork 2
/
haskell.html
427 lines (317 loc) · 11.1 KB
/
haskell.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Haskell, Haskell, Haskell</title>
<meta name="generator" content="Slide Show (S9) v2.5.0 on Ruby 2.1.5 (2014-11-13) [i386-mingw32]">
<meta name="author" content="Your Name Here" >
<!-- helper/macro that lets you add (CSS3) gradient using headers
see http://slideshow.rubyforge.org/themes.html
-->
<!-- S6 style sheet links -->
<link rel="stylesheet" href="haskell.css" media="projection" id="styleProjection">
<link rel="stylesheet" href="s6/screen.css" media="screen" id="styleScreen">
<link rel="stylesheet" href="s6/print.css" media="print">
<!-- S6 JS -->
<script src="s6/jquery.js"></script>
<script src="s6/jquery.slideshow.js"></script>
<script>
$(document).ready( function() {
Slideshow.init();
} );
</script>
<!-- Better Browser Banner for Microsoft Internet Explorer (IE) -->
<!--[if IE]>
<script src="s6/jquery.microsoft.js"></script>
<![endif]-->
</head>
<body>
<div class="layout">
<div id="header"></div>
<div id="footer">
<h1>Your Footer Here</h1>
<h2>Your Subfooter Here</h2>
</div>
</div>
<div class="presentation">
<div class='slide '>
<!-- === begin markdown block ===
generated by markdown/1.2.0 on Ruby 2.1.5 (2014-11-13) [i386-mingw32]
on 2015-03-12 17:52:29 +0100 with Markdown engine kramdown (1.5.0)
using options {}
-->
<!-- _S9SLIDE_ -->
<h1 id="sqlite-database-access">SQLite Database Access</h1>
<ul>
<li>HSQL</li>
<li>Persistent</li>
</ul>
<h2 id="hsql-example">HSQL Example</h2>
<pre><code>λ> :module Database.HDBC Database.HDBC.Sqlite3
λ> conn <- connectSqlite3 "test.db"
Loading package HDBC-1.1.5 ... linking ... done.
Loading package HDBC-sqlite3-1.1.4.0 ... linking ... done.
λ> run conn "CREATE TABLE test (id INTEGER NOT NULL, desc VARCHAR(80))" []
0
λ> run conn "INSERT INTO test (id) VALUES (0)" []
1
λ> commit conn
λ> disconnect conn
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="persistent-entities-schema-and-migrations">Persistent Entities (Schema and Migrations)</h1>
<pre><code>import Database.Persist.TH
mkPersist sqlSettings [persistLowerCase|
Country
name String
deriving Show
Client
firstName String
lastName String
address String
country CountryId
age Int
deriving Show
...
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="persistent-example">Persistent Example</h1>
<pre><code>import Database.Persist.Sqlite
conn = runSqlite "test.db" $ do
austria <- insert $ Country "Austria"
_client1 <- insert $ Client "Anton" "Dreher" "Brauplatz 1" austria 25
return ()
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="persistent-queries">Persistent Queries</h1>
<pre><code>getAdultsOfSpainAndUS = do -- returns [Entity Client]
Just (Entity spId _) <- getBy $ UniqueCountryName "Spain"
Just (Entity usId _) <- getBy $ UniqueCountryName "United States of America"
selectList ( [ ClientCountry ==. spId, ClientAge >=. Just 18 ]
||. [ ClientCountry ==. usId, ClientAge >=. Just 21 ] )
[]
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="sql-queries-with-esqueleto">SQL Queries with Esqueleto</h1>
<pre><code>getPeopleOver25FromSpainOrGermany = -- returns [Entity Client]
select $
from $ \(client, country) -> do
where_ ( client ^. ClientAge >. just (val 25)
&&. country ^. CountryName `in_` valList [ "Spain", "Germany" ]
&&. client ^. ClientCountry ==. country ^. CountryId )
orderBy [ asc (client ^. ClientLastName), asc (client ^. ClientFirstName) ]
return client
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="sql-queries-with-esqueleto-join">SQL Queries with Esqueleto (Join)</h1>
<pre><code>getPeopleOver25FromSpainOrGermanyJoin = -- returns [Entity Client]
select $
from $ \(client `InnerJoin` country) -> do
on (client ^. ClientCountry ==. country ^. CountryId)
where_ ( client ^. ClientAge >. just (val 25)
&&. country ^. CountryName `in_` valList [ "Spain", "Germany" ])
orderBy [ asc (client ^. ClientLastName), asc (client ^. ClientFirstName) ]
return client
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="web-frameworks--libraries">Web Frameworks / Libraries</h1>
<ul>
<li>Yesod (“Full stack”)</li>
<li>Happstack</li>
<li>Snap</li>
<li>Scotty (Minimal; Sinatra-Inspired)</li>
<li>Spock</li>
<li>and many more</li>
</ul>
<h2 id="scotty-example">Scotty Example</h2>
<pre><code>import Web.Scotty
import Data.Monoid (mconcat)
main = scotty 3000 $ do
get "/:word" $ do
beam <- param "word"
html $ mconcat ["<h1>Scotty, ", beam, " me up!</h1>"]
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="scotty-example---url-shortener">Scotty Example - URL Shortener</h1>
<pre><code>main :: IO ()
main = scotty 3000 $ do
middleware logStdoutDev
middleware static
m <- liftIO $ newMVar (0::Int,M.empty :: M.Map Int T.Text)
get "/" $ do
html $ renderHtml
$ H.html $ do
H.body $ do
H.form H.! method "post" H.! action "/shorten" $ do
H.input H.! type_ "text" H.! name "url"
H.input H.! type_ "submit"
post "/shorten" $ do
url <- param "url"
liftIO $ modifyMVar_ m $ \(i,db) -> return (i+1, M.insert i (T.pack url) db)
redirect "/list"
-- We have to be careful here, because this route can match pretty much anything.
-- Thankfully, the type system knows that 'hash' must be an Int, so this route
-- only matches if 'read' can successfully parse the hash capture as an Int.
-- Otherwise, the pattern match will fail and Scotty will continue matching
-- subsequent routes.
get "/:hash" $ do
hash <- param "hash"
(_,db) <- liftIO $ readMVar m
case M.lookup hash db of
Nothing -> raise $ mconcat ["URL hash #", T.pack $ show $ hash, " not found in database!"]
Just url -> redirect url
-- We put /list down here to show that it will not match the '/:hash' route above.
get "/list" $ do
(_,db) <- liftIO $ readMVar m
json $ M.toList db
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="why-haskell">Why Haskell?</h1>
<p>Learn something new - state-of-the-art in language design;
many new concepts (compared to classics e.g. C, Perl, Ruby, PHP; etc.):</p>
<ul>
<li>Great for learing a “pure” “state-of-the-art” functional programming (FP) language</li>
<li>Great for learning the “state-of-the-art” for a strongly-typed language
<ul>
<li>Type classes, type families (Typeclassopedia)</li>
<li>Algebraic Data types (ADT)</li>
<li>Category theory (Yoneda Lemma, Kleisli Fish, etc.)</li>
<li>etc.</li>
</ul>
</li>
<li>Many new concepts
<ul>
<li>Programming without Null/Nil - Maybe, Just, Nothing, ()</li>
<li>“Pure” Functions without Side-Effects and I/O Functions with Side-Effects</li>
<li>Monads, Monoids, Applicatives, Functors, Prisms, Lenses, etc.</li>
</ul>
</li>
</ul>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="beautiful-minimalistic-syntax">Beautiful “Minimalistic” Syntax</h1>
<pre><code>a b c d
</code></pre>
<p>Trivia Quiz - Only one interpretation possible in Haskell</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="beautiful-minimalistic-syntax-1">Beautiful “Minimalistic” Syntax</h1>
<p>Function a with three parameters, that is, b, c and d.</p>
<pre><code>a b c d f . g x y f g( x y )
a( b, c, d ) f( g(x,y)) -- Classic-style
(a b c d) (f (g x y)) -- Lisp-style
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="language-example---inspired-by-haskell----elm">Language Example - Inspired by Haskell - Elm</h1>
<h2 id="elm">Elm</h2>
<p>Why: Runs in your Browser – (Yet Another) Alternative Generate-to-JavaScript Language</p>
<h3 id="who">Who?</h3>
<p>Sponsored by Prezi (Budapest, Hungary); Headed by Evan Czaplicki</p>
<p>Example:</p>
<pre><code>profile : User -> Html
profile user =
div [] [
img [ src user.picture ] [],
text user.name
font : List CssProperty
font = [
prop "font-family" "futura, sans-serif",
prop "color" "rgb(42, 42, 42)",
prop "font-size" "2em"
]
background : List CssProperty
background = [
prop "background-color" "rgb(245, 245, 245)"
]
profiles : List User -> Html
profiles users =
div [ style (font ++ background) ] (map profile users)
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="language-example---inspired-by-haskell----idris">Language Example - Inspired by Haskell - Idris</h1>
<h2 id="idris">Idris</h2>
<p>Why: Faster, Easier – Dependent Datatypes, Laziness Optional, and More</p>
<p>A standard example of a dependent type is the type of “lists with length”,
conventionally called vectors in the dependent type literature.</p>
<p>Example:</p>
<pre><code>data Vect : Nat -> Type -> Type where
Nil : Vect Z a
(::) : a -> Vect k a -> Vect (S k) a
(++) : Vect n a -> Vect m a -> Vect (n + m) a
(++) Nil ys = ys
(++) (x :: xs) ys = x :: xs ++ xs -- broken
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="other-languages-inspired-by-haskell">Other languages inspired by Haskell</h1>
<ul>
<li>F# .NET (Microsoft)</li>
<li>Swift (Apple)</li>
<li>Hack (Facebook)</li>
<li>Many more</li>
</ul>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="haskell-programs---haskell-goodies---haskell-in-the-real-world">Haskell Programs - Haskell Goodies - Haskell in the Real World</h1>
<p>Pandoc - “Universal” Document Generator; Headed by John MacFarlane (University of Berkeley)</p>
<p>Example: Generate a book (EPUB) using Markdown</p>
<p>mybook.txt:</p>
<pre><code>Title: My Book
Author: Ruby Rubacuori
This is my book.
# Chapter One
Chapter one is over.
# Chapter Two
Chapter two has just begun.
</code></pre>
<p>To build your book issue:</p>
<pre><code>$ pandoc mybook.txt -o mybook.epub
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="learning-haskell">Learning Haskell</h1>
<h2 id="books">Books</h2>
<ul>
<li>Learn You a Haskell for Great Good</li>
<li>Real World Haskell</li>
<li>Developing Web Applications with Haskell and Yesod, 2nd Edition</li>
<li>Beginning Haskell</li>
<li>Seven Languages in Seven Weeks (incl. Haskell)</li>
<li>Seven More Languages in Seven Weeks (incl. Elm and Idris)</li>
</ul>
<h2 id="articles">Articles</h2>
<ul>
<li>How I Start Series - Haskell - by Chris Allen</li>
<li>School of Haskell - Basics of Haskell Series - by Bartosz Milewski (FP Complete)</li>
<li>What I Wish I Knew When Learning Haskell - by Stephen Diehl</li>
</ul>
<!-- === end markdown block === -->
</div>
</div><!-- presentation -->
</body>
</html>