Manage the type of a variable in the show implementation

Hi, I wrote this kind of code: data DataLog a = SingleData a | MultipleData [a] instance (Show a) => Show (DataLog a) where show (SingleData v) = show v show (MultipleData vs) = join "|" vs where join _ [] = [] join _ (x:[]) = show x join s (x:xs) = show x ++ s ++ join s xs It works but when the DataLog is String typed I got this result: "My Test"|"Another Value" I'd prefer have: My Test|Another Value I know the quote problem comes from the show function on String value. How could I do a show for no-string values and return directly the value for strings? Thanks. Chris

On Wed, Jul 23, 2014 at 2:20 PM, Christian Sperandio < christian.sperandio@gmail.com> wrote:
I know the quote problem comes from the show function on String value. How could I do a show for no-string values and return directly the value for strings?
You can't ! (ok you can but you'll need to use OverlappingInstances) Anyway, using Show for this is wrong, Show is supposed to translate datatypes in a form you could copy in your code to get the value back, it isn't supposed to be for presentation. You have libraries that do pretty formatting better. If you absolutely want this, I would suggest writing another class for it and use OverlappingInstances to allow for one "DataLog String" instance and one "(Show a) => YourClass (DataLog a)" instance. Another possibility is to do a newtype for strings that have a Show instance that doesn't put quotes around. That might even be a better idea, maybe your Strings really represent variables or something in this case anyway ? -- Jedaï

On Wed, Jul 23, 2014 at 7:40 PM, Chaddaï Fouché
Anyway, using Show for this is wrong, Show is supposed to translate datatypes in a form you could copy in your code to get the value back, it isn't supposed to be for presentation. You have libraries that do pretty formatting better.
Very insightful, thanks! -- Kim-Ee

I chose the newtype solution.
Thanks :)
2014-07-23 14:40 GMT+02:00 Chaddaï Fouché
On Wed, Jul 23, 2014 at 2:20 PM, Christian Sperandio < christian.sperandio@gmail.com> wrote:
I know the quote problem comes from the show function on String value. How could I do a show for no-string values and return directly the value for strings?
You can't ! (ok you can but you'll need to use OverlappingInstances)
Anyway, using Show for this is wrong, Show is supposed to translate datatypes in a form you could copy in your code to get the value back, it isn't supposed to be for presentation. You have libraries that do pretty formatting better.
If you absolutely want this, I would suggest writing another class for it and use OverlappingInstances to allow for one "DataLog String" instance and one "(Show a) => YourClass (DataLog a)" instance.
Another possibility is to do a newtype for strings that have a Show instance that doesn't put quotes around. That might even be a better idea, maybe your Strings really represent variables or something in this case anyway ? -- Jedaï
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (3)
-
Chaddaï Fouché
-
Christian Sperandio
-
Kim-Ee Yeoh