Using DataKinds and TypeOperators, I can make type level-tuples of types, and type-level lists of types, but I cannot nest them:
> :k '['(Int, Int), '(Int, Int)]
error: parse error on input ‘'’`
I can make a list of multiple tuples:
> :k ['(Int,Int),'(Int,Int)]
['(Int,Int),'(Int,Int)] :: [(*, *)]
But this doesn't work with only one tuple gives:
:k ['(Int,Bool)]
<interactive>:1:2: error:
• Expected a type, but ‘'(Int, Bool)’ has kind ‘(*, *)’
It can be done using KindSignatures, but it is very verbose:
> :k '[('(Int,Bool) :: (*,*))]
'[('(Int,Bool) :: (*,*))] :: [(*, *)]
Is there a less verbose way to do this, or is this the best way?
One is of type (String,Int) , whereas the other is (Int,String) . This has implications for building up lists of tuples. We could very well have lists like [("a",1),("b",9),("c",9)] , but Haskell cannot have a list like [("a",1),(2,"b"),(9,"c")] .
A tuple is a fixed-length coupling of values, written in parentheses with the values separated by commas. One way to use this is to pass all parameters into a function as one value, rather than the curried functions we've seen so far.
In a general way, you can't. Each size of tuple is a distinct type, whereas lists of any length are a single type. Thus, there's no good way to write a function that takes a list and returns a tuple of the same length--it wouldn't have a well-defined return type.
2. snd. This tuple function is used to get the second element from the tuple values or group. We can use this function before the tuple and it will return us the second element as the result in Haskell.
You need to add a space:
> :k '['(Int, Int), '(Int, Int)]
<interactive>:1:1: error: parse error on input '
> :k '[ '(Int, Int), '(Int, Int)]
'[ '(Int, Int), '(Int, Int)] :: [(*, *)]
Essentially, the parser is confused by the char literal '['
which happens to be at the beginning.
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