Last active
August 29, 2015 14:07
-
-
Save dustingetz/099c242a29181295a724 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Both exercises have a common pattern of "filter by a transformed list, then untransform the result". | |
-- Is there an idiomatic way to do this? | |
-- I am thinking like `filterBy :: (b -> Bool) -> (a -> b) -> [a] -> [b]`, | |
-- but i also need (b->a) and that's no better than what i already have | |
-- exercise 1 | |
skips :: [a] -> [[a]] | |
skips xs = map (\n -> skip n xs) [1..(length xs)] | |
skip :: Integral n => n -> [a] -> [a] | |
skip n xs = map snd $ filter (\x -> (fst x) `mod` n == 0) (zip [1..] xs) | |
--exercise 2 | |
isLocalMaximum :: Integral a => (a,a,a) -> Bool | |
isLocalMaximum (a,b,c) = b > a && b > c | |
sliding3 :: [a] -> [(a,a,a)] | |
sliding3 xs@(a:b:c:_) = (a,b,c) : sliding3 (tail xs) | |
sliding3 _ = [] | |
localMaxima :: Integral a => [a] -> [a] | |
localMaxima xs = map proj2 $ filter isLocalMaximum (sliding3 xs) | |
where proj2 (_,b,_) = b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is what my instincts were leading me towards, but i can't quite make it typecheck, and it is not very general since second parameter must be
f :: [a] -> [b]
rather thanf :: a -> b