Prototype
Prototype

Reputation: 114

Convert list haskell

I have this list ([Integer],[Integer]) which I want to convert into [(Integer,Integer)].

From what I've read I should use high order functions like map.

alterList :: ([Integer],[Integer]) -> [(Integer,Integer)]
alterList a = map(\a -> .....?)

Any guidelines? Both lists are of the same size.

Like I have two lists ([1,2,3,4],[5,6,7,9]) which I want to convert to [(1,5),(2,6),(3,7),(4,8)]

Upvotes: 0

Views: 84

Answers (1)

Stéphane Laurent
Stéphane Laurent

Reputation: 84539

So after you've corrected your post, I think you want

alterList :: ([Integer],[Integer]) -> [(Integer,Integer)]
alterList (l1, l2) = zip l1 l2

Example:

>>> alterList ([1,2,3,4],[5,6,7,9])
[(1,5),(2,6),(3,7),(4,9)]

This is equivalent to

alterList' :: ([Integer],[Integer]) -> [(Integer,Integer)]
alterList' = uncurry zip

Note that this function works even if the two lists have not the same size: in this case it acts as if the longest list were truncated to the length of the shortest one.

Upvotes: 4

Related Questions