question about show -- RWH chapter 5

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

Well, for JSON, I think the rendered string must be enclosed in double
quotes, which is what the show instance for String does.
So
renderJValue (JString s) = show s
is not the same as
renderJValue (JString s) = s
You can easily see this with GHCi. A copy from my Windows session:
C:\>ghci
GHCi, version 6.10.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> let s = "Haskell"
Prelude> s
"Haskell"
Prelude> show s
"\"Haskell\""
Prelude> show (show s)
"\"\\\"Haskell\\\"\""
Prelude>
On Thu, Mar 26, 2009 at 4:41 PM, 7stud
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
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Peter Verswyvelen
Well, for JSON, I think the rendered string must be enclosed in double quotes, which is what the show instance for String does.
Ahh. I see. My version yields this output: main = let x = JString "hello" y = JNumber 10 rx = renderJValue x ry = renderJValue y in putStrLn ("[" ++ rx ++ ", " ++ ry ++ "]") $ simple [hello, 10.0] ...and that is not correct JSON format. hello looks like a variable name--not a string. The books version produces: ["hello", 10.0] which is what a javascript array that contains a string looks like. Thanks.

Am Donnerstag 26 März 2009 16:41:47 schrieb 7stud:
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
The JSON values are rendered so that they can be parsed at the other end. The parser must have the string enclosed in quotation marks for that, also some characters need to be escaped. Consider having JString "true" JString "[true, null, false]" JString "This is a\nmultiline text.\b!"
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?
Does the directory exist? If it exists, is in in the linker path?
Thanks

Daniel Fischer
$ 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?
Does the directory exist? If it exists, is in in the linker path?
No it doesn't exist. There is a directory: /Library/Frameworks This is what I did to install ghc: 1) I downloaded GMP.framework and GNUreadline.framework, which my mac automatically unzipped and placed on my desktop. I then dragged the resulting two folders into /Library/Frameworks as per the instructions at: http://www.haskell.org/ghc/download_ghc_682.html#macosxintel 2) I downloaded ghc-6.8.2 3) I unzipped an untared into /Users/me/my_tar_extractions 4) I cd'ed into the newly created ghc-6.8.2 folder. 5) I read the INSTALL document in the ghc-6.8.2 folder. 6) I ran the command: $ ./configure 7) Then I ran the command: $ sudo make install 8) At the end of the install output, I got a message that said: ------------- Installation of ghc-6.8.2 was successful. To use, add /usr/local/bin to your PATH. Warning: this binary distribution does NOT contain documentation! -------------- 9) I appended /usr/local/bin onto the PATH in ~/.bash_profile. After unzipping and untaring ghc, should I have put the ghc-6.8.2 folder in /Library/Frameworks like I did with GMP.framework and GNUreadline.framework? Can I still do that?

7stud
5) I read the INSTALL document in the ghc-6.8.2 folder.
This is what INSTALL says: ---- This is the INSTALL instructions for a binary distribution of GHC. For more details on what on earth this package is up to, please consult the README and ANNOUNCE. This distribution can be installed in a location of your choosing. To set the ball rolling, run the configure script (as usual, run the script with --help to see what options it supports). eg. to set up the package for installing in directory <my-dir>, use ./configure --prefix=<my-dir> The default installation directory is /usr/local. The configure script will figure out what platform you're running on, and a couple of other interesting pieces of trivia, which it will then fill in the Makefile.in template to give you a real Makefile. If you're of a paranoid persuasion, you might want to take a look at this Makefile to see if the information is correct. Now run: make install (`make show-install-setup' prints the details of where the different pieces of the bundle are heading when -- possibly helpful). For more information, full GHC documentation is available from the main GHC site: http://www.haskell.org/ghc Bug reports/suggestions for improvement to the installation procedure/setup (as well as other GHC related troubles you're experiencing, of course), gratefully received. Bug reporting instructions are here: http://www.haskell.org/ghc/reportabug Enjoy, -- The GHC Team. --------------- It didn't say anything about putting the ghc folder in /Library/Frameworks.

Daniel Fischer
writes: $ ghc -o simple Main.hs PutJSON.hs SimpleJSON.hs /usr/libexec/gcc/i686-apple-darwin8/4.0.1/ld: warning -F:
Am Donnerstag 26 März 2009 17:20:46 schrieb 7stud: directory
name
(/Users/me/Library/Frameworks) does not exist
$ simple hello
Also can anyone tell me why I always get that warning?
Does the directory exist? If it exists, is in in the linker path?
No it doesn't exist. There is a directory:
/Library/Frameworks
That explains the warning, though not why ld is looking for that directory. Since I don't know Macs, I can only guess. Maybe the fact that you untarred ghc to /Users/me/my_tar_extractions/ghc-6.8.2 confused it as it expected to be untarred to /usr/local/ghc-6.8.2 and find the frameworks in ../../Library/Frameworks. You can try symlinking /Library to /Users/me and see if that gets rid of the warning, though for a real fix, a mac-user's answer would be required.
This is what I did to install ghc:
1) I downloaded GMP.framework and GNUreadline.framework, which
automatically unzipped and placed on my desktop. I then dragged
my mac the
resulting two folders into /Library/Frameworks as per the instructions at:
http://www.haskell.org/ghc/download_ghc_682.html#macosxintel
2) I downloaded ghc-6.8.2
3) I unzipped an untared into /Users/me/my_tar_extractions
4) I cd'ed into the newly created ghc-6.8.2 folder.
5) I read the INSTALL document in the ghc-6.8.2 folder.
6) I ran the command:
$ ./configure
7) Then I ran the command:
$ sudo make install
8) At the end of the install output, I got a message that said: ------------- Installation of ghc-6.8.2 was successful.
To use, add /usr/local/bin to your PATH.
Warning: this binary distribution does NOT contain documentation! --------------
9) I appended /usr/local/bin onto the PATH in ~/.bash_profile.
After unzipping and untaring ghc, should I have put the ghc-6.8.2 folder in /Library/Frameworks like I did with GMP.framework and GNUreadline.framework? Can I still do that?

On 2009 Mar 26, at 11:41, 7stud wrote:
$ 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
My guess is that the binary ghc was compiled to allow for use of private frameworks, so you get an unnecessary warning when you don't have any. Just create ~/Library/Frameworks if you want the warning to go away. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
participants (4)
-
7stud
-
Brandon S. Allbery KF8NH
-
Daniel Fischer
-
Peter Verswyvelen