Skip to content

Instantly share code, notes, and snippets.

@paolino
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paolino/827d3e14a11fb20671d6 to your computer and use it in GitHub Desktop.
Save paolino/827d3e14a11fb20671d6 to your computer and use it in GitHub Desktop.
{-# 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