1

The below is in an F# code snippet where I am trying to remove reliance on the input parameters and perform some sort of function composition:

let h x1 x2 = (*) (exp x1) (sin x2) // how to reduce to point free style

I have tried:

let h1 = (*) exp  sin   
let h2 = exp * sin  
let h3 = ((*) << exp) << sin 

Compiler not happy with the first two and the last doesn't yield the desired result.

I am in fact trying to solve the more general problem of function composition (using whatever tools F# has available) summarised as follows:

f1: float -> float
f2: float -> float
f3: float -> float -> float
How to construct h = f3(f1(x),f2(y)) represented mathematically.
Sam
  • 2,745
  • 3
  • 20
  • 42
  • The following Haskell question apears relevant but is over my head for now: http://stackoverflow.com/questions/16888222/functional-composition-with-multi-valued-functions-in-haskell – Sam May 02 '15 at 11:44
  • 1
    `lambdabot> pl \a b -> (exp a) * (sin b)` output translated back to F#: `let h = ((<<) sin) << (*) << exp` – Mauricio Scheffer May 02 '15 at 11:52
  • @MauricioScheffer Thanks, but that doesn't give the right answer in my test case. – Sam May 02 '15 at 11:57
  • 3
    Hmm, missed a flip in the composition operator. `let h : float -> float -> float = ((>>) sin) << (*) << exp` – Mauricio Scheffer May 02 '15 at 12:11
  • @MauricioScheffer wow that does seem to work, thanks...now to wrap my head around it! – Sam May 02 '15 at 12:14
  • @MauricioScheffer you gotta explain that to me! Specifically what that flipped part before the first "sin" is doing. Or point me to where I can read up about this kinda stuff - not covered in the basic F# books I have read. – Sam May 02 '15 at 12:16
  • 1
    Everything in this example is a `float` which makes understanding a bit harder, i.e. everything is a "thing". Instead, try another example that has different types, then decompose subexpressions as needed to understand what's going on. Ultimately, if you reason it generically (i.e. without concrete examples), the types will lead you to the solution. – Mauricio Scheffer May 02 '15 at 12:23
  • Also, check http://stackoverflow.com/q/24890026/974789 to see some caveats of point-free coding style. – Be Brave Be Like Ukraine May 02 '15 at 13:22
  • This questions sounds very similar to this one http://stackoverflow.com/questions/21636772/how-to-do-pointfree-style-with-long-parameter-list/21643183#21643183. Can you please say is something is missing? – Daniel Fabian May 03 '15 at 04:56

0 Answers0