Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  edit-compile-test loop (Roel van Dijk)
   2. Re:  Re: \x -> x < 0.5 && x > -0.5 (Roel van Dijk)
   3. Re:  edit-compile-test loop (Magnus Therning)
   4.  Re: \x -> x < 0.5 && x > -0.5 (Jordan Cooper)
   5. Re:  Re: \x -> x < 0.5 && x > -0.5 (Daniel Fischer)
   6.  Re: parsing upto n items with parsec (Ashish Agarwal)
   7. Re:  Re: \x -> x < 0.5 && x > -0.5 (aditya siram)


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

Message: 1
Date: Mon, 19 Oct 2009 13:25:20 +0200
From: Roel van Dijk <vandijk.r...@gmail.com>
Subject: Re: [Haskell-beginners] edit-compile-test loop
To: Joe Fredette <jfred...@gmail.com>
Cc: Haskell Beginners <beginners@haskell.org>
Message-ID:
        <ab9dc6fa0910190425q310725far428617090aab4...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

> [1] right now I use my own little hacked up testrunning script (just loads
> the stuff in ghci, and I have one function that sits in a base "test"
> directory, underwhich the project module hierarchy is mirrored. So if I
> have, as I do now, Text/HWN/Parser/HWN.hs, then I also have
> Test/Text/HWN/Parser/HWN_Test.hs. in the Test directory I have
> Test_Harness.hs, which has a main function which runs all the tests in the
> directories below it. Each directory has a similar Test_Harness.hs (though
> all the names are different) which loads all the tests of the directories
> below it, and exports a single test group. All of this is manual, though
> I've been planning a kind of manager thing for it lately. But thats a story
> for another day.

I found the test-framework package to be quite useful for this.

http://batterseapower.github.com/test-framework/

Using the defaultMain function you can easily define a small program
which will run all your tests (quickcheck, hunit or other). The only
problem I had with test-framework is that it prints the test results
in nice colours. It looks pretty in a console but unreadable in ghci.

Hackage seems to be down at the moment, but you can check out which
other packages use test-framework on my hackage plaything:

http://bifunctor.homelinux.net/~roel/cgi-bin/hackage-scripts/revdeps/test-framework

Roel


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

Message: 2
Date: Mon, 19 Oct 2009 13:38:15 +0200
From: Roel van Dijk <vandijk.r...@gmail.com>
Subject: Re: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5
To: beginners@haskell.org
Message-ID:
        <ab9dc6fa0910190438r160c7daav114fceb75e70c...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Or simply write a function:

between :: Ord a => a -> a -> a -> Bool
between min max x = x > min && x < max

filter (between (-0.5) 0.5) xs

Keep it simple, stupid?


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

Message: 3
Date: Mon, 19 Oct 2009 14:33:36 +0100
From: Magnus Therning <mag...@therning.org>
Subject: Re: [Haskell-beginners] edit-compile-test loop
To: Roel van Dijk <vandijk.r...@gmail.com>
Cc: Haskell Beginners <beginners@haskell.org>
Message-ID:
        <e040b520910190633x758632cepb6cc0ff783a4e...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Mon, Oct 19, 2009 at 12:25 PM, Roel van Dijk <vandijk.r...@gmail.com> wrote:
>> [1] right now I use my own little hacked up testrunning script (just loads
>> the stuff in ghci, and I have one function that sits in a base "test"
>> directory, underwhich the project module hierarchy is mirrored. So if I
>> have, as I do now, Text/HWN/Parser/HWN.hs, then I also have
>> Test/Text/HWN/Parser/HWN_Test.hs. in the Test directory I have
>> Test_Harness.hs, which has a main function which runs all the tests in the
>> directories below it. Each directory has a similar Test_Harness.hs (though
>> all the names are different) which loads all the tests of the directories
>> below it, and exports a single test group. All of this is manual, though
>> I've been planning a kind of manager thing for it lately. But thats a story
>> for another day.
>
> I found the test-framework package to be quite useful for this.
>
> http://batterseapower.github.com/test-framework/
>
> Using the defaultMain function you can easily define a small program
> which will run all your tests (quickcheck, hunit or other). The only
> problem I had with test-framework is that it prints the test results
> in nice colours. It looks pretty in a console but unreadable in ghci.
>
> Hackage seems to be down at the moment, but you can check out which
> other packages use test-framework on my hackage plaything:
>
> http://bifunctor.homelinux.net/~roel/cgi-bin/hackage-scripts/revdeps/test-framework

Yes, I've found this useful as well.  I'm surprised it's not possible
to turn off the colourisation... but `my-test --help` doesn't seem to
offer any option for it.  Maybe that's a good thing to add?  ;-)

/M

-- 
Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus@therning.org          Jabber: magnus@therning.org
http://therning.org/magnus         identi.ca|twitter: magthe


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

Message: 4
Date: Mon, 19 Oct 2009 08:24:12 -0700
From: Jordan Cooper <nefi...@gmail.com>
Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5
To: beginners@haskell.org
Message-ID:
        <301488c50910190824w43dde17cta55ac778f7a46...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Whoa... how on earth does this work? How does it interpret the
sections as Reader monads?

> That's a job for the reader monad.
>
>
> Lambda Fu, form 53 - silent reader of truth
>
>     import Control.Monad
>     import Control.Monad.Reader
>
>     filter (liftM2 (&&) (< 0.5) (> -0.5)) xs
>
>
>
> Regards,
> apfelmus


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

Message: 5
Date: Mon, 19 Oct 2009 17:58:25 +0200
From: Daniel Fischer <daniel.is.fisc...@web.de>
Subject: Re: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5
To: beginners@haskell.org
Cc: Jordan Cooper <nefi...@gmail.com>
Message-ID: <200910191758.26174.daniel.is.fisc...@web.de>
Content-Type: text/plain;  charset="iso-8859-1"

Am Montag 19 Oktober 2009 17:24:12 schrieb Jordan Cooper:
> Whoa... how on earth does this work? How does it interpret the
> sections as Reader monads?

liftM2 (&&) :: (Monad m) => m Bool -> m Bool -> m Bool

So for (liftM2 (&&) (< 0.5) (> -0.5)) to be well typed, we must have
(< 0.5) :: m Bool
for some Monad m (same for (> -0.5)).

Now (< 0.5) :: Fractional a => a -> Bool, hence m must be ((->) a) for some a 
in the 
Fractional class.

Reader r a is just (r -> a) wrapped in a newtype, so (r -> a) 'is' the reader 
monad.

To use it, we must import Control.Monad for liftM2 and some module which brings 
the

instance Monad ((->) r)

into scope. That could be Control.Monad.Instances or, appropriately, 
Control.Monad.Reader.

>
> > That's a job for the reader monad.
> >
> >
> > Lambda Fu, form 53 - silent reader of truth
> >
> >     import Control.Monad
> >     import Control.Monad.Reader
> >
> >     filter (liftM2 (&&) (< 0.5) (> -0.5)) xs
> >
> >
> >
> > Regards,
> > apfelmus
>




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

Message: 6
Date: Mon, 19 Oct 2009 12:22:48 -0400
From: Ashish Agarwal <agarwal1...@gmail.com>
Subject: [Haskell-beginners] Re: parsing upto n items with parsec
To: Christian Maeder <christian.mae...@dfki.de>
Cc: beginners@haskell.org
Message-ID:
        <d8be5ae20910190922n2e41b5c0je2aa7d9608e86...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Thanks. So I tried
upTo n p =
    liftM2 (:) p (upTo (n-1) p)

which doesn't quite work because the recursion does not have a base case.
The following gets me closer:

upTo n p =
    if n <= 0 then
        return []
    else
        liftM2 (:) p (upTo (n-1) p)

> parse (upTo 3 digit) "" "123ab"
Right "123"

However:

> parse (upTo 4 digit) "" "123ab"
Left (line 1, column 4):
unexpected "a"
expecting digit

The semantics of (upTo n p) should be to parse at most n tokens, but if less
than n tokens are available that should still be a successful parse. And the
next token should be the first one upTo failed on.

I attempted to use the "try" parser in various locations but that doesn't
seem to help, or maybe I'm using it incorrectly.


On Sat, Oct 17, 2009 at 5:15 AM, Christian Maeder
<christian.mae...@dfki.de>wrote:

> Ashish Agarwal schrieb:
> > Hi. I'm just learning Parsec and Haskell. It is a great library, and I
> > see how "many" lets you parse 0 or more items, "many1" parses 1 or more
> > items, and "count" parses exactly n items. However, there is no
> > combinator that parses between m and n items. What would be the best
> > implementation for this?
>
> I would write an "upTo" parser with the same type as "count" that parses
> not exactly but at most n items. Your desired parser is than the
> concatenated results of "count m p" and "upTo (n - m) p" (achieved by
> "liftM2 (++)").
>
> For "upTo" a recursive definition seems best (other may come up with
> tricky combinator application.) "upTo 0 p" (or something less than 0)
> returns "[]" and "upTo n p" is an "option [] ..." parser of one "p"
> result followed by the "upTo (n - 1) p" result:
>
> "option [] (liftM2 (:) p (upTo (n - 1) p))"
>
> HTH Christian
>
> Another possibility is to use "many" and check if the resulting list has
> the desired length (if not fail), but that may consume too many tokens
> that subsequent parsers are supposed to consume.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091019/305d5640/attachment-0001.html

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

Message: 7
Date: Mon, 19 Oct 2009 11:42:01 -0500
From: aditya siram <aditya.si...@gmail.com>
Subject: Re: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5
To: Jordan Cooper <nefi...@gmail.com>
Cc: beginners@haskell.org
Message-ID:
        <594f78210910190942n3f068682l77f1e3b5437f4...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

This one had me puzzled too - so did a traced through the program below.
Please make sure your viewing window is at least this wide:
<---------------------------------------------------------------->

It was more helpful to think of this as using the ((->) r) instance of
Monad. It is defined in ./libraries/base/Control/Monad/Instances.hs in your
GHC source as:
>instance Monad ((->) r) where
>    return = const
>    f >>= k = \ r -> k (f r) r

And const just returns its first argument like:
const 1 3 => 1
const "hello" "world" => "hello"

And liftM2 is defined in ./libraries/base/Control/Monad.hs as :
>liftM2  :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
>liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) }

