
Can application of an expression (to a function) be treated like a function itself? In my specific case, I started with this expression: code: -------- (cos b * velocityC, sin b * velocityC) -------- But I have a compulsive hatred of duplication. I happened to have this function handy called appPair: code: ------- appPair f (a, b) = (f a, f b) appPair (* velocityC) (cos b, sin b) ------- Better, but the b identifier is still duplicated. Is there some way I could have... code: -------- appPair (?) (cos, sin) -------- ...shifting the application of b into the (* velocityC) expression, without modifying my appPair function? -- frigidcode.com

On 12/01/2012 01:31 PM, Christopher Howard wrote:
Can application of an expression (to a function) be treated like a function itself? In my specific case, I started with this expression:
code: -------- (cos b * velocityC, sin b * velocityC) --------
But I have a compulsive hatred of duplication. I happened to have this function handy called appPair:
code: ------- appPair f (a, b) = (f a, f b)
appPair (* velocityC) (cos b, sin b) -------
Better, but the b identifier is still duplicated. Is there some way I could have...
code: -------- appPair (?) (cos, sin) --------
...shifting the application of b into the (* velocityC) expression, without modifying my appPair function?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Wait, think I got it... code: -------- appPair (\x -> (x $ b) * velocityC) (cos, sin) -------- -- frigidcode.com

Hi Christopher, On Sat, Dec 01, 2012 at 01:39:22PM -0900, Christopher Howard wrote:
(cos b * velocityC, sin b * velocityC)
vs.
appPair (\x -> (x $ b) * velocityC) (cos, sin)
Duplication isn't always a bad thing. Programming in Haskell can be quite tempting for code golfing. The first version is simpler, shorter and even understandable for non Haskellers. Sometimes the desire to look smart can result in quite horrible code. Greetings, Daniel

A short derivation: An obvious solution is: Use a lambda abstraction: appPair (\x -> (x b) * velocityC) (cos,sin) or (using function composition) appPair ((* velocityC) . (\x -> x b))(cos,sin) or (explictly using the application operator $): appPair ((* velocityC) . (\x -> x $ b))(cos,sin) Now the lambda abstraction can be removed appPair ((* velocityC ) . ($ b)) (cos,sin) Regards, David Am 01.12.2012 23:31, schrieb Christopher Howard:
Can application of an expression (to a function) be treated like a function itself? In my specific case, I started with this expression:
code: -------- (cos b * velocityC, sin b * velocityC) --------
But I have a compulsive hatred of duplication. I happened to have this function handy called appPair:
code: ------- appPair f (a, b) = (f a, f b)
appPair (* velocityC) (cos b, sin b) -------
Better, but the b identifier is still duplicated. Is there some way I could have...
code: -------- appPair (?) (cos, sin) --------
...shifting the application of b into the (* velocityC) expression, without modifying my appPair function?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Sun, Dec 2, 2012 at 5:31 AM, Christopher Howard < christopher.howard@frigidcode.com> wrote:
But I have a compulsive hatred of duplication.
And thus haskell code golfing is born! Actually that's not always true. The motivation is sometimes compellingly noble. E.g. we want a faithfully compact translation of (English!) "Given some number compute both its sine and cosine, both scaled by velocityC." into import Control.Arrow( (***), (&&&) ) import Control.Monad( join ) f = join (***) (velocityC*) . (sin &&& cos) Unfamiliarity with symbol-rich combinators has been known to exact cognitive pain, leading to unfortunate comparisons with Perl. Evidently, the story isn't always driven by sadism. Credits: I asked a similar question years ago [1] and got a heap of useful replies. Special mention to Robert Vollmert and Luke Palmer for help arriving at the above version. [1] http://www.haskell.org/pipermail/haskell-cafe/2009-February/056195.html -- Kim-Ee On Sun, Dec 2, 2012 at 5:31 AM, Christopher Howard < christopher.howard@frigidcode.com> wrote:
Can application of an expression (to a function) be treated like a function itself? In my specific case, I started with this expression:
code: -------- (cos b * velocityC, sin b * velocityC) --------
But I have a compulsive hatred of duplication. I happened to have this function handy called appPair:
code: ------- appPair f (a, b) = (f a, f b)
appPair (* velocityC) (cos b, sin b) -------
Better, but the b identifier is still duplicated. Is there some way I could have...
code: -------- appPair (?) (cos, sin) --------
...shifting the application of b into the (* velocityC) expression, without modifying my appPair function?
-- frigidcode.com
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (4)
-
Christopher Howard
-
Daniel Trstenjak
-
David Sabel
-
Kim-Ee Yeoh