Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 7-Haskell/day3.hs @ 96:f548c8460d15
Add extra note about monadic-ness
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 30 Jun 2019 15:50:33 +0100 |
parents | 83b4f23b9df8 |
children | 85a5e9a6ef5c |
rev | line source |
---|---|
95
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 -- Monad tutorials: |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 -- * https://wiki.haskell.org/All_About_Monads |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 -- * https://www.haskell.org/tutorial/monads.html |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 -- |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 -- Pre-existing monads: https://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Monad.html#g:2 |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 module Day3 where |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 -- Build a "hash" table (actually a simple lookup in a key-value list, because hashes are hard) |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 hashtable = [ |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 ("Phonetic", [ |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 ("Latin", [ |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 ("alpha", "a"), |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 ("beta", "b"), |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 ("charlie", "c") |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 ]), |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 ("Greek", [ |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 ("alpha", "α"), |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 ("beta", "β"), |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 ("gamma", "γ") |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 ]) |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 ]) |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 ] |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
24 -- Note: We've got to put the key first so that we can call it in-fix and supply the search key |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
25 -- Note 2: The input value has to be a "Just x" otherwise we can "Couldn't match type 'Maybe' with []" |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
26 -- Note 3: I can't get anything out of this other than "Just <val>", even if I include ">>= return" |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 -- Example usage: Just hashtable >>= ht_lookup "Phonetic" >>= ht_lookup "Greek" >>= ht_lookup "alpha" |
96
f548c8460d15
Add extra note about monadic-ness
IBBoard <dev@ibboard.co.uk>
parents:
95
diff
changeset
|
28 -- This is monadic because "Just hashtable >>= ht_lookup "Phonetic" >>= ht_lookup "foo" >>= ht_lookup "alpha" >>= return" doesn't explode but returns Nothing |
95
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 ht_lookup :: String -> [(String, a)] -> Maybe a |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 ht_lookup _ [] = Nothing |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 ht_lookup search_key ((key, value):tail) |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
32 | search_key == key = Just value |
83b4f23b9df8
First successful attempt at a Haskell monad for hashtable lookup
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
33 | otherwise = ht_lookup search_key tail |