【问题标题】:Taking the nth list from a list of lists从列表列表中获取第 n 个列表
【发布时间】:2014-12-01 23:44:29
【问题描述】:

我有以下函数,它返回对列表的列表。

getprofileMatrix :: Profile -> [[(Char, Int)]]
getprofileMatrix (Profile profileMatrix _ _ _) = profileMatrix

我想做的是创建一个函数,它接受一个Profile、一个Int 和一个Char 值,然后在列表[[(Char, Int)]] 中查找并返回第n 个列表(Int 参数值) 看起来像这样 [(Char, Int)] 然后在元组列表中查找Char,如果我的参数Char 值匹配,则它返回该元组的Int 值。

为了得到一个 [(Char, Int)] 列表的 Int 值,我构造了以下函数:

tupleNr :: Char -> [(Char, Int)] -> Int
tupleNr letter list = head [nr | (ltr, nr) <- list, ltr == letter]

此功能按预期工作。我的问题是尝试从 [[(Char, Int]] 中提取第 n 个列表,然后对其应用 tupleNr 函数时:

profileFrequency :: Profile -> Int -> Char -> Int
profileFrequency p i c = tupleNr c (map (\x -> (x !! i)).(getprofileMatrix p)) 

但它不起作用..我真的不明白为什么它不起作用。首先我调用getprofileMatrix 函数,它从Profilep 返回列表[[(Char, Int])]。之后,它映射我的 lambda 函数以从列表中提取第 i 个元素,以便我获得单个列表 [(Char, Int)]。之后,我应用tupleNr 函数来获取[(Char, Int)] 列表的Int 值。我觉得这应该有效,但它没有。

我收到以下错误:

  Couldn't match expected type ‘[(Char, Int)]’
                with actual type ‘a0 -> [b0]’
    In the second argument of ‘tupleNr’, namely
      ‘(map (\ x -> (x !! i)) . (getprofileMatrix p))’
    In the expression:
      tupleNr c (map (\ x -> (x !! i)) . (getprofileMatrix p))
    In an equation for ‘profileFrequency’:
        profileFrequency p i c
          = tupleNr c (map (\ x -> (x !! i)) . (getprofileMatrix p))


    Couldn't match expected type ‘a0 -> [[b0]]’
                with actual type ‘[[(Char, Int)]]’
    Possible cause: ‘getprofileMatrix’ is applied to too many arguments
    In the second argument of ‘(.)’, namely ‘(getprofileMatrix p)’
    In the second argument of ‘tupleNr’, namely
      ‘(map (\ x -> (x !! i)) . (getprofileMatrix p))’

对不起,如果我不够清楚,但希望任何人都可以提供帮助!谢谢。

【问题讨论】:

    标签: list haskell matrix tuples extract


    【解决方案1】:

    替换:

    profileFrequency :: Profile -> Int -> Char -> Int
    profileFrequency p i c = tupleNr c (map (\x -> (x !! i)).(getprofileMatrix p))
    

    与:

    profileFrequency :: Profile -> Int -> Char -> Int
    profileFrequency p i c = tupleNr c . map (!! i) . getprofileMatrix $ p
    --                                 ^1    ^^^^^^2                  ^^^3
    

    问题是:

    1. 函数应用程序优先于组合,因此在tupleNr c (map (\x -&gt; (x !! i)) 中,您将传递(map (\x -&gt; (x !! i)),它是一个函数,作为tupleNr 的“第二个参数”,它接受一个列表。你想要的是改写tupleNr c(map (\x -&gt; (x !! i))

    2. 不是真正的问题,但map (\x -&gt; (x !! i) 等效于map (!! i),因此您可以将其重构为。这与(+ 1)(\i -&gt; i + 1) 的概念相同。

    3. 最后,您想要的是将p 传递给组合函数(tupleNr c . map (!! i) . getprofileMatrix)。那不是你在做的事情:(getprofileMatrix p)[[(Char, Int)]] 类型,它不是函数类型,所以你不能组合它。因此,您必须将p 的应用延迟到所有函数组合完成后。

    【讨论】:

    • 感谢您的回复..!但我仍然收到一个错误:无法将预期类型“Int”与实际类型“a0 -> Int”匹配在表达式中:tupleNr c。 (map (\ x -> (x !! i)) . (getprofileMatrix p)) 在“profileFrequency”的等式中:profileFrequency p i c = tupleNr c。 (map (\ x -> (x !! i)) . (getprofileMatrix p))
    • 哇,非常感谢添加的 cmets!真的很感激!
    猜你喜欢
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 2012-10-26
    • 2019-09-05
    相关资源
    最近更新 更多