question about show -- RWH chapter 5

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

question about show -- RWH chapter 5

7stud-2
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

Reply | Threaded
Open this post in threaded view
|

question about show -- RWH chapter 5

Peter Verswyvelen-2
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 <[hidden email]> wrote:

> 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
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090326/6ff499d7/attachment.htm
Reply | Threaded
Open this post in threaded view
|

question about show -- RWH chapter 5

Daniel Fischer-4
In reply to this post by 7stud-2
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


Reply | Threaded
Open this post in threaded view
|

Re: question about show -- RWH chapter 5

7stud-2
In reply to this post by Peter Verswyvelen-2
Peter Verswyvelen <bugfact <at> gmail.com> writes:
>
> 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.







Reply | Threaded
Open this post in threaded view
|

Re: question about show -- RWH chapter 5

7stud-2
In reply to this post by Daniel Fischer-4
Daniel Fischer <daniel.is.fischer <at> web.de> writes:

> > $ 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?





Reply | Threaded
Open this post in threaded view
|

Re: question about show -- RWH chapter 5

7stud-2
7stud <bbxx789_05ss <at> yahoo.com> writes:
> 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.

Reply | Threaded
Open this post in threaded view
|

question about show -- RWH chapter 5

Brandon S Allbery KF8NH
In reply to this post by 7stud-2
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] [hidden email]
system administrator [openafs,heimdal,too many hats] [hidden email]
electrical and computer engineering, carnegie mellon university    KF8NH


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/beginners/attachments/20090326/7e9891cb/PGP-0001.bin
Reply | Threaded
Open this post in threaded view
|

Re: question about show -- RWH chapter 5

Daniel Fischer-4
In reply to this post by 7stud-2
Am Donnerstag 26 M?rz 2009 17:20:46 schrieb 7stud:
> Daniel Fischer <daniel.is.fischer <at> web.de> writes:
> > > $ 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

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
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?
>