# HG changeset patch # User IBBoard # Date 1561907384 -3600 # Node ID 85a5e9a6ef5c8583314ac486ea15bab417bd6e7e # Parent f548c8460d154274ac256c096b277869e737fc6c Add more notes on extracting Just/Maybe values diff -r f548c8460d15 -r 85a5e9a6ef5c 7-Haskell/day3.hs --- a/7-Haskell/day3.hs Sun Jun 30 15:50:33 2019 +0100 +++ b/7-Haskell/day3.hs Sun Jun 30 16:09:44 2019 +0100 @@ -3,6 +3,7 @@ -- * https://www.haskell.org/tutorial/monads.html -- -- Pre-existing monads: https://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Monad.html#g:2 +-- and https://wiki.haskell.org/All_About_Monads#Introduction_2 module Day3 where -- Build a "hash" table (actually a simple lookup in a key-value list, because hashes are hard) @@ -26,6 +27,8 @@ -- Note 3: I can't get anything out of this other than "Just ", even if I include ">>= return" -- Example usage: Just hashtable >>= ht_lookup "Phonetic" >>= ht_lookup "Greek" >>= ht_lookup "alpha" -- This is monadic because "Just hashtable >>= ht_lookup "Phonetic" >>= ht_lookup "foo" >>= ht_lookup "alpha" >>= return" doesn't explode but returns Nothing + -- To unwrap, we need something like "fromMaybe "" (Just hashtable >>= ht_lookup "Phonetic" >>= ht_lookup "Greek" >>= ht_lookup "alpha")" which returns the value or the empty string + -- BUT we need to get our value types to match (and we need to do "import Data.Maybe" first) ht_lookup :: String -> [(String, a)] -> Maybe a ht_lookup _ [] = Nothing ht_lookup search_key ((key, value):tail)