How to access nth element in a Haskell tuple -
i have this:
get3th (_,_,a,_,_,_) =
which works fine in ghci want compile ghc , gives error. if want write function nth element of tuple , able run in ghc should do? program below, should that?
get3th (_,_,a,_,_,_) = main = mytuple <- getline print $ get3th mytuple
your problem getline
gives string
, want tuple of kind. can fix problem converting string
tuple – example using built-in read
function. third line here tries parse string
six-tuple of int
s.
main = mystring <- getline let mytuple = read mystring :: (int, int, int, int, int, int) print $ get3th mytuple
note while useful learning types , such, should never write kind of code in practise. there @ least 2 warning signs:
you have tuple more 3 or elements. such tuple needed , can replaced list, vector or custom data type. tuples used more temporarily bring 2 kinds of data 1 value. if start using tuples often, think whether or not can create own data type instead.
using
read
read structure not idea.read
explode program terrible error message @ tiny little mistake, , that's not want. if need parse structures, it's idea use real parser.read
can enough simple integers , such, no more that.
Comments
Post a Comment