开发者

Is lambda a type of higher order function?

I saw this question on one of the job postings and its asking what's a lambda function and what its relation to h开发者_JAVA技巧igher order function. I already know how to use lambda function but not quite confident explaining it so I did a little googling and found this: What is a lambda (function)? and this http://en.wikipedia.org/wiki/Higher-order_function

The definition of HOF which says should at least take one or more function or return a function fits on what a lambda is, so my question is.. is a lambda a type of HOF?

Or anyone who could explain their relation further?


The definition of HOF which says should at least take one or more function or return a function fits on what a lambda is

Does it? (lambda (x) (x+1)) (or x => x+1 or \x -> x+1 or fun x -> x+1, depending on your language's syntax) is a lambda. However it neither takes a function as its argument (it takes an int), nor does it return one.

So no, lambdas aren't necessarily higher order functions, though they can be.

A lambda is an anonymous function. As such it is a function. But it's only a higher-order function if it takes or returns a function, which most lambdas do not. However lambdas are most often used as arguments to higher functions (i.e. if you do Where(s => s.Length > 5) Where is a higher-order function and s => s.Length > 5 is a (first-order) lambda), so they are related.


It depends on what you mean by "lambda".

The following paragraph from the Wikipedia page you linked to describes the relationship clearly from a type theoretic standpoint.

"In the untyped lambda calculus, all functions are higher-order; in a typed lambda calculus, from which most functional programming languages are derived, higher-order functions are generally those with types containing more than one arrow. In functional programming, higher-order functions that return other functions are said to be curried."

In other words, in type theoretic terms, a function (lambda) is always higher-order in untyped lambda calculus, and may be higher order in typed lambda calculus ... depending on its type signature.

If we are talking about the "lambda" construct implemented by some programming languages, then it depends on 1) the actual language you are talking about, and 2) on the particular usage in a particular language.

In languages where lambdas are anonymous first class functions, you would expect them to be capable of expressing higher-order functions. But a higher-order function is a function that takes other functions as arguments and / or returns them as results. And not all uses of "lambda" in an application will do that.


Lambda syntax makes implementation of higher-order functions easier. For example, currying is a higher-order function made easier by lambda syntax.

You probably want to study the lambda operator in order to understand higher-order functions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