t->t->[t]->[t] subst a b [xs]=[if x==a then b else x | x-6ren">
gpt4 book ai didi

haskell 。 "Could not deduce (t ~ [t]) from the context (Eq t)"

转载 作者:行者123 更新时间:2023-12-02 21:32:23 24 4
gpt4 key购买 nike

仅仅自学haskell,并不能解决这个问题。这些是代码。

subst :: Eq t=>t->t->[t]->[t]
subst a b [xs]=[if x==a then b else x | x <- xs]

错误如下。

subst.hs:2:46:
Could not deduce (t ~ [t])
from the context (Eq t)
bound by the type signature for
subst :: Eq t => t -> t -> [t] -> [t]
at subst.hs:1:10-29
‘t’ is a rigid type variable bound by
the type signature for subst :: Eq t => t -> t -> [t] -> [t]
at subst.hs:1:10
Relevant bindings include
xs :: t (bound at subst.hs:2:12)
b :: t (bound at subst.hs:2:9)
a :: t (bound at subst.hs:2:7)
subst :: t -> t -> [t] -> [t] (bound at subst.hs:2:1)
In the expression: xs
In a stmt of a list comprehension: x <- xs

我猜问题在于 haskell 无法确保 [t] 中的元素与 t 匹配。我不知道。我想知道如何解决这个问题。

最佳答案

你只有一个[..]包装到许多:

subst :: Eq t=>t->t->[t]->[t]
subst a b xs = [if x==a then b else x | x <- xs]

这只是xs不是[xs]

原因很简单:如果你写 [xs]你告诉 Haskell 期待一个包含单个条目的列表 xs它会尝试模式匹配它 - 之后你告诉它提取值 x来自xs ( x <- xs ) 告诉 Haskell xs本身必须是某种列表。所以最后它会假设 t成为一些 list t ~ [s] 。但然后你继续检查 x == a在这里x :: sa :: t以现在的错误结束 [s] ~ t ~ s .

关于 haskell 。 "Could not deduce (t ~ [t]) from the context (Eq t)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31810616/

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