SML 如何减去 2 个列表并比较乘积?

发布于 2024-09-24 03:19:00 字数 332 浏览 0 评论 0原文

我正在尝试减去 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攒眉千度 2024-10-01 03:19:00

您可以使用 ListPair.map 一次迭代 2 个列表,如下所示:

val c = ListPair.map (fn (i, j) => (i - j) mod 10) (a,b)

You can use ListPair.map to iterate over 2 lists at once, like this:

val c = ListPair.map (fn (i, j) => (i - j) mod 10) (a,b)
哥,最终变帅啦 2024-10-01 03:19:00

您不想减去b - 您想要减去相应的值。执行此操作的一种便捷方法是将列表压缩在一起:

val c = List.map (fn (i, j) => (i - j) mod 10) (ListPair.zip (a, 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:

val c = List.map (fn (i, j) => (i - j) mod 10) (ListPair.zip (a, b))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文