A.A
A.A

Reputation: 823

Iterate through a list of pairs in Haskell

I have a list of pairs eg. [("Word",3),("Test",1)] that I want to loop through and extract the key and value to pass to another function I've defined.

So in this case test function has a list of pairs and it's calling another function with the key and value of a pair. Obviously this throws an errors as it's incorrect but I'm not sure how to go about this.

test :: String -> String
test str = [another (fst n) (snd n) | n <- list]
   where list = genPairList

another :: String -> Int -> String
another str n = str ++ (replicate n 'T')

Upvotes: 1

Views: 1204

Answers (1)

karakfa
karakfa

Reputation: 67567

you can write

 test list = [another w n | (n,w) <- list]

but notice that the return type is [String]

> test $ zip [1..] ["a","b","c"]
["aT","bTT","cTTT"]`

Upvotes: 1

Related Questions