, , , ""...
head (h [10]) . : h [10] => h [10,9] => h [10,9,9,8] => h [10,9,9,8,9,8,8,7] => .... , , 10, . , , f 10 => [10,9,8,7,....
,
binarily a [] = a
binarily a (b:bs) = binarily (a ++ map (b+) a) bs
{- try it out with [b1,b2,b3,b4] :
a b1 the arguments received;
a1@(a ++ (map (b1+) a)) b2 if we've already produced a, we just need
to produce (map (b1+) a) next
a2@(a1 ++ (map (b2+) a1)) b3 if we've already produced a1, we just need
to produce (map (b2+) a1) next
a3@(a2 ++ (map (b3+) a2)) b4 ai@... name the interim values
a4@(a3 ++ (map (b4+) a3)) [] a4 is returned -}
import Data.List (mapAccumL)
-- mapAccumL :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
binarily a bs = (last . snd) (mapAccumL g a bs)
where
g a b = let anew = a ++ map (b+) a
in (anew, anew) -- (next_accum, part_result)
mapAccumL . :: [y] ( snd ) , y, , x :: [x] ( (b:bs)). , fst , .
, :
binarily a bs = a ++ (concat . snd) (mapAccumL g a bs)
where
g a b = let -- for each b in bs:
res = map (b+) a -- this part of the result
anew = a ++ res -- next accumulator value
in (anew, res)