[Solved] adding elements of a list in a given way in haskell [closed]


I think you’re looking for the inverse of this function

conv = concat . f
    where f [] = []
          f (x:xs) = map (x+) xs : f xs

> conv [3,10,5]
[13,8,15]

> conv [2,5,15,34]
[7,17,36,20,39,49]

I came up something like this. Note that for length less than 3 there can’t be a unique solution, handle that case…

solve x = (reverse . (h :) . map (subtract h) . map head . split') rx
        where rx@(a:b:c:_) = reverse x
              h = (a+b-c) `div` 2

> solve [7,17,36,20,39,49]
[2,5,15,34]

> solve [13,8,15]
[3,10,5]

you can check whether

conv . solve = solve . conv = id

split’ scrolled up on my screen, it’s defined as

split' = go 1
     where go _ [] = []
           go n x = take n x : go (n+1) (drop n x)

8

solved adding elements of a list in a given way in haskell [closed]