tags:

views:

85

answers:

1

how to write a function from list to a tuple

i have taken the string to a tuple. but i need to send it to a tuple.

can someone help me

+2  A: 

You can't convert an arbitrarily long list to a tuple because tuples of different lengths are considered as distinct types. But you can have:

listToTuple2 :: [a] -> (a, a)
listToTuple3 :: [a] -> (a, a, a)
listToTuple4 :: [a] -> (a, a, a, a)
listToTuple5 :: [a] -> (a, a, a, a, a)

etc.

See also http://www.haskell.org/haskellwiki/Template_Haskell#Convert_the_first_n_elements_of_a_list_to_a_tuple.

KennyTM
thanks a lot. i found a solution. but have some small clarification to dolisttotuple :: [String] -> (String,String,String)listtotuple [] = error "Empty List"listtotuple (a:b:c:d) = (a,b,c)i tried just putting (a:b:c) only but its not working. then i put another one . then it work. can someone explain the reason for that
Nubkadiya
@Pradeep: `(:) :: a -> [a] -> [a]`. The RHS of the cons operator must be a list. And you can just use pattern matching `listtotuple [a,b,c] = (a,b,c)`.
KennyTM
`[a,b,c]` is equivalent to `(a:b:c:[])`, and will match a list of exactly three elements. To match a list of three or more (and bind names to the first three), you can use `(a:b:c:d)` or, since in this case you don't need to refer to the rest of the list as `d`, `(a:b:c:_)`. Note the difference between `[]` matching the empty list, and `_` or `d` matching any list.
Nefrubyr