Write a function that doubles other number beginning with the 2nd number from the right:
Example:
doubleEveryOther [8,7,6,5]
=> [16,7,12,5]
doubleEveryOther [1,2,3]
=> [1,4,3]
O(n) solution:
doubleEveryOther :: Num a => [a] -> [a]
doubleEveryOther xs0 =
let (_,r) = deo xs0
deo xs1 = case xs1 of
[] -> (False, [])
(x:xs) -> let (b, xs') = deo xs in ((not b), (if b then 2*x else x) : xs')
in r
The use above of explicit recursion is generally considered poor Haskell style (e.g., use fold*, scan, etc where possible).
QUESTIONS
what Haskell library functions cover the above case?
what would be a more concise/idiomatic Haskell solution that is still O(n)?
is there a name for the above type of recursion (where we use the value from a deeper recursion to make a decision the next level up)?