[Haskell-beginners] Reducing local variable duplication

Michael Orlitzky michael at orlitzky.com
Mon Aug 29 17:35:50 CEST 2011


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)
>       ...




More information about the Beginners mailing list