I was playing around with a simple function for someone else's Stack Overflow question, and wrote the expression:
f a x ++ f a y
Obviously this is the best way to write that expression in real life, given I have all those variables in scope anyway, but I saw the duplication of f a
, and thought "Hey, maybe you can remove that with the Applicative instance for functions". I wound up with:
liftA2 (++) (flip f x) (flip f y) a
which is just awful. Is there some nicer way to remove this duplication?Obviously I could also remove the duplication by binding f a
to something in a where
clause, but this was intended as an exercise in using built-in functions.