Skip to content

Instantly share code, notes, and snippets.

@acamino
Last active December 29, 2015 11:14
Show Gist options
  • Save acamino/ba3cd6bf86a86d347069 to your computer and use it in GitHub Desktop.
Save acamino/ba3cd6bf86a86d347069 to your computer and use it in GitHub Desktop.
CIS 194 Week 4
-- Prelude functions which are defined in terms of foldr
-- http://www.cis.upenn.edu/~cis194/spring13/lectures/04-higher-order.html
length' :: [a] -> Int
length' = foldr (\_ acc -> 1 + acc) 0
sum' :: Num a => [a] -> a
sum' = foldr (*) 1
product' :: Num a => [a] -> a
product' = foldr (*) 1
and' :: [Bool] -> Bool
and' = foldr (\e acc -> e && acc) True
or' :: [Bool] -> Bool
or' = foldr (\e acc -> e || acc) False
any :: (a -> Bool) -> [a] -> Bool
any' p = foldr(\e acc -> p e || acc) False
all' :: (a -> Bool) -> [a] -> Bool
all' p = foldr(\e acc -> p e && acc) True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment