Skip to content

Instantly share code, notes, and snippets.

@johnlonganecker
Created January 28, 2020 02:09
Show Gist options
  • Save johnlonganecker/6101d17d1396b734d9334b6836df5c80 to your computer and use it in GitHub Desktop.
Save johnlonganecker/6101d17d1396b734d9334b6836df5c80 to your computer and use it in GitHub Desktop.
-- taken from https://www.youtube.com/watch?v=fg24wrRKE8g
import Prelude hiding (Maybe(..), lookup)
data Maybe a = Just a | Nothing
deriving Show
favoriteFoods :: [(String, String)]
favoriteFoods = [("David", "Green Papaya Salad")
,("Becky", "Sorbet")]
lookup key [] = Nothing
lookup key ((k, v) : rest) = if key == k then Just v else lookup key rest
fromMaybe def (Just a) = a
fromMaybe def Nothing = def
main = do
let favorite = lookup "Johan" favoriteFoods
print (fromMaybe "Ice Cream" favorite)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment