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!
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)
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" Vector
s
data Nat = Ze | Su Nat
data Vec :: * -> Nat -> * where
Nil :: Vec a Ze
Cons :: a -> Vec a n -> Vec a (Su n)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With