[Haskell-beginners] question about show -- RWH chapter 5

7stud bbxx789_05ss at yahoo.com
Thu Mar 26 11:41:47 EDT 2009


In chapter 5, RWH defines a JValue data type like this:


SimpleJSON.hs:
--------------

module SimpleJSON
    (
     JValue(..)
    ) where 

data JValue = JNumber Double
            | JString String
            | JArray [JValue]
            | JObject [(String, JValue)]
            | JBool Bool
            | JNull 
              deriving (Eq, Ord, Show)

------------


Then RWH defines some functions like this:

PutJSON.hs:
----------
module PutJSON where

import SimpleJSON

renderJValue::JValue->String
renderJValue (JNumber f)    = show f
renderJValue (JString s)    = show s
renderJValue (JBool True)   = "true"
renderJValue (JBool False)  = "false" 
renderJValue JNull          = "null"
----------

My question is about the function: 

renderJValue (JString s) = show s

A JString value contains a string, so why does the function use
show to convert s to a string?  Why isn't that function defined
like this:

renderJValue (JString s) = s

Using that modified function seems to work:


Main.hs:
---------
module Main () where

import SimpleJSON
import PutJSON 

main = let x = JString "hello" 
       in putStrLn (renderJValue x)


$ ghc -o simple Main.hs PutJSON.hs SimpleJSON.hs
/usr/libexec/gcc/i686-apple-darwin8/4.0.1/ld: warning -F: directory name 
(/Users/me/Library/Frameworks) does not exist

$ simple
hello

Also can anyone tell me why I always get that warning?

Thanks



More information about the Beginners mailing list