3
\$\begingroup\$

I've written the following code:

{- ...
   ...
   ... -}

data Ellipsoid
data Halfplane
data Intersection

data PointSet s a where
    Halfplane :: RealFrac a =>
                 a -> a -> a -> (a -> a -> Bool) -> a ->
                 PointSet Halfplane a
    Ellipsoid :: RealFrac a =>
                 a -> a -> a -> (a -> a -> Bool) -> a ->
                 PointSet Ellipsoid a
    Intersection :: [PointSet s a] -> PointSet Intersection a

type TestFunc a = RealFrac a => (a -> a -> a ->  Bool)

test :: (Show  a) => PointSet t a -> TestFunc a
test (Ellipsoid a b c f r) = f'
                       where f' z y x = ((x/a)^2 + (y/b)^2 + (z/c)^2) `f` r
test (Halfplane a b c f t) = f'
                       where f' z y x = (a*x + b*y + c*z) `f` t
test (Intersection ps) =     f'
        where f' z y x = and . map ($ x) $ map ($ y) $ map ($ z) ts
              ts = map test ps

In particular I'd like to have input on f' z y x = and . map ($ x) $ map ($ y) $ map ($ z) ts, whether there is a way to express this more succinctly.

Of course, I'm happy about any other comment as well.

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

The expression

and . map ($ x) . map ($ y) . map ($ z) . map test $ ps

contains several map calls connected with .. But since map f . map g === map (f . g), we can simplify it as

and . map (($ x) . ($ y) . ($ z) . test) $ ps

or more concisely

and . map (\g -> test g x y z) $ ps

Also and . map p can be simplified by all, so finally we get

all (\g -> test g x y z) ps

Just one more note, please post code that compiles, especially for CR, it's much easier to work with; see http://sscce.org/

\$\endgroup\$
1
  • \$\begingroup\$ Thank you! (So at least I had the right feeling that this is simplifiable. The simplified version is also more readable. Exactly what I wanted. I have to learn more identities. -- And thanks for the pointers, I'll bear SSCCE in mind for future posts! (in this case it was only the language extensions, but I see how this must have been annoying). \$\endgroup\$
    – A Sz
    Jun 26, 2014 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.