Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to "show" a constructor in lower case
    text
    copied!<p>Suppose I'd want to create a new data type and make the constructors showable, only in lower case instead of their upper case definition. For example:</p> <pre><code>data Day = Monday | Tuesday | Wednesday| Thursday | Friday | Saturday | Sunday </code></pre> <p>By adding <em>deriving Show</em>, ghci would print them as "Monday, Tuesday.. etc." To get it to show "monday, tuesday.. etc" I've tried to make a special instance of show:</p> <pre><code>import Data.Char strToLower :: [Char] -&gt; [Char] strToLower (x:xs) = toLower x : strToLower xs strToLower [] = [] instance Show Day where show d = strToLower (show d) </code></pre> <p>where the first occurrence of show should designate my new amended show function (which will be called everytime I print) whereas for the second I intend the normally derived version of show, to get from the constructor name to a String.</p> <p>Of course this doesn't work (circular definition) as the ghci has no clue to my separate meanings of the word "show" but I can't figure out how to let him know the distinction, for both versions need to be named show, the first because that's what print calls to and the second because it's a predefined haskell function which can give me a String out of a constructor name. I've tried </p> <pre><code>show d = strToLower ((showsPrec 0 d) "") </code></pre> <p>but this comes down to the same circular definition, at least that's what I guess from the ghci getting stuck in a loop.</p> <p>I understand why constructor names need to begin with an upper case letter, but showing them lower case shouldn't be a problem, should it? I know I could just define my show function for every case separately, e.g. <code>show Monday = "monday"</code> <code>show Tuesday = "tuesday"</code> etc, but I'm only using the days of the week as an example here and my real data type consists of 64 constructors so I think it would be more elegant to solve it differently somehow.</p> <p>Is it possible to dig into the haskell definition of show and alter a copy of that code? This is the only possible solution I can think of but I don't know how to do it, if it is possible at all. Probably not. So other solutions are very welcome as well!</p> <p>Thank you for taking your time,</p> <p>Jelle (Haskell beginner)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload