简体   繁体   English

无法将[Char]与实际类型绑定(也许是a)

[英]Couldn't bind [Char] with actual type (Maybe a)

So I have this: 所以我有这个:

lstDelete :: [Char] -> Lst a -> Lst a
lstDelete k inp@(lstNode key)
    | k == key && lstIsEmpty = Map.delete key (Map.fromList inp)

The problem is, k is of type [Char], and key is of type (Maybe a): 问题是,k是[Char]类型,而key是(也许a)类型:

data Lst a = lstNode (Maybe a)

So I'm getting the error saying: 所以我收到错误消息:

Couldn't match expected type ‘[Char]’ with actual type ‘Maybe a’

How can I fix this without changing lstDelete :: [Char] -> Lst a -> Lst a ? 我如何在不更改lstDelete :: [Char] -> Lst a -> Lst a情况下解决此问题? I also want to keep the Maybe a since I want it to be able to be empty too. 我也想保留Maybe a因为我也希望它可以为空。

There is several problem with your code: 您的代码有几个问题:

  1. Constructors names should start with capital letter. 构造函数名称应以大写字母开头。
    data Lst a = LstNode (Maybe a)
  1. Your list can contain only one value 您的清单只能包含一个值

  2. lstDelete doesn't make any sense. lstDelete没有任何意义。 It accept [Char] as key and Lst a as list. 它接受[Char]作为键,并接受Lst a作为列表。 There is no way to compare them (there is no nontrivial function that can accept those parameters). 无法比较它们(没有非平凡的函数可以接受这些参数)。 You should either change type to 您应该将类​​型更改为

   lstDelete :: [Char] -> Lst [Char] -> Lst [Char]

or write more generic 或者写更通用的

   lstDelete :: Eq a => a -> Lst a -> Lst a
  1. Map.fromList accepts [(k, v)] , you can't pass Lst a to it. Map.fromList接受[(k, v)] ,您不能将Lst a传递给它。
  2. lstIsEmpty is not defined. lstIsEmpty

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将预期类型`Maybe(String,Int,String)'与实际类型`([Char],t0,[Char])'匹配 - Couldn't match expected type `Maybe (String, Int, String)' with actual type `([Char], t0, [Char])' 无法将预期的类型'Maybe Int-> Char'与实际类型'Char'相匹配 - Couldn't match expected type `Maybe Int -> Char' with actual type `Char' 无法将预期类型[a]与实际类型Maybe [a]相匹配 - Couldn't match expected type [a] with actual type Maybe[a] 无法将预期类型 '[foo]' 与实际类型 'Maybe [foo]' 匹配 - Couldn't match expected type ‘[foo]’ with actual type ‘Maybe [foo]’ 无法将预期的类型'Char'与实际类型'[Char]'相匹配 - Couldn't match expected type ‘Char’ with actual type ‘[Char]’ 错误无法将预期类型“ Char”与实际类型“ [Char]”匹配 - error Couldn't match expected type ‘Char’ with actual type ‘[Char]’ “无法将类型'Maybe'与'IO'匹配预期类型:IO String实际类型:Maybe String”在Haskell中 - “Couldn't match type `Maybe' with `IO' Expected type: IO String Actual type: Maybe String” In Haskell 无法将预期类型“ [Char]”与实际类型“ a”匹配 - Couldn't match expected type `[Char]' with actual type `a' Haskell无法将预期类型'String'与实际类型'Char'匹配 - Haskell Couldn't match expected type 'String' with actual type 'Char' 无法将预期类型“Bool”与实际类型“Char -> Bool”匹配 - Couldn't match expected type ‘Bool’ with actual type ‘Char -> Bool´
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM