首页 > 解决方案 > 无法将类型“[Char]”与 spa 的“Char”匹配

问题描述

我正在尝试将水疗中心的名称提供给对技能服务进行评分的给定主管以及他们对每个水疗中心的评分结果。但我不断收到如下所示的错误。对不起,如果我的代码看起来很糟糕,因为我是 Haskell 的新手。

* Couldn't match type `[Char]' with `Char'
  Expected type: Spa -> Service
    Actual type: Spa -> [Service]
* In the second argument of `(.)', namely `getService'
  In the first argument of `filter', namely `((== se) . getService)'
  In the expression: filter ((== se) . getService)

我尝试为主管检索和过滤。然后我全部输出。是否可以只输出主管名称、水疗中心和等级评级?我走对路了吗?如何修复错误?

data Spa = Spa SID Brand Area Stars [(Service, LevelRating)]

-- spa service
getService :: Spa -> [Service]
getService (Spa _ _ _ _ xs) = map fst xs

-- filter spa service
spaService :: Service -> [Spa] -> [Spa]
spaService se = filter ((==se) . getService)

ratedListStr :: Spa -> String
ratedListStr (Spa sid br ar st s) = "\nSpaID: " ++ sid ++ "\n Brand: " ++ br ++ "\n Area: " ++ ar ++ "\n Star: " ++ show st ++ "\n Service Rating" ++ show s 

标签: haskell

解决方案


getService返回 s, zo 的列表,其中是single ,所以没有多大意义。由于 的操作数是 a而另一个是 a 。由于作用于相同类型的两个项目,因此不会进行类型检查。Service[Service]seService(==se)Service[Service](==) :: Eq a => a -> a -> Bool

如果您想检查是否se是例如服务的一个元素,您可以使用elem :: Foldable f => a -> f a -> Bool

spaService :: Service -> [Spa] -> [Spa]
spaService se = filter (elem se . getService)

推荐阅读