Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fResult/369a213f95454823de5407c4bedb4aec to your computer and use it in GitHub Desktop.
Save fResult/369a213f95454823de5407c4bedb4aec to your computer and use it in GitHub Desktop.
True definition of Higher-Order Function from Mathematics for Working Programmers class, by lect. Dave Rawitat Pulam

Higher-Order Functions 😡😡😡

See something from an actual Maths book.

Definition

A function is called a higher-order function if its arguments and values are allowed to be functions.
This is an important property that most good programming languages possess.
The composition and tupling operations are examples of functions that take other functions as arguments and return functions as results.
For example, suppose we have two functions f: A → B and g: B → C.
Then we can write g • f in prefix form • (f, g).
Viewed in this way, composition is a higher-order function with the following type expression:
(A → B) × (B → C) → (A → C).

A concrete example

The Map Function (Apply to All)

The map function takes one argument, a function f: A → B, and returns as a result the function map(f): Lists[A] → Lists[B], where map(f) applies f to each element in its argument list.
For example, let f: { a, b, c } → { 1, 2, 3 } be defined by f(a) = f(b) = 1 and f(c) = 2.
Then map(f) applied to the list <a, b, c, a> can be calculated as follows:

map(f)(<a, b, c, a.>) = <f(a), f(b), f(c), f(a)> = <1, 1, 2, 1>.
.
.

We have map(f:A → B): [A] → [B] or, in curry form map: (A → B) → [A] → [B] which we shall see in a moment. ;-)

Definition of Higher-Order Functions A concrete example of Higher-Order Functions

These definitions are from this book, Discrete Structure, Logic, and Computability.

The book name is called Discrete Structure, Logic, and Computability
And this is a pretty good book. First edition only. Later edition sucks.
(In lecturer's opinion)

ที่มาที่ไปของ gist นี้: https://www.linkedin.com/posts/sila-setthakan-anan_maths4workingprogrammers-activity-6997207193452257280-og4i

Composition Function Reference: https://www.aplustopper.com/composition-functions-f-o-gx


by the way, just note.
High school's student may have seen Higher-Order Function before...

$\frac{dx^2}{dx} = 2x$ (is Higher-Order Function)

$$f(x) = 2x, find\ f(5) = 10$$ $$g(x) = y^2, find\ g(4) = 16$$ $$\frac{dg(x)}{dx} = f(x)$$ $$\frac{d}{dx}g(x) = f(x)$$ $$\frac{d}{dx}(g(x)) = f(x)$$ $$So...$$ $$let\ f(x) = 2x, let-g(x) = x^2$$ $$let\ h(x) = \frac{d}{dx}$$ so here $h(g(x)) = f(x)$, h is a function that take function g and return function f

More Example: if term $t \in T =&gt; \lambda{x}{t}$ $\lambda{y}{x}\lambda{y}\dot{x}$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment