Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Defined in <code>Prelude</code>,</p> <pre><code>type ShowS = String -&gt; String class Show a where showsPrec :: Int -&gt; a -&gt; ShowS show :: a -&gt; String showList :: [a] -&gt; ShowS type ReadS a = String -&gt; [(a, String)] class Read a where readsPrec :: Int -&gt; ReadS a readList :: ReadS [a] read :: (Read a) =&gt; String -&gt; a </code></pre> <p>In short, these are the standard "serialization" methods in Haskell. <code>show :: (Show a) =&gt; a -&gt; String</code> can turn anything that is an instance of <code>Show</code> into a string, and <code>read :: (Read a) =&gt; String -&gt; a</code> can turn a string into anything that is an instance of <code>Read</code> (or throw an exception).</p> <p>Most of the built-in types and data structures in the standard library have <code>Show</code> and <code>Read</code> instances defined; if you're composing parts from them, your type also has <code>Show</code> and <code>Read</code> instances defined.</p> <pre><code>type Table = [(String, String)] load :: (Read a) =&gt; FilePath -&gt; IO a load f = do s &lt;- readFile f return (read s) save :: (Show a) =&gt; a -&gt; FilePath -&gt; IO () save x f = writeFile f (show x) </code></pre> <p>If <code>Table</code> were a datatype, you have to ask for the instances, but you can request that the compiler automatically derive them for you.</p> <pre><code>data Table = Table [(String, String)] deriving (Read, Show) </code></pre> <p>Sometimes that's not possible and you have to define your own instances.</p> <pre><code>instance Show Table where showsPrec p x = ... instance Read Table where readsPrec p x = ... </code></pre> <p>But that shouldn't be common.</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