So a trace of the program goes like this:

(liftM2 (&&) (< 0.5) (> -0.5))
=> do {x1 <- (< 0.5);
       x2 <- (> -0.5);
       return ((&&) x1 x2)}

=> (< 0.5) >>= \x1
   (> -0.5) >>= \x2
   return ((&&) x1 x2)

=> \r ->(\x1 ->
            (> -0.5) >>= \x2
            return ((&&) x1 x2))
        ((< 0.5) r)
        r

=> \r -> (return (> -0.5) >>= \x2
          return ((&&) ((< 0.5) r) x2))
          r

=> \r -> (\r' -> (\x2 ->
                      return ((&&) (const (< 0.5) r) x2))
                 ((> -0.5) r')
                 r')
         r
=> \r -> (\r' -> (return ((&&) ((< 0.5) r) ((> -0.5) r'))) r') r
=> \r -> (\r' -> (const ((&&) ((< 0.5) r) ((> -0.5) r'))) r') r
=> \r -> (\r' -> ((&&) ((< 0.5) r) ((> -0.5) r'))) r
=> \r -> (\r' -> ((r < 0.5) && (r' > -0.5))) r

hope this helps,
-deech

On Mon, Oct 19, 2009 at 10:24 AM, Jordan Cooper <nefi...@gmail.com> wrote:

> Whoa... how on earth does this work? How does it interpret the
> sections as Reader monads?
>
> > That's a job for the reader monad.
> >
> >
> > Lambda Fu, form 53 - silent reader of truth
> >
> >     import Control.Monad
> >     import Control.Monad.Reader
> >
> >     filter (liftM2 (&&) (< 0.5) (> -0.5)) xs
> >
> >
> >
> > Regards,
> > apfelmus
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091019/578ea6af/attachment.html

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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 16, Issue 13
*****************************************

Reply via email to