Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a programming language that performs currying when named parameters are omitted?

Many functional programming languages have support for curried parameters. To support currying functions the parameters to the function are essentially a tuple where the last parameter can be omitted making a new function requiring a smaller tuple.

I'm thinking of designing a language that always uses records (aka named parameters) for function parameters.

Thus simple math functions in my make believe language would be:

add { left : num, right : num } = ...
minus { left : num, right : num } = ..

You can pass in any record to those functions so long as they have those two named parameters (they can have more just "left" and "right").

If they have only one of the named parameter it creates a new function:

minus5 :: { left : num } -> num
minus5 = minus { right : 5 }

I borrow some of haskell's notation for above.

Has any one seen a language that does this?

like image 592
Adam Gent Avatar asked Jun 10 '10 13:06

Adam Gent


People also ask

When would you use a currying function?

Currying is helpful when you have to frequently call a function with a fixed argument. Considering, for example, the following function: If we want to define the function error , warn , and info , for every type, we have two options. Currying provides a shorter, concise, and more readable solution.

Why do we use currying in JavaScript?

Currying is a checking method to make sure that you get everything you need before you proceed. It helps you to avoid passing the same variable again and again. It divides your function into multiple smaller functions that can handle one responsibility.

What is currying FP?

My least jargon filled way of answering that question is this: Currying is the act of refactoring a function that normally receives all its arguments at once into a series of functions that only take one argument at a time. In JavaScript, functions can receive any number of arguments.

What is currying in lambda calculus?

Currying All functions in lambda calculus are prefix and take exactly one argument. If we want to apply a function to more than one argument, we can use a technique called currying that treats a function applied to more than one argument to a sequence of applications of one-argument functions.


2 Answers

OCaml has named parameters and currying is automatic (though sometimes type annotation is required when dealing with optional parameters), but they are not tupled :

    Objective Caml version 3.11.2

# let f ~x ~y = x + y;;
val f : x:int -> y:int -> int = <fun>
# f ~y:5;;
- : x:int -> int = <fun>
# let g = f ~y:5;;
val g : x:int -> int = <fun>
# g ~x:3;;
- : int = 8
like image 146
ygrek Avatar answered Oct 11 '22 12:10

ygrek


Sure, Mathematica can do that sort of thing.

like image 24
High Performance Mark Avatar answered Oct 11 '22 13:10

High Performance Mark