Luca9984
Luca9984

Reputation: 151

Generating tuples using list comprehension

I generate tuples using list comprehension:

[(a,b,c) | a <- [1..3], b <- [1..3], c <- [1..3]]

Since all three entries of the tuples are from the same interval, is there a shorter way to write this?

Upvotes: 3

Views: 121

Answers (1)

chi
chi

Reputation: 116174

A few alternatives

let xs = [1..3] in [(a,b,c) | a <- xs, b <- xs, c <- xs]
[(a,b,c) | let xs = [1..3], a <- xs, b <- xs, c <- xs]
(,,) <$> [1..3] <*> [1..3] <*> [1..3]
let xs = [1..3] in (,,) <$> xs <*> xs <*> xs
(\[a,b,c]->(a,b,c)) <$> replicateM 3 [1..3]

However, I'd look for the most readable one, rather than the shortest.

Upvotes: 4

Related Questions