gpt4 book ai didi

Haskell 非详尽模式缺失

转载 作者:行者123 更新时间:2023-12-02 13:39:04 25 4
gpt4 key购买 nike

我收到以下错误,我假设所有递归情况,缺少什么?

Prelude> product [] =1
Prelude> product (x:xs) =x * product xs
Prelude> product [1,2,3]
*** Exception: <interactive>:48:1-30: Non-exhaustive patterns in function product

最佳答案

GHCi 单独处理每一行,因此您定义了一个函数

product [] = 1

然后通过定义新函数来隐藏产品

product (x:xs) = x * product xs

要解决此问题,您可以将 :{:} 用于多行 block :

:{
product [] = 1
product (x:xs) = x * product xs
:}

或者,这是我建议的,将函数定义放入文件中并将其加载到 GHCi 中。

关于Haskell 非详尽模式缺失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47936615/

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