gpt4 book ai didi

haskell - 为什么产品 [] 返回 1?

转载 作者:行者123 更新时间:2023-12-02 01:58:31 25 4
gpt4 key购买 nike

为什么函数product在 Haskell 中返回 1如果给它一个空列表?

最佳答案

列表形成一个幺半群结构,具有关联二元运算++和中性元素[] .也就是说,我们有

[] ++ xs = xs = xs ++ []    (xs ++ ys) ++ zs = xs ++ (ys ++ zs)

同时,数字有很多幺半群结构,但这里相关的是运算是 *。中性元素是 1 .
1 * x = x = x * 1           (x * y) * z = x * (y * z)
product函数不仅仅是从数字列表到数字的映射:它是一个幺半群同态,反射(reflect)了数字幺半群中的列表幺半群结构。至关重要的是,
product (xs ++ ys) = product xs * product ys


product [] = 1

事实上,为了获得前者,我们几乎把后者强加给了我们。

关于haskell - 为什么产品 [] 返回 1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33732513/

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