Skip to content

Instantly share code, notes, and snippets.

@gabebw
Created July 31, 2015 23:51
Show Gist options
  • Save gabebw/039ca8c2199372b811ba to your computer and use it in GitHub Desktop.
Save gabebw/039ca8c2199372b811ba to your computer and use it in GitHub Desktop.
-- Added type signatures
module PatternMatching where
import Data.Char (isUpper)
-- `:t even` shows that `even` needs `Integral`, but nothing more precise
-- than that
anyEven :: (Integral a) => [a] -> Bool
anyEven list = not $ null $ filter even list
-- Again, `even` only needs Integral
maxEven :: (Integral a) => [a] -> a
maxEven list = maximum $ filter even list
-- neighbors operates on list of a's and returns a tuple of (a, a)
neighbors :: [a] -> [(a, a)]
neighbors (x:y:xs) = (x, y):neighbors (y:xs)
neighbors _ = []
-- `isUpper` operates on Char, and `filter` operates on a list of whatever
-- the filterer (in this case, `isUpper`) operates on.
firstUpper :: String -> Char
firstUpper string = head $ filter isUpper string
-- Need (Eq a) to pattern-match, otherwise how can we tell if
-- the argument == 0?
-- Need (Num a) so we can subtract 1 from a
countTo :: (Eq a, Num a) => a -> [a]
countTo 0 = []
countTo x = countTo (x - 1) ++ [x]
-- The only thing we do with a is use `==`, so all we need is (Eq a)
hasChange :: (Eq a) => (a -> a) -> a -> Bool
hasChange f a = not $ a == f a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment