Search code examples
rreturn-valuelambda-calculus

What does the lambda calculus have to say about return values?


It is by now a well known theorem of the lambda calculus that any function taking two or more arguments can be written through currying as a chain of functions taking one argument:

# Pseudo-code for currying
f(x,y) -> f_curried(x)(y)

This has proven to be extremely powerful not just in studying the behavior of functions but in practical use (Haskell, etc.).

Functions returning values, however, seem to not be discussed. Programmers typically deal with their inability to return more than one value from a function by returning some meta-object (lists in R, structures in C++, etc.). It has always struck me as a bit of a kludge, but a useful one.

For instance:

# R code for "faking" multiple return values
uselessFunc <- function(dat) {
   model1 <- lm( y ~ x , data=dat )
   return( list( coef=coef(model1), form=formula(model1) ) )
}

Questions

  1. Does the lambda calculus have anything to say about a multiplicity of return values? If so, do any surprising conclusions result?
  2. Similarly, do any languages allow true multiple return values?

Solution

  • According to the Wikipedia page on lambda calculus:

    Lambda calculus, also written as λ-calculus, is a formal system for function definition, function application and recursion

    And a function, in the mathematical sense:

    Associates one quantity, the argument of the function, also known as the input, with another quantity, the value of the function, also known as the output

    So answering your first question no, lambda calculus (or any other formalism based on mathematical functions) can not have multiple return values.

    For your second question, as far as I know, programming languages that implement multiple return values do so by packing multiple results in some kind of data structure (be it a tuple, an array, or even the stack) and then unpacking it later - and that's where the differences lie, as some programming languages make the packing/unpacking part transparent for the programmer (for instance Python uses tuples under the hood) while other languages make the programmer do the job explicitly, for example Java programmers can simulate multiple return values to some extent by packing multiple results in a returned Object array and then extracting and casting the returned result by hand.