[Haskell-beginners] Reducing local variable duplication

Michael Xavier nemesisdesign at gmail.com
Mon Aug 29 19:53:08 CEST 2011


The where block takes precedence over the global scope so if these points
are generally reused for most tests and only deviate in a few, you could
define them globally to the file and then modify individual ones in the
where blocks of code that needs different values.

Hope that helps

On Mon, Aug 29, 2011 at 8:35 AM, Michael Orlitzky <michael at orlitzky.com>wrote:

> I have a series of unit tests like this,
>
> > test_volume1 :: Assertion
> > test_volume1 =
> >   assertEqual "volume is correct" True (vol ~= (-1/3))
> >   where
> >     p0 = (0, -0.5, 0)
> >     p1 = (0, 0.5, 0)
> >     p2 = (2, 0, 0)
> >     p3 = (1, 0, 1)
> >     ...
> >
> > test_volume2 :: Assertion
> > test_volume2 =
> >   assertEqual "volume is correct" True (vol ~= (1/3))
> >   where
> >     p0 = (0, -0.5, 0)
> >     p1 = (2, 0, 0)
> >     p2 = (0, 0.5, 0)
> >     p3 = (1, 0, 1)
> >     ...
>
>
> I would like to de-duplicate some of the values in the 'where' clause.
> Furthermore, I don't want to declare e.g. p0 and p3 at the top level,
> since later tests may need different values of p0,p3. What I would
> really like is something like the following. Is it possible?
>
> > let p0 = (0, -0.5, 0)
> >     p3 = (1, 0, 1)
> > in
> >   test_volume1 :: Assertion
> >   test_volume1 =
> >     assertEqual "volume is correct" True (vol ~= (-1/3))
> >     where
> >       p1 = (0, 0.5, 0)
> >       p2 = (2, 0, 0)
> >       ...
> >
> >   test_volume2 :: Assertion
> >   test_volume2 =
> >     assertEqual "volume is correct" True (vol ~= (1/3))
> >     where
> >       p1 = (2, 0, 0)
> >       p2 = (0, 0.5, 0)
> >       ...
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
Michael Xavier
http://www.michaelxavier.net
LinkedIn <http://www.linkedin.com/pub/michael-xavier/13/b02/a26>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110829/46c01c07/attachment.htm>


More information about the Beginners mailing list