【发布时间】: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