Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 higher 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?

like image 975
Marconi Avatar asked Feb 15 '11 03:02

Marconi


People also ask

What is the type of a lambda function?

Lambda expressions in C++14 are functions that can be treated as any other object, such as a class or struct. They can utilize variables defined in the same scope, 'capturing' them implicitly or explicitly by value or reference. A lambda object (also known as 'closure object') can be called like a normal function.

What is higher-order function example?

Note: Functions such as filter(),map(),reduce(),some() etc, these all are example of Higher-Order Functions.

What makes a function a higher-order function?

Basically, a function which takes another function as an argument or returns a function is known as a higher order function. Let's deep dive a bit to see both types of implementation, that is: Passing a function as an argument to another function. Returning a function from another function.

What are higher-order functions in Python?

In high order function, a function can act as an instant of an object type. In high order function, we can return a function as a result of another function. In high order function, we can pass a function as a parameter or argument inside another function.


2 Answers

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.

like image 128
sepp2k Avatar answered Sep 21 '22 14:09

sepp2k


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.

like image 37
Stephen C Avatar answered Sep 22 '22 14:09

Stephen C