gpt4 book ai didi

haskell - 关联和交换之间的区别

转载 作者:行者123 更新时间:2023-12-02 02:30:49 24 4
gpt4 key购买 nike

我试图理解幺半群中的关联。

从书中,它说:

Associativity simply says that you can associate the arguments of your operation differently and the result will be the same.



例如:
Prelude> (1 + 9001) + 9001
18003
Prelude> 1 + (9001 + 9001)
18003

关于交换:

This is not as strong a property as an operation that commutes or is commutative. Commutative means you can reorder the arguments and still get the same result. Addition and multiplication are commutative, but (++) for the list type is only associative.



上面的例子是关联的和可交换的,但有什么区别呢?
我看不出有什么区别。

最佳答案

以字符串连接为例。假设您使用的语言使用 +用于字符串连接。这自然是关联的,因为分组无关紧要:

("a" + "b") + "c" == "abc"
"a" + ("b" + "c") == "abc"

但操作数顺序绝对重要:
"a" + "b" = "ab"
"b" + "a" = "ba"

关于haskell - 关联和交换之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44759470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com