Hi, of course, the easiest way is to write:

1
2
 
let square x = power x 2

.. and honestly, I'd just use this, because it keeps your code readable. There is no shortcut that woud allow witing this more briefly. If you're asking just out of curiosity, then you can also write the following thing:

1
2
3
4
5
6
7
8
9
 
> let switch2 f = (fun x y -> f y x);;
val switch2 : ('a -> 'b -> 'c) -> 'b -> 'a -> 'c

> let square = (switch2 power) 2;;
val square : (int -> int)

> square 10;;
val it : int = 100

The "switch2" function is a higher-order function (i.e. takes a function as an argument). It works only for functions that take two arguments and it returns a function that takes two arguments and call the original function ('f') with these arguments in the opposie order.

But I guess, you'll agree that the first option is just more readable.

By on 11/20/2008 5:41 PM ()

For some reaons, I see many FP(especially haskell) programs prefer the point free form :

let square = flip power 2

By on 11/20/2008 8:26 PM ()

Oh, that's nice; I'll add that to my pervasives lib.

By on 11/22/2008 6:30 PM ()

*smacks forehead*

I missed an 'x'. Thanks for the quick reply. :)

Also thank you for the rest of the info, as well.

By on 11/20/2008 6:02 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper