[erlang-questions] LFE - Lisp Flavoured Erlang released

Richard A. O'Keefe ok@REDACTED
Wed Mar 12 05:35:44 CET 2008


Can we distinguish, please, between
     partial application
and
     currying?
Neither of them is a special case of the other.

Currying is the act of taking a function of multiple arguments and,
without binding any, returning a function that takes them one at a time.
For example,

	curry(F) ->
	    case erlang:fun_info(F, arity)
	      of 0 -> fun (_) -> F() end
	       ; 1 -> F
	       ; 2 -> fun (X1) -> fun (X2) -> F(X1,X2) end end
	       ; 3 -> fun (X1) -> fun (X2) -> fun (X3) -> F(X1,X2,X3) end end  
end
	       % continue as long as you have patience or use
	    end.

Partial application is the act of taking a function of multiple
arguments and, by some means, supplying some but not all of them,
returning a function, not necessarily unary, awaiting the rest.

The British AI language Pop-2 supported partial application but not
currying.  If I remember correctly, f(% ... %) provided *trailing*
arguments for f, leaving it still awaiting its leading arguments.
With a curried function, it is easiest to provide leading arguments,
but it is often useful to supply others.

For example, in Haskell a binary operator like > is curried:
	> :: Ord a => a -> a -> Bool
We might want to supply *either* of the arguments:

	x_exceeds = (x >)
	returns \y -> x > y

	exceeding_x = (> x)
	returns \y -> y > x

Working on binary functions, we might have

	apply_left(X, Op) -> fun (Y) -> Op(X, Y) end.

	apply_right(Op, Y) -> fun (X) -> Op(X, Y) end.







More information about the erlang-questions mailing list