list comprehension не удалось пока перевести. Чяднт?
coins = [2, 3, 7]
change' :: Int -> [[Int]] -> [[Int]]
change' 0 acc = acc
change' amount acc | amount < 0 = []
| amount > 0 = concatMap fn coins
where
fn :: Int -> [[Int]]
fn c = change' (amount - c) (newAcc c)
newAcc c = if length acc > 0
then map (\w -> w ++ [c]) acc
else [[c]]
fn amount c | amount `elem` coins = [[c]]
| amount > 0 = change (amount - c)
| otherwise = []
change amount | amount >= 0 = [w ++ [c] | c <- coins, w <- fn amount c, c <= amount]
| otherwise = []
change :: Int -> [Int] -> [[Int]] change n _ | n < 0 = [] change n coins | n elem coins = [[n]] change n coins | otherwise = [ c:cs | c <- coins, cs <- change (n - c) coins]
Обсуждают сегодня