Hi! I usually use the function 'sized' for this. The function would look something like this:<br><br>myIntGen :: Gen Int<br>myIntGen = sized $ \n -> choose (0,f n)<br><br>where
'f' is a function that uses the size value to generate an upper value
for your random range. I usually use ^2 or sqrt or something like that
depending on my needs.<br>
<br>Hope it helps!<br><br>Best regards, Øystein<br><br><div class="gmail_quote">On Mon, Jul 25, 2011 at 6:31 AM, Mark Spezzano <span dir="ltr"><<a href="mailto:mark.spezzano@chariot.net.au">mark.spezzano@chariot.net.au</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">Hi Kevin,<br>
<br>
Thanks for the response. The first part works well with minor modifications.<br>
<br>
Part 2 is still a bit vague to me. I basically want to "clamp" the Integers generated within the Queue to between 0 and some positive number. At present they're giving me numbers all over the place (specifically negative number)<br>
<br>
Thanks<br>
<font color="#888888"><br>
Mark<br>
</font><div><div></div><div class="h5"><br>
<br>
On 25/07/2011, at 4:44 AM, Kevin Quick wrote:<br>
<br>
> On Sun, 24 Jul 2011 07:30:56 -0700, Mark Spezzano <<a href="mailto:mark.spezzano@chariot.net.au">mark.spezzano@chariot.net.au</a>> wrote:<br>
><br>
>> Hi all,<br>
>><br>
>> I would appreciate it if someone can point me in the right direction with the following problem.<br>
>><br>
>> I'm deliberately implementing a naive Queues packages that uses finite lists as the underlying representation. I've already read through Hughes' paper and the article in The Fun of Programming, but I'm still having some difficulties. Specifically:<br>
>><br>
>> 1. I have a newtype Queue a = Queue [a] and I want to generate Queues of random Integers that are also of random size. How do I do this in QuickCheck? I guess that I need to write a generator and then make my "Queue a" concrete type an instance of Arbitrary? How?<br>
><br>
> Mark,<br>
><br>
> One of the great things about QuickCheck is that it is automatically compositional.<br>
> What I mean by this is that all you need in your instance is how to form a "Queue [a]" given "[a]", because there are already QuickCheck instances for forming lists, and as long as a is pretty standard (Integers is fine) then there's likely an Arbitrary instance for that as well.<br>
><br>
> So (from my head, not actually tested in GHC):<br>
><br>
> import Control.Applicative<br>
> import Test.QuickCheck<br>
><br>
> instance Arbitrary Queue where<br>
> arbitrary = Queue <$> arbitrary<br>
><br>
> Then you can use this as:<br>
><br>
> testProperty "length is something" propQInts<br>
><br>
> propQInts t = length t == ....<br>
> where types = (t :: Queue Integers)<br>
><br>
> The where clause is a fancy way of specifying what the type of t should be without having to express the overall type of propQInts. You could use a more conventional type specification as well.<br>
><br>
>><br>
>> 2. If I wanted to specify/constrain the ranges of random Integers generated, how would I do this?<br>
><br>
> Probably something like this:<br>
><br>
> instance Arbitrary Queue where<br>
> arbitrary = do li <- listOf $ arbitrary<br>
> lr <- liftM $ map rangelimit li<br>
> return $ Queue lr<br>
> where rangelimit n = case (n < LOW, n > HIGH) of<br>
> (True,_) -> LOW<br>
> (_,True) -> HIGH<br>
> _ -> n<br>
><br>
><br>
>><br>
>> 3. If I wanted to specify/constrain the Queue sizes how would I do this?<br>
><br>
> Similar to #2. Perhaps:<br>
><br>
> arbitrary = arbitrary >>= (return . Queue . take CNT . listOf)<br>
><br>
><br>
> --<br>
> -KQ<br>
><br>
> _______________________________________________<br>
> Haskell-Cafe mailing list<br>
> <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
> <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
><br>
><br>
<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Mvh Øystein Kolsrud<br>