Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a list of 10-tuples in Haskell and I want to get nth tuple from that list of tuples. But as I saw, only length function worked with that list. head, tail or !! functions didn't work. Can you tell me what should I do? The tuples are composed of integers and strings. For example when I try this :

tail [(3,5,"String1","String2","String3","String4","String5","String6","String7","String8"),(3,5,"String1","String2","String3","String4","String5","String6","String7","String8"),(3,5,"String1","String2","String3","String4","String5","String6","String7","String8")]

I get this error message from hugs:

ERROR - Cannot find "show" function for:
*** Expression : tail [(3,5,"String1","String2","String3","String4","String5","String6","String7","String8"),(3,5,"String1","String2","String3","String4","String5","String6","String7","String8"),(3,5,"String1","String2","String3","String4","String5","String6","String7","String8")]
*** Of type    : [(Integer,Integer,[Char],[Char],[Char],[Char],[Char],[Char],[Char],[Char])]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
210 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here's how to declare a Show instance for a 3-tuple. Hopefully this illustrates the idea and you can extend it to more elements:

import Data.List (intercalate)

instance (Show a, Show b, Show c) => Show (a, b, c) where
  show (a, b, c) = "(" ++ (intercalate "," ([show a, show b, show c])) ++ ")"

You can read the instance declaration just like logical implication: if I can show values of type a, b, and c, then I can show a tuple of type (a, b, c), and here's how.

GHC defines a Show instance for everything up to a 15-tuple, so you probably won't need to define this in your case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...