Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: 'makeNtuple' function?

Tags:

haskell

tuples

Is there a function or functions in Haskell that takes n arguments, and returns an n-tuple? For example:

make3tuple:: a -> a -> a -> (a,a,a)
make3tuple a b c = (a,b,c)

ie: like comma, but for more than two arguments. Obviously make3tuple does the job, but I feel like there must be a built-in way to do this, and I've not found it, or missed some way to use some other ubiquitous function.

FWIW, this arises when using liftM3 (or higher). For example:

type RandomState a = State StdGen a
[...]
getTwoRandoms = liftM2 (,) getRandom getRandom
get3Randoms = liftM3 make3tuple getRandom getRandom getRandom

Thanks!

like image 756
gwideman Avatar asked Mar 04 '13 01:03

gwideman


2 Answers

Yes.

(,,) :: a -> b -> c -> (a, b, c)
(,,,) :: a -> b -> c -> d -> (a, b, c, d)

etc.

So you could write liftM3 (,,) getRandom getRandom getRandom

Haskell compilers provide functions like this up to a certain size (I think the guarantee is 15-tuples)

like image 118
amindfv Avatar answered Oct 17 '22 21:10

amindfv


Not as a function like makeNtuple :: Int -> a -> a -> ... -> (a,a,...) and note it seems to even be non-expressible in the type language. If you're okay with the tuple having a homogenous type then you can use "dependently-typed" Vectors

data Nat = Ze | Su Nat

data Vec :: * -> Nat -> * where
  Nil  :: Vec a Ze
  Cons :: a -> Vec a n -> Vec a (Su n)
like image 1
J. Abrahamson Avatar answered Oct 17 '22 19:10

J. Abrahamson