【发布时间】:2018-05-03 08:16:07
【问题描述】:
在 REPL 中这是有效的:
> mm n = (\n -> n * 2) <$> n
> mm (2:3:Nil)
(4 : 6 : Nil)
在这个编译的文件中,我可以运行它:
squareOf ls =
map (\n -> n * n) ls
但是当我向该函数添加类型定义时
squareOf :: List Int -> Int
squareOf ls =
map (\n -> n * n) ls
我收到一个错误:
Could not match type
List Int
with type
Int
while checking that type t0 t1
is at least as general as type Int
while checking that expression (map (\n ->
(...) n
)
)
ls
has type Int
in value declaration squareOf
where t0 is an unknown type
t1 is an unknown type
我尝试将签名更改为列表的类型别名,并且我尝试了一个 forall 定义但没有运气。 如果我检查未在函数中添加签名时创建的定义,我会得到:
forall t2 t3. Functor t2 => Semiring t3 => t2 t3 -> t2 t3
谁能解释为什么我的签名不正确以及为什么我会得到这个函数的签名?
干杯
编辑:感谢 cmets,更新 fn 定义使其也返回 List Int,当然它解决了问题
【问题讨论】:
-
检查函数的返回类型:
Int。但是 map 是结构保留的,所以它必须在你的例子中返回一个List的东西。
标签: purescript