SML 如何减去 2 个列表并比较乘积?
我正在尝试减去 2 个列表并返回比较结果。
所以如果列出 a = [2,3,2] b = [1,1,1] 然后 ab = [1,2,1] 并且返回的乘积 (c) 应为 1。
val c = List.map (fn i => (i - b) mod 10) a
模 (mod) 10 适用于两个数相减得到奇数结果的情况,例如 2-8 = ~6 mod 10 = 4。
我陷入了减法,因为 List.map 不允许我进行减法,因为它需要一个 int 值而不是一个 int 列表(至少不是我编码的方式:( )。
我也是比较空白。
I'm trying to subtract 2 lists and return the compared product.
So if list
a = [2,3,2]
b = [1,1,1]
then
a-b = [1,2,1] and the returned product (c) should be 1.
val c = List.map (fn i => (i - b) mod 10) a
modulo (mod) 10 is for cases where the two subtracted numbers gives an odd result, e.g. 2-8 = ~6 mod 10 = 4.
I'm stuck at the subtraction, because List.map doesn't allow me to do the subtraction because it expects an int value and not an int list (at least not the way I have coded it :( ).
I'm also blank on the comparison.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
ListPair.map
一次迭代 2 个列表,如下所示:You can use
ListPair.map
to iterate over 2 lists at once, like this:您不想减去
b
- 您想要减去相应的值。执行此操作的一种便捷方法是将列表压缩在一起:You don't want to subtract
b
— you want to subtract the corresponding value. A convenient way to do this is to zip the lists together: