I’m trying to make a custom type an instance of Show.
Here’s theType, it’s just a basic Set type.
data Set a = Insert a (Set a) | EmptySet
I’d like something like
Insert 1 (Insert 2 (Insert 3 EmptySet))
to display like
{1, 2, 3}
How do I do this? I tried doing it with string concatenation, but it seems like doing string interpolation is considered bad form (Haskell doesn’t seem to natively support this?) Also, how do I get the curly braces around the list? So far, all I was able to cook up was this, which basically does nothing…
instance (Show a) => Show (Set a) where
show EmptySet = ""
show (Insert a as) = show a ++ show as
Also, I tried to use Hoogle and Hayoo to find the List implementation so I could see how this was implemented on Lists. I couldn’t find it. Does anyone have any pointers on this? I tried searching “show::[a]->String”, “Data.Lists”, “Lists” etc….
Here’s a solution with direct recursion:
If you don’t like the inefficient use of
(++), you can of course use difference lists:That should do it; so, let’s test: