Pablo Dominguez Lyons
Pablo Dominguez Lyons

Reputation: 57

Haskell, convert string into all possible "tuples" of itself

I have searched and found some solutions but none of them look as simple as what i have in mind, so you have a list of numbers [1,2,3,4] and want to print the tuples like this: [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]

So by using

combinaList :: String -> String
combinaList x = [(x,y) | x <- x, y <- drop 1 x ]

Should do the trick, drop the first element from the list and combine the two lists, however I am getting my types wrong and possibly the drop bit too as ghci keeps on whinning at me, any help would be appreciated, thanks!

Upvotes: 1

Views: 191

Answers (1)

hammar
hammar

Reputation: 139860

Use tails from Data.List.

combinaList xs = [(x, y) | (x:ys) <- tails xs, y <- ys]

Upvotes: 6

Related Questions