简体   繁体   English

无法将预期类型“a”与实际类型“[a]”匹配

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

I have this list in which I want to go through and convert certain elements of the list into a string.我有这个列表,我想通过 go 并将列表的某些元素转换为字符串。 The first 3 functions work, however, the last one (placeRainfull) does not.前 3 个函数有效,但最后一个 (placeRainfull) 无效。 When I try to load the script, I get this error:当我尝试加载脚本时,我收到此错误:

Couldn't match expected type ‘Place’ with actual type ‘[Place]’

I want the function to go through each element in the list and have it run through the addDayWithRainfull function.我希望通过列表中的每个元素将 function 到 go 并让它通过 addDayWithRainfull function 运行。

Code代码

getRainfull :: Place -> (String, [Int])
getRainfull (Place p _ _ rf ) = (p, rf)

convrtIntArray :: [Int] -> [String]
convrtIntArray rainfullArray = map show [ i | i <- rainfullArray]

addDayWithRainfull :: (String, [Int]) -> String
addDayWithRainfull (p, rf) = p ++ " " ++ unwords (convrtIntArray rf)

placeRainful :: [Place] -> String
placeRainful places = addDayWithRainfull (getRainfull places)

You've told the compiler that placeRainful takes a list of Place s ( [Place] ), but then you're passing that list of places to getRainFull , which you have indicated takes only a single Place .您已经告诉编译器placeRainful需要一个Place s ( [Place] ) 的列表,但随后您将该位置列表传递给getRainFull ,您已经指出它只需要一个Place

In other words, Haskell is inferring based on the implementation of placeRainful that it should have type Place -> String , but then you said that that's not it's type.换句话说, Haskell 是根据placeRainful的实现推断它应该具有类型Place -> String ,但是你说那不是它的类型。

As a side note, based on this several similar questions you've asked, it seems like you're having trouble differentiating the types [Place] and Place .作为旁注,根据您提出的几个类似问题,您似乎无法区分类型[Place]Place Haskell (and indeed, most programming languages) cares a great deal about this distinction. Haskell(实际上,大多数编程语言)非常关心这种区别。 Think about the data that each type represent:想想每种类型代表的数据:

[1, 2, 3, 4, 5] :: [Int]

1 :: Int

These types need to be treated differently because it's very clear what a should be in this expression:这些类型需要区别对待,因为很清楚a在这个表达式中应该是什么:

let a = 1 * 1

but it's not at all clear what it should be in this expression:但目前还不清楚这个表达式应该是什么:

let a = [1, 2, 3] *  [1, 2, 3]

The problem is that you defined a function:问题是你定义了一个 function:

getRainfull :: Place -> (String, [Int])

which takes as input a single Place object, but in your placeRainful you write:它将单个Place object 作为输入,但在你的placeRainful你写:

placeRainful :: [Place] -> String
placeRainful places = addDayWithRainfull (getRainfull places)

so here you expect to process a list of Place s, that will not work.所以在这里你希望处理一个Place列表,这是行不通的。 You can for example make use of concatMap:: Foldable f => (a -> [b]) -> fa -> [b] to process each Place in the list of places and then concatenate these together:例如,您可以使用concatMap:: Foldable f => (a -> [b]) -> fa -> [b]来处理地点列表中的每个Place ,然后将它们连接在一起:

placeRainful :: [Place] -> String
placeRainful places = concatMap (addDayWithRainfull . getRainfull) places

or with [a] -> [[a]] -> [a] :或使用[a] -> [[a]] -> [a]

import Data.List(intercalate)

placeRainful :: [Place] -> String
placeRainful places = intercalate "," (map (addDayWithRainfull . getRainfull) places)

or perhaps you need to use some other function, but the general idea is that you will need something to pass elements of the list to getRainfull , not the entire list itself.或者您可能需要使用其他一些 function,但一般的想法是您需要将列表的元素传递给getRainfull ,而不是整个列表本身。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM