Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Typeclasses and generic types (Christopher Howard)
2. help understanding JSON (Sean Perry)
3. Complexity of Arrays in Haskell (?ystein Kolsrud)
4. Re: help understanding JSON (Adrien Haxaire)
5. Re: Complexity of Arrays in Haskell (Isaac Dupree)
6. Re: help understanding JSON (Sean Perry)
7. Re: Complexity of Arrays in Haskell (Daniel Fischer)
8. Re: help understanding JSON ([email protected])
----------------------------------------------------------------------
Message: 1
Date: Mon, 09 May 2011 20:01:48 -0800
From: Christopher Howard <[email protected]>
Subject: Re: [Haskell-beginners] Typeclasses and generic types
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On 05/09/2011 07:44 PM, Elvio Toccalino wrote:
> One further addendum to the answer provided by Arlen:
>
> Notice that
>
> instance Eq Point where
> (==) a b = getX a == getX b && getY a == getY b
>
> is defining equality between 'Point a' (as well pointed out by Arlen),
> but it does so in terms of equality test between 'a'... but there's no
> declaration in your code stating that 'a' can be equated.
> This all boils does to the following instance declaration:
>
> instance Eq a => Eq (Point a) where ...
>
> which reads nicely :)
>
Ah... Makes perfect sense now. Thanks! I'm glad I finally got around to
joining this mailing list.
--
frigidcode.com
theologia.indicium.us
------------------------------
Message: 2
Date: Tue, 10 May 2011 00:51:30 -0700
From: Sean Perry <[email protected]>
Subject: [Haskell-beginners] help understanding JSON
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
I have some JSON which looks like this:
{"count":2, "result":[{"LastTrade":"31.24", "Symbol":"FOO"},
{"LastTrade":"345.12", "Symbol":"BAR"}]}
(named testQuoteResult below)
When I try to parse this into types defined below it failed because the Double
is encoded as a String. If I change the type of LastTrade in my code to String
everything is ok but I am left read'ing the String.
I have defined the following using Language.JsonGrammar:
import Data.Iso
import Language.JsonGrammar
import Prelude hiding (id, (.), head, either)
import Control.Category ((.))
import qualified Data.Aeson as JS
import qualified Data.ByteString.Char8 as BS
import qualified Data.Attoparsec as P
data MyQuote = MyQuote { price :: Double,
symbol :: String }
deriving(Show)
myQuote = $(deriveIsos ''MyQuote)
instance Json MyQuote where
grammar = myQuote . object
(
prop "LastTrade"
. prop "Symbol"
)
data MyQuoteResult = MyQuoteResult { count :: Int,
result
:: [MyQuote] }
deriving(Show)
myQuoteResult = $(deriveIsos ''MyQuoteResult)
instance Json MyQuoteResult where
grammar = myQuoteResult . object
( prop "count"
. prop "result"
)
tryJSON input =
case P.parse JS.json (BS.pack input) of
P.Done s v -> Right v
other -> Left ("failed parse " ++ show other)
Using it like this:
> let Right qr = tryJSON testQuoteResult
> fromJson qr :: Maybe MyQuoteResult
Nothing
Is there some way to convince the JSON text parser to turn "12.34" into (Double
12.34)??
An example using pure aeson would be fine if required. I just decided to play
with JsonGrammar since it was announced today and it sounded interesting.
------------------------------
Message: 3
Date: Tue, 10 May 2011 09:51:21 +0200
From: ?ystein Kolsrud <[email protected]>
Subject: [Haskell-beginners] Complexity of Arrays in Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi! What's the complexity of indexing arrays in Haskell? The
Data.Array documentation doesn't seem to mention complexity of any of
its functions, and it's not obvious to me how to implement an array
data structure with indexing being O(1).
--
Mvh ?ystein Kolsrud
------------------------------
Message: 4
Date: Tue, 10 May 2011 10:05:57 +0200
From: Adrien Haxaire <[email protected]>
Subject: Re: [Haskell-beginners] help understanding JSON
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
Hello,
I do not have the right answer for your question, I am just starting to
learn Haskell.
Still, you may want to take a look at the JSON library example
described in real world Haskell, this can bring you some more clues:
http://book.realworldhaskell.org/read/writing-a-library-working-with-json-data.html
Regards,
Adrien
On Tue, 10 May 2011 00:51:30 -0700, Sean Perry wrote:
> I have some JSON which looks like this:
>
> {"count":2, "result":[{"LastTrade":"31.24", "Symbol":"FOO"},
> {"LastTrade":"345.12",
> "Symbol":"BAR"}]}
> (named testQuoteResult below)
>
> When I try to parse this into types defined below it failed because
> the Double is encoded as a String. If I change the type of LastTrade
> in my code to String everything is ok but I am left read'ing the
> String.
>
> I have defined the following using Language.JsonGrammar:
>
> import Data.Iso
> import Language.JsonGrammar
> import Prelude hiding (id, (.), head, either)
> import Control.Category ((.))
>
> import qualified Data.Aeson as JS
> import qualified Data.ByteString.Char8 as BS
> import qualified Data.Attoparsec as P
>
> data MyQuote = MyQuote { price :: Double,
> symbol :: String }
> deriving(Show)
>
> myQuote = $(deriveIsos ''MyQuote)
>
> instance Json MyQuote where
> grammar = myQuote . object
> (
> prop "LastTrade"
> . prop "Symbol"
> )
>
> data MyQuoteResult = MyQuoteResult { count :: Int,
>
> result :: [MyQuote] }
> deriving(Show)
>
> myQuoteResult = $(deriveIsos ''MyQuoteResult)
>
> instance Json MyQuoteResult where
> grammar = myQuoteResult . object
> ( prop "count"
> . prop "result"
> )
>
> tryJSON input =
> case P.parse JS.json (BS.pack input) of
> P.Done s v -> Right v
> other -> Left ("failed parse " ++ show other)
>
> Using it like this:
>> let Right qr = tryJSON testQuoteResult
>> fromJson qr :: Maybe MyQuoteResult
> Nothing
>
> Is there some way to convince the JSON text parser to turn "12.34"
> into (Double 12.34)??
>
> An example using pure aeson would be fine if required. I just decided
> to play with JsonGrammar since it was announced today and it sounded
> interesting.
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 5
Date: Tue, 10 May 2011 04:22:28 -0400
From: Isaac Dupree <[email protected]>
Subject: Re: [Haskell-beginners] Complexity of Arrays in Haskell
To: ?ystein Kolsrud <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 05/10/11 03:51, ?ystein Kolsrud wrote:
> Hi! What's the complexity of indexing arrays in Haskell? The
> Data.Array documentation doesn't seem to mention complexity of any of
> its functions, and it's not obvious to me how to implement an array
> data structure with indexing being O(1).
Compilers implement special representation of arrays (an array in
memory, rather as you might expect), so indexing is O(1). The price you
pay is that array *updates* are O(n) even if you're only changing one
element. It has to copy the whole thing in order to leave the original
unmodified.
(There are also mutable arrays that are more annoyingly imperative to
use, Data.Map which has good enough performance for a lot of purposes, etc.)
------------------------------
Message: 6
Date: Tue, 10 May 2011 01:41:40 -0700
From: Sean Perry <[email protected]>
Subject: Re: [Haskell-beginners] help understanding JSON
To: Adrien Haxaire <[email protected]>
Cc: "<[email protected]>" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Thanks Adrien. In that chapter they actually explicitly define all JS numbers
to be Double and later in the Parsec chapter their parser reads the Strings
into Floats directly. I was looking for a way to use the library provided
parsers rather than crafting my own. Sure it is not much code but why rewrite
what others have already tested?
Mostly this boils down to a complete lack of useful docs. A laundry list of
functions is not always helpful. At least we could mimic the Python docs and
add examples.
On May 10, 2011, at 1:05, Adrien Haxaire <[email protected]> wrote:
> Hello,
>
> I do not have the right answer for your question, I am just starting to learn
> Haskell.
>
> Still, you may want to take a look at the JSON library example described in
> real world Haskell, this can bring you some more clues:
>
> http://book.realworldhaskell.org/read/writing-a-library-working-with-json-data.html
>
> Regards,
> Adrien
>
>
> On Tue, 10 May 2011 00:51:30 -0700, Sean Perry wrote:
>> I have some JSON which looks like this:
>>
>> {"count":2, "result":[{"LastTrade":"31.24", "Symbol":"FOO"},
>> {"LastTrade":"345.12", "Symbol":"BAR"}]}
>> (named testQuoteResult below)
>>
>> When I try to parse this into types defined below it failed because
>> the Double is encoded as a String. If I change the type of LastTrade
>> in my code to String everything is ok but I am left read'ing the
>> String.
>>
>> I have defined the following using Language.JsonGrammar:
>>
>> import Data.Iso
>> import Language.JsonGrammar
>> import Prelude hiding (id, (.), head, either)
>> import Control.Category ((.))
>>
>> import qualified Data.Aeson as JS
>> import qualified Data.ByteString.Char8 as BS
>> import qualified Data.Attoparsec as P
>>
>> data MyQuote = MyQuote { price :: Double,
>> symbol :: String }
>> deriving(Show)
>>
>> myQuote = $(deriveIsos ''MyQuote)
>>
>> instance Json MyQuote where
>> grammar = myQuote . object
>> (
>> prop "LastTrade"
>> . prop "Symbol"
>> )
>>
>> data MyQuoteResult = MyQuoteResult { count :: Int,
>>
>> result :: [MyQuote] }
>> deriving(Show)
>>
>> myQuoteResult = $(deriveIsos ''MyQuoteResult)
>>
>> instance Json MyQuoteResult where
>> grammar = myQuoteResult . object
>> ( prop "count"
>> . prop "result"
>> )
>>
>> tryJSON input =
>> case P.parse JS.json (BS.pack input) of
>> P.Done s v -> Right v
>> other -> Left ("failed parse " ++ show other)
>>
>> Using it like this:
>>> let Right qr = tryJSON testQuoteResult
>>> fromJson qr :: Maybe MyQuoteResult
>> Nothing
>>
>> Is there some way to convince the JSON text parser to turn "12.34"
>> into (Double 12.34)??
>>
>> An example using pure aeson would be fine if required. I just decided
>> to play with JsonGrammar since it was announced today and it sounded
>> interesting.
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 7
Date: Tue, 10 May 2011 10:45:23 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Complexity of Arrays in Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="iso-8859-1"
On Tuesday 10 May 2011 09:51:21, ?ystein Kolsrud wrote:
> Hi! What's the complexity of indexing arrays in Haskell? The
> Data.Array documentation doesn't seem to mention complexity of any of
> its functions, and it's not obvious to me how to implement an array
> data structure with indexing being O(1).
For the array types provided by the array package (Array, UArray, STArray,
STUArray, IOArray, IOUArray), indexing is O(1) [for reasonable index
types?].
Implementing such a data structure with O(1) indexing of course requires to
go low-level, for the payload, you need a contiguous chunk of memory where
either the pointers to the data are stored (boxed arrays) or the raw data
themselves (unboxed arrays). Then indexing is "read size bytes from address
(start + index*size)", just like in C or similar. Of course there's bounds-
checking and computing the effective index from the programmer-visible, but
for reasonable index types, that is O(1) too.
? You could of course make an Ix instance for e.g. lazy Peano numbers, then
indexing would become O(index).
------------------------------
Message: 8
Date: Tue, 10 May 2011 14:55:41 +0530
From: "[email protected]" <[email protected]>
Subject: Re: [Haskell-beginners] help understanding JSON
To: Sean Perry <[email protected]>
Cc: "<[email protected]>" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
>Mostly this boils down to a complete lack of useful docs
True, I faced problems with respect to functions in System.Posix.IO because
of lack of proper docs.
On Tue, May 10, 2011 at 2:11 PM, Sean Perry <[email protected]> wrote:
> Thanks Adrien. In that chapter they actually explicitly define all JS
> numbers to be Double and later in the Parsec chapter their parser reads the
> Strings into Floats directly. I was looking for a way to use the library
> provided parsers rather than crafting my own. Sure it is not much code but
> why rewrite what others have already tested?
>
> Mostly this boils down to a complete lack of useful docs. A laundry list of
> functions is not always helpful. At least we could mimic the Python docs
> and add examples.
>
> On May 10, 2011, at 1:05, Adrien Haxaire <[email protected]> wrote:
>
> > Hello,
> >
> > I do not have the right answer for your question, I am just starting to
> learn Haskell.
> >
> > Still, you may want to take a look at the JSON library example described
> in real world Haskell, this can bring you some more clues:
> >
> >
> http://book.realworldhaskell.org/read/writing-a-library-working-with-json-data.html
> >
> > Regards,
> > Adrien
> >
> >
> > On Tue, 10 May 2011 00:51:30 -0700, Sean Perry wrote:
> >> I have some JSON which looks like this:
> >>
> >> {"count":2, "result":[{"LastTrade":"31.24", "Symbol":"FOO"},
> >> {"LastTrade":"345.12",
> "Symbol":"BAR"}]}
> >> (named testQuoteResult below)
> >>
> >> When I try to parse this into types defined below it failed because
> >> the Double is encoded as a String. If I change the type of LastTrade
> >> in my code to String everything is ok but I am left read'ing the
> >> String.
> >>
> >> I have defined the following using Language.JsonGrammar:
> >>
> >> import Data.Iso
> >> import Language.JsonGrammar
> >> import Prelude hiding (id, (.), head, either)
> >> import Control.Category ((.))
> >>
> >> import qualified Data.Aeson as JS
> >> import qualified Data.ByteString.Char8 as BS
> >> import qualified Data.Attoparsec as P
> >>
> >> data MyQuote = MyQuote { price :: Double,
> >> symbol :: String }
> >> deriving(Show)
> >>
> >> myQuote = $(deriveIsos ''MyQuote)
> >>
> >> instance Json MyQuote where
> >> grammar = myQuote . object
> >> (
> >> prop "LastTrade"
> >> . prop "Symbol"
> >> )
> >>
> >> data MyQuoteResult = MyQuoteResult { count :: Int,
> >>
> >> result :: [MyQuote] }
> >> deriving(Show)
> >>
> >> myQuoteResult = $(deriveIsos ''MyQuoteResult)
> >>
> >> instance Json MyQuoteResult where
> >> grammar = myQuoteResult . object
> >> ( prop "count"
> >> . prop "result"
> >> )
> >>
> >> tryJSON input =
> >> case P.parse JS.json (BS.pack input) of
> >> P.Done s v -> Right v
> >> other -> Left ("failed parse " ++ show other)
> >>
> >> Using it like this:
> >>> let Right qr = tryJSON testQuoteResult
> >>> fromJson qr :: Maybe MyQuoteResult
> >> Nothing
> >>
> >> Is there some way to convince the JSON text parser to turn "12.34"
> >> into (Double 12.34)??
> >>
> >> An example using pure aeson would be fine if required. I just decided
> >> to play with JsonGrammar since it was announced today and it sounded
> >> interesting.
> >>
> >>
> >> _______________________________________________
> >> Beginners mailing list
> >> [email protected]
> >> http://www.haskell.org/mailman/listinfo/beginners
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Regards
Lakshmi Narasimhan T V
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110510/5c20efd5/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 35, Issue 17
*****************************************