Count the coins: Difference between revisions

→‎{{header|Haskell}}: more efficient method
(/* {{header|Haskell}}: simple method, not for big numbers)
(→‎{{header|Haskell}}: more efficient method)
Line 653:
 
main = print (count 100 [1, 5, 10, 25])</lang>
 
Much faster, probably harder to read, is to update results from bottom up:
<lang haskell>count = foldr addCoin (1:repeat 0)
where addCoin c oldlist = newlist
where newlist = (take c oldlist) ++ zipWith (+) newlist (drop c oldlist)
 
main = do
print (count [25,10,5,1] !! 100)
print (count [100,50,25,10,5,1] !! 100000)</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Anonymous user