Skip to content

Instantly share code, notes, and snippets.

@basile-henry
Created May 16, 2017 16:58
Show Gist options
  • Select an option

  • Save basile-henry/6a2bbfd9800e6d8968e37d39915e29af to your computer and use it in GitHub Desktop.

Select an option

Save basile-henry/6a2bbfd9800e6d8968e37d39915e29af to your computer and use it in GitHub Desktop.
import Data.List
conv :: [Int] -> [Int]
conv = concat . f
where f [] = []
f (x:xs) = map (x+) xs : f xs
inverseConv :: [Int] -> [Int]
inverseConv xs
| d == fromIntegral (round d) = inverseF $ inverseConcat n xs
| otherwise = error "Invalid input length"
where
d = sqrt $ 1 + 8 * genericLength xs
n = (round d - 1) `div` 2
inverseConcat :: Int -> [a] -> [[a]]
inverseConcat 0 _ = [[]]
inverseConcat l xs = let (a, b) = splitAt l xs
in a : inverseConcat (l - 1) b
inverseF :: [[Int]] -> [Int]
inverseF [[a1,a2],[b],[]] =
let x = (a1 + a2 - b) `div` 2
in [x, a1 - x, a2 - x]
inverseF ((a1:a2:_):bs@(b:_):rest) = (a1 + a2 - b) `div` 2 : inverseF (bs:rest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment