Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
元组理解Haskell_Haskell_Tuples - Fatal编程技术网

元组理解Haskell

元组理解Haskell,haskell,tuples,Haskell,Tuples,最终目标是获得一个评分列表,这样列表中就不会重复相同的用户。因此,您不能有两次“Emma”评级 数据和元组定义为: type Rating = (String, Int) data Film = Film { title :: String, director :: String, year :: Int, ratings :: [Rating] } deriving (Re

最终目标是获得一个评分列表,这样列表中就不会重复相同的用户。因此,您不能有两次“Emma”评级

数据和元组定义为:

type Rating = (String, Int)
data Film = Film {  title :: String,
                    director :: String,
                    year :: Int,
                    ratings :: [Rating] } deriving (Read,Show)
旧功能定义为(工作正常):

我不明白我错在哪里。 我对函数的理解是。->获取评分并将其与旧的评分列表进行比较,以检查用户是否匹配。如果他们这样做了,不要包括他们的具体评级,并继续通过名单,直到空。然后将新评级附加到列表中

有人能解释一下我哪里出了问题吗

我得到的错误是:

 v3.hs:104:63:
    Couldn't match expected type ‘[Rating] -> Bool’
                with actual type ‘Bool’
    In the first argument of ‘(.)’, namely
      ‘((map fst ratings) /= newUser)’
    In the first argument of ‘filter’, namely
      ‘(((map fst ratings) /= newUser) . ratings)’
    In the second argument of ‘(.)’, namely
      ‘filter (((map fst ratings) /= newUser) . ratings)’

v3.hs:104:72:
    Couldn't match expected type ‘[(Char, b0)]’
                with actual type ‘Film -> [Rating]’
    Probable cause: ‘ratings’ is applied to too few arguments
    In the second argument of ‘map’, namely ‘ratings’
    In the first argument of ‘(/=)’, namely ‘(map fst ratings)’

v3.hs:104:93:
    Couldn't match type ‘(String, Int)’ with ‘Film’
    Expected type: Rating -> [Rating]
      Actual type: Film -> [Rating]
    In the second argument of ‘(.)’, namely ‘ratings’
    In the first argument of ‘filter’, namely
      ‘(((map fst ratings) /= newUser) . ratings)’

第一:当询问ins为什么不工作时,请提供实际发生的情况的粘贴:如果包含编译器错误消息,您将更快获得更好的帮助,因此潜在的回答者不必复制和粘贴您的代码来查看错误

我首先想到的是

(map fst ratings)/= newUser
一点也不好输入:
newUser
是一个字符串,所以它不可能等于
(映射fst评级)
,一个字符串列表。你想要的东西更像:

filter ((newUser /=) . fst) . ratings
另一个问题是你把这一点放在了一边:有时候这很酷,但在这里,我认为这让阅读变得更加困难。这样写要简单得多:

uniquify :: [Rating] -> [Rating]
uniquify [] = []
uniquify (rate@(newUser,_):more) = rate : uniquify (filter ((newUser /=) . fst) more)
但更好的方法是找到一个合适的内置函数,在本例中,它是
Data.List.nubBy
,与
Data.function.on
结合使用:

uniquify' :: Eq a => [(a,b)] -> [(a,b)]
uniquify' = nubBy ((==) `on` fst)
这是一种更通用的类型,您可以专门指定给
uniquify
的类型,而且实现起来更简单。

感谢您1)告诉我发布时哪里出错了!2) 为我指明了正确的方向。它成功了
uniquify :: [Rating] -> [Rating]
uniquify [] = []
uniquify (rate@(newUser,_):more) = rate : uniquify (filter ((newUser /=) . fst) more)
uniquify' :: Eq a => [(a,b)] -> [(a,b)]
uniquify' = nubBy ((==) `on` fst)