我想编写一个函数,在给定一定数量的硬币时计算变化。除了当我真的希望输入是Double而不是Int时,它工作得很好。将“Double”转换为Int
这是我的代码
coins::[Int]
coins = [200, 100, 50, 20, 10, 5, 2, 1]
change::Int->[Int]->[Int]
change n [] = []
change n coins
| n == 0 = []
| n >= firstcoin = firstcoin:change (n-firstcoin) coins
| otherwise = change n (tail coins)
where firstcoin = head coins
这工作不错,一切,但是当我尝试改变代码:
change::Double->[Int]->[Int]
| (n*100) == 0 = []
| (n*100) >= firstcoin = firstcoin:change (n-firstcoin) coins
| otherwise = change n (tail coins)
where firstcoin = head coins
会出现以下情况:
[1 of 1] Compiling Main (coin.hs, interpreted)
coin.hs:7:27:
Couldn't match expected type ‘Double’ with actual type ‘Int’
In the second argument of ‘(>=)’, namely ‘firstcoin’
In the expression: (n * 100) >= firstcoin
coin.hs:7:59:
Couldn't match expected type ‘Double’ with actual type ‘Int’
In the second argument of ‘(-)’, namely ‘firstcoin’
In the first argument of ‘change’, namely ‘(n - firstcoin)’
Failed, modules loaded: none.
这是像使用“/”我必须事先从整体使用?如果是这样,这是怎么翻译的?
* A副作用小的问题:如何才能让这里写的“嵌入式”硬币列表进入功能,使签名看起来像:
change::Int->[Int]
(换句话说,我不想要明确地写在列表中,以使其工作。我是否需要更改我的整个代码?)
处理货币的第一条规则之一是不使用浮点值来表示货币量。 – chepner
请您详细说明一下吗?我不明白。 – Rad
浮点值只是实数的近似值。你不会为一美元的模糊部分做出改变;你修改一个确切的整数美分。 – chepner