gpt4 book ai didi

PureScript 无法将 Maybe Int 类型与 Int 类型匹配

转载 作者:行者123 更新时间:2023-12-05 08:54:41 24 4
gpt4 key购买 nike

我正在做“PureScript by example”一书中的作业,使用递归来计算数组中偶数项的数量。

这是我写的代码

isEven :: Int -> Boolean
isEven 0 = true
isEven 1 = false
isEven n = isEven(n - 2)

countEven :: List Int -> Int
countEven list =
if null list
then 0
else
if isEven(unsafePartial (head list))
then 1
else 0 + countEven(unsafePartial (tail list))

我收到一条错误消息说

Error found:
in module Main
at src/Main.purs line 74, column 17 - line 74, column 42

Could not match type

Maybe Int

with type

Int


while checking that type t0
is at least as general as type Int
while checking that expression unsafePartial (head list)
has type Int
in binding group countEven

对此我有点意外,因为unsafePartial head list的数据类型是Int,unsafePartial tail list是List Int

那么为什么感觉某处有一个 Maybe?

最佳答案

您收到此错误的事实意味着您正在使用 headtail来自 Data.List,它们实际上不是部分的,而是返回一个 Maybe

要使用它们的部分副本,请从 Data.List.Partial 导入它们.

关于PureScript 无法将 Maybe Int 类型与 Int 类型匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48779739/

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