【发布时间】:2018-09-19 18:09:35
【问题描述】:
当对函数积使用递归的Foldr模式时,我们得到:
product [] = 1
product (x:xs) = x * product xs
我的问题是,“product [] = 1”是什么意思?例如,对于 sum 函数,我们有 sum[] = 0?这是对答案的某种限制吗?
提前谢谢你。
【问题讨论】:
-
你了解“foldr递归模式”吗?
-
“无积”等于 1 是通常的数学约定。
-
空值列表的乘积是什么?
-
谢谢斯蒂芬,我完全错过了其中的逻辑!
-
一般来说,
foldLikeThing [] = mempty和foldLikeThing (x:xs) = x `mappend` foldLikeThing xs。 (参照foldr对foldMap的定义。)