我有这个代码,它会有条件地将元组更新(或添加)到元组列表中。我得到了上述错误
updateTuple :: String -> String -> Int -> [Film] -> String
updateTuple userName requestedTitle newRating ((Film title _ _ ratings):restOfFilms)
| requestedTitle == title = map (\ rating -> if rating == (userName,_) then (userName,newRating) else rating) ratings
| otherwise = updateTuple userName requestedTitle newRating restOfFilms
答案 0 :(得分:1)
问题在于这个lambda:
\rating -> if rating == (userName,_) then (userName,newRating) else rating
您在表达式上下文中使用通配符,这对编译器没有意义,因为通配符只能用于模式匹配上下文。
我想你打算这样做:
\rating@(userName', _) ->
if userName' == userName then (userName,newRating) else rating
答案 1 :(得分:0)
正如@bheklilr和@Nikita Volkov所说,问题在于:
| requestedTitle == title = map (\ rating -> if rating == (userName,_) then (userName,newRating) else rating) ratings
另一种方法是使用@ -syntax来解构rating
变量:
| requestedTitle == title = map (\rating@(ruser,_) -> if ruser == userName then ... else rating)