views:

293

answers:

3

I'm just starting up with F# and see how you can use currying to pre-load the 1st parameter to a function. But how would one do it with the 2nd, 3rd, or whatever other parameter? Would named parameters to make this easier? Are there any other functional languages that have named parameters or some other way to make currying indifferent to parameter-order?

+7  A: 

Typically you just use a lambda:

fun x y z -> f x y 42

is a function like 'f' but with the third parameter bound to 42.

You can also use combinators (like someone mentioned Haskell's "flip" in a comment), which reorder arguments, but I sometimes find that confusing.

Note that most curried functions are written so that the argument-most-likely-to-be-partially-applied comes first.

F# has named parameters for methods (not let-bound function values), but the names apply to 'tupled' parameters. Named curried parameters do not make much sense; if I have a two-argument curried function 'f', I would expect that given

let g = f
let h x y = f x y

then 'g' or 'h' would be substitutable for 'f', but 'named' parameters make this not necessarily true. That is to say, 'named parameters' can interact poorly with other aspects of the language design, and I personally don't know of a good design offhand for 'named parameters' that interacts well with 'first class curried function values'.

Brian
+3  A: 

OCaml, the language that F# was based on, has labeled (and optional) arguments that can be specified in any order, and you can partially apply a function based on those arguments' names. I don't believe F# has this feature.

You might try creating something like Haskell's flip function. Creating variants that jump the argument further in the argument list shouldn't be too hard.

let flip f a b = f b a
let flip2 f a b c = f b c a
let flip3 f a b c d = f b c d a
Chuck
F# has optional named arguments, but they're only for `member` definitions (not for `let` definitions), and they cannot be curried that way (all arguments you don't specify take on their default values, and you cannot omit non-optional ones).
Pavel Minaev
+1  A: 

In Python, you can use functools.partial, or a lambda. Python has named arguments. functools.partial can be used to specify the first positional arguments as well as any named argument.

from functools import partial

def foo(a, b, bar=None):
    ...

f = partial(foo, bar='wzzz') # f(1, 2) ~ foo(1, 2, bar='wzzz')
f2 = partial(foo, 3)         # f2(5) ~ foo(3, 5)

f3 = lambda a: foo(a, 7)     # f3(9) ~ foo(9, 7)
codeape