haskell - Taking the nth list from a list of lists -
i have following function returns list of list of pairs.
getprofilematrix :: profile -> [[(char, int)]] getprofilematrix (profile profilematrix _ _ _) = profilematrix
what want create function takes profile
, int
, char
value , looks in list [[(char, int)]]
, returns nth list (the int
parameter value) [(char, int)] , looks char
in list of tuples, , if parameter char
value matches, returns int
value of tuple.
in order int value of [(char, int)] list, constructed following function:
tuplenr :: char -> [(char, int)] -> int tuplenr letter list = head [nr | (ltr, nr) <- list, ltr == letter]
this function works expected. problem when trying extract nth list [[(char, int]] , applying tuplenr function it:
profilefrequency :: profile -> int -> char -> int profilefrequency p c = tuplenr c (map (\x -> (x !! i)).(getprofilematrix p))
but doesn't work.. don't understand why doesn't work. first call getprofilematrix
function returns list [[(char, int])]
profile
p
. after maps lambda function extract ith element list single list [(char, int)]
. , after apply tuplenr
function int
value of [(char, int)]
list. feel should work doesn't.
i following error:
couldn't match expected type ‘[(char, int)]’ actual type ‘a0 -> [b0]’ in second argument of ‘tuplenr’, namely ‘(map (\ x -> (x !! i)) . (getprofilematrix p))’ in expression: tuplenr c (map (\ x -> (x !! i)) . (getprofilematrix p)) in equation ‘profilefrequency’: profilefrequency p c = tuplenr c (map (\ x -> (x !! i)) . (getprofilematrix p)) couldn't match expected type ‘a0 -> [[b0]]’ actual type ‘[[(char, int)]]’ possible cause: ‘getprofilematrix’ applied many arguments in second argument of ‘(.)’, namely ‘(getprofilematrix p)’ in second argument of ‘tuplenr’, namely ‘(map (\ x -> (x !! i)) . (getprofilematrix p))’
sorry if i've not been clear enough, hope can help! thanks.
replace:
profilefrequency :: profile -> int -> char -> int profilefrequency p c = tuplenr c (map (\x -> (x !! i)).(getprofilematrix p))
with:
profilefrequency :: profile -> int -> char -> int profilefrequency p c = tuplenr c . map (!! i) . getprofilematrix $ p -- ^1 ^^^^^^2 ^^^3
the problems were:
function application has precedence on composition, in
tuplenr c (map (\x -> (x !! i))
passing(map (\x -> (x !! i))
, function, "second argument" oftuplenr
accepts list instead. want composetuplenr c
,(map (\x -> (x !! i))
instead.not issue,
map (\x -> (x !! i)
equivalentmap (!! i)
, can refactor that. it's same concept(+ 1)
being(\i -> + 1)
.in end, want pass
p
composed function (tuplenr c . map (!! i) . getprofilematrix
). that's not doing:(getprofilematrix p)
of type[[(char, int)]]
, not function type, can't compose it. therefore have delay application ofp
after functions have been composed.
Comments
Post a Comment