Skip to content

Lambdas and partial application

Jonathan Price edited this page Sep 16, 2017 · 4 revisions

There are several functions which manipulate other functions, these include partial and flip. It is somewhat of an open question as to how the resulting functions (lambdas) should be displayed.

Displaying lambdas

Lambdas anonymous functions. Currently these are displayed like (flip ^) or (0 + partial), but we could use a more notation:

  1. 0 + partial : would yield {x -> x 0 +}

And likewise:

  1. ^ flip : would yield {(x, y) -> x y ^}

Note that the {} needn't be explicitly written as tokens are already blocked-off visually. This lambda notation may scale better with combinations of flip and partial, such as:

  1. 2 ^ flip : partial : would yield {x -> x 2 ^} instead of (partial (flip ^) 2). Note that currently, an issue (#2) makes this display as (2 flip partial)

Replacing flip and partial with pattern matching

I am also not very happy with how partial works at the time-of-writing. Currently it only works for one argument at a time. This may or may not be the preferred method. It may be possible to implement a function that takes a list using the current binary partial, however whether or not that is possible, it currently often takes several steps to do something that might be made much simpler -- that is mapping a list over some function created with partial and flip.

I had implemented a bit of pattern matching using a new literal _. This makes it possible to combine partial and flip into a single function which could take a list of arguments, some of which can be _. For example, if we want to square each number in a list. We would currently write

[1, 2, 3] 2 ^ flip : partial : map :

However with a replacement (called part for now,to avoid confusion) instead we could write:

[1, 2, 3] [_ 2] ^ part : map :

This function takes a list of arguments and a function (like apply) and creates a new function with some of the arguments filled out.

To reverse the order of the application, i.e. to take 2 to the power of each item, we write:

[1, 2, 3] [2 _] ^ part : map :

This is much more intuitive conceptually than flip. It also generalizes to ternary or higher arity functions:

We might reduce a single function list over two functions. Here reduce takes 3 arguments, so this would have been difficult with flip and partial.

[-, +] [[1, 2, 3] _ 0] reduce part : map :

Furthermore, (depending on implementation details) we may be able to implement apply using the new partial (part). Essentially we would replace [args] f apply : with [args] f part : :. That is, we create a 0-argument function by giving all arguments to part then apply that with an extra :.

Clone this wiki locally