Last active
August 29, 2015 14:19
-
-
Save paolino/827d3e14a11fb20671d6 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
{-# LANGUAGE ViewPatterns #-} | |
-- problem: given a list of numbers and a counter in a structure C [Int] Int | |
-- write a function step :: Int -> C -> C which | |
-- change first occurrence of a multiple of a given number in a list in its successive | |
-- and subtract it to the counter | |
-- or | |
-- subtract 1 to the counter and add 1 to all numbers in the list. | |
-- step 3 (C [1,2,3,5,6] 7) == C [1,2,4,5,6] 4 | |
-- step 3 (C [1,2,4,5,7] 16) == C [2,3,5,6,8] 15 | |
import Control.Arrow (second) | |
data C = C [Int] Int deriving Eq | |
select :: (a -> Bool) -> [a] -> Maybe (a,a -> [a]) | |
select f [] = Nothing | |
select f (x:xs) | |
| f x = Just (x,(:xs)) | |
| otherwise = fmap (second $ fmap (x:)) $ select f xs | |
step :: Int -> C -> C | |
step n (C (select ((==0). (`mod` n)) -> Just (x,g)) y) = C (g $ succ x) (y - x) | |
step _ (C xs y) = C (map succ xs) $ pred y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment