Skip to content

Instantly share code, notes, and snippets.

@tafsiri
Created July 5, 2012 22:24
Show Gist options
  • Save tafsiri/3056872 to your computer and use it in GitHub Desktop.
Save tafsiri/3056872 to your computer and use it in GitHub Desktop.
module Main where
-- Q1. Write a function that looks up a hash table value that uses the Maybe Monad. (IMHO this
-- question is kinda sloppily written, does the hash table use the maybe monad or the function?).
-- Write a hash that stores other hashes, several levels deep. Use the Maybe monad to retrieve
-- an element several levels deep. (wtf does this even mean? is the maybe monad going to do the
-- retrieving for me?)
-- What I am going to attempt to do (my interpretation of the above):
-- 1. Write a function that looks up a value by key (in a collection of key value pairs)
-- and returns a result wrapped in the Maybe monad.
-- 2. Write a function that can look for a value in a list of (posibly) nested key-value pair collections
-- returning results wrapped in a maybe
-- Note: I do know that haskell has a Data.Map type, i am just assuming we are not supposed to use
-- it for pedagogical reasons.
hash = [('a', "apple"), ('b', "bear"), ('c', "cat"), ('d', "dog")]
find :: Eq a => [(a, b)] -> a -> Maybe b
find [] _ = Nothing
find tuples key = let
(nextKey, nextVal) = head tuples
rest = tail tuples
in
if key == nextKey then Just nextVal else find rest key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment