Skip to content

Instantly share code, notes, and snippets.

@JossWhittle
Created August 10, 2012 17:37
Show Gist options
  • Save JossWhittle/3316002 to your computer and use it in GitHub Desktop.
Save JossWhittle/3316002 to your computer and use it in GitHub Desktop.
-- Dictionary to search
type Dict = [(String, String)]
dict :: Dict
dict = [("bread", "pain"),("milk", "lait"),("hello", "bonjour")]
-- Filter to apply to Dictionary
food :: [String]
food = ["bread", "milk"]
-- Where:
foodOnly :: Dict -> Dict
foodOnly d = filter test d where
test :: (String,String) -> Bool
test x = elem (fst x) food
-- Recursion:
foodOnly :: Dict -> Dict
foodOnly [] = []
foodOnly (x:xs) = if elem (fst x) food
then x : (foodOnly xs)
else (foodOnly xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment