Generating all tuples (Haskell)

From LiteratePrograms
Jump to: navigation, search
Other implementations: Haskell | Lisp | Perl | Python | Ruby

This is a simple example of the tuples generation algorithm, written in Haskell.

see The Art of Computer Programming (TAOCP) Volume 4 Fascicle 2, Generating All Tuples and Permutations (2005), ISBN 0-201-85393-0

[edit] Algorithm M

The generation function is genTuplesM as below:

  • Input parameter: we list the limits on each bit into a list as an input
  • Output: generating all possible tuples
<<genTuplesM.hs>>=
module Main where

genTuplesM :: [Int] -> [[Int]]
genTuplesM = mapM (\b -> [1..b])

genTuplesM [3,5,3,2]

[edit] Test

Just load the file genTuplesM.hs in GHC, execute it and you will get the result.

Download code
hijacker
hijacker
hijacker
hijacker