고차 함수고차 함수(高次函數, higher-order function)는 수학과 컴퓨터 과학에서 적어도 다음 중 하나를 수행하는 함수이다.[1][2][3]
다른 모든 함수들은 일차(first order) 함수이다. 수학에서 고차 함수들은 연산자, 범함수라고도 한다. 미적분에서 미분 연산자가 일반적인 예인데, 그 이유는 함수를 그 미분에 연결시키기 때문이다. 형식화되지 않은 람다 대수에서 모든 함수들은 고차 함수이다. 대부분의 함수형 프로그래밍 언어를 파생시키는 형식화된 람다 대수에서 하나의 함수를 인수로 취하는 고차 함수들은 의 형태의 값이다. 일반적인 예수많은 함수형 프로그래밍 언어에서 볼 수 있는 프로그래밍 언어에서의 지원직접적인 지원파이썬>>> def twice(function):
... return lambda x: function(function(x))
>>> def f(x):
... return x + 3
>>> g = twice(f)
>>> print g(7)
13
F#let twice f = f >> f
let f = (+) 3
twice f 7 |> printf "%A" // 13
하스켈twice :: (a -> a) -> (a -> a)
twice f = f . f
f :: Num a => a -> a
f = subtract 3
main :: IO ()
main = print (twice f 7) -- 1
클로저(defn twice [function x]
(function (function x)))
(twice #(+ % 3) 7) ;13
스킴(define (add x y) (+ x y))
(define (f x)
(lambda (y) (+ x y)))
(display ((f 3) 7))
(display (add 3 7))
얼랭or_else([], _) -> false;
or_else([F | Fs], X) -> or_else(Fs, X, F(X)).
or_else(Fs, X, false) -> or_else(Fs, X);
or_else(Fs, _, {false, Y}) -> or_else(Fs, Y);
or_else(_, _, R) -> R.
or_else([fun erlang:is_integer/1, fun erlang:is_atom/1, fun erlang:is_list/1],3.23).
자바스크립트var twice = function(f, v) {
return f(f(v));
};
var f = function(v) {
return v + 3;
};
console.log(twice(f, 7)); // 13
고func twice(f func(int) int, v int) int {
return f(f(v))
}
func main() {
f := func(v int) int {
return v + 3
}
twice(f, 7) // returns 13
}
스칼라def twice(f:Int=>Int) = f compose f
twice(_+3)(7) // 13
자바 (1.8 이상)Function<Function<Integer, Integer>, Function<Integer, Integer>> twice = f -> f.andThen(f);
twice.apply(x -> x + 3).apply(7); // 13
스위프트func f(x: Int) -> Int {
return x + 3
}
func g(function: (Int) -> Int, paramX: Int) -> Int {
return function(paramX) * function(paramX)
}
g(function: f, paramX: 7)
러스트// Take function f(x), return function f(f(x))
fn twice<A, F>(function: F) -> Box<Fn(A) -> A>
where F: 'static + Fn(A) -> A
{
Box::new(move |a| function(function(a)))
}
// Return x + 3
fn f(x: i32) -> i32 {
x + 3
}
fn main() {
let g = twice(f);
println!("{}", g(7));
}
C++// Generic lambdas provided by C++14.
#include <iostream>
auto twice = [](auto f, int v)
{
return f(f(v));
};
auto f = [](int i)
{
return i + 3;
};
int main()
{
std::cout << twice(f, 7) << std::endl;
}
// Or, use std::function in C++11
#include <iostream>
#include <functional>
auto twice = [](const std::function<int(int)>& f, int v)
{
return f(f(v));
};
auto f = [](int i)
{
return i + 3;
};
int main()
{
std::cout << twice(f, 7) << std::endl;
}
콜드퓨전 마크업 언어 (CFML)twice = function(f, v) {
return f(f(v));
};
f = function(v) {
return v + 3;
};
writeOutput(twice(f, 7)); // 13
PHP$twice = function($f, $v) {
return $f($f($v));
};
$f = function($v) {
return $v + 3;
};
echo($twice($f, 7)); // 13
Rtwice <- function(func) {
return(function(x) {
func(func(x))
})
}
f <- function(x) {
return(x + 3)
}
g <- twice(f)
> print(g(7))
[1] 13
XACMLrule allowEntry{
permit
condition anyOfAny(function[stringEqual], citizenships, allowedCitizenships)
}
대안함수 포인터#include <stdio.h>
double square(double x)
{
return x * x;
}
double cube(double x)
{
return x * x * x;
}
/* Compute the integral of f() within the interval [a,b] */
double integral(double f(double x), double a, double b, int n)
{
int i;
double sum = 0;
double dt = (b - a) / n;
for (i = 0; i < n; ++i) {
sum += f(a + (i + 0.5) * dt);
}
return sum * dt;
}
int main()
{
printf("%g\n", integral(square, 0, 1, 100));
printf("%g\n", integral(cube, 0, 1, 100));
return 0;
}
오브젝트program example;
type
int = integer;
Txy = record x, y: int; end;
Tf = function (xy: Txy): int;
function f(xy: Txy): int;
begin
Result := xy.y + xy.x;
end;
function g(func: Tf): Tf;
begin
result := func;
end;
var
a: Tf;
xy: Txy = (x: 3; y: 7);
begin
a := g(@f); // return a function to "a"
writeln(a(xy)); // prints 10
end.
기능상실// Defunctionalized function data structures
template<typename T> struct Add { T value; };
template<typename T> struct DivBy { T value; };
template<typename F, typename G> struct Composition { F f; G g; };
// Defunctionalized function application implementations
template<typename F, typename G, typename X>
auto apply(Composition<F, G> f, X arg) {
return apply(f.f, apply(f.g, arg));
}
template<typename T, typename X>
auto apply(Add<T> f, X arg) {
return arg + f.value;
}
template<typename T, typename X>
auto apply(DivBy<T> f, X arg) {
return arg / f.value;
}
같이 보기각주
|
Index:
pl ar de en es fr it arz nl ja pt ceb sv uk vi war zh ru af ast az bg zh-min-nan bn be ca cs cy da et el eo eu fa gl ko hi hr id he ka la lv lt hu mk ms min no nn ce uz kk ro simple sk sl sr sh fi ta tt th tg azb tr ur zh-yue hy my ace als am an hyw ban bjn map-bms ba be-tarask bcl bpy bar bs br cv nv eml hif fo fy ga gd gu hak ha hsb io ig ilo ia ie os is jv kn ht ku ckb ky mrj lb lij li lmo mai mg ml zh-classical mr xmf mzn cdo mn nap new ne frr oc mhr or as pa pnb ps pms nds crh qu sa sah sco sq scn si sd szl su sw tl shn te bug vec vo wa wuu yi yo diq bat-smg zu lad kbd ang smn ab roa-rup frp arc gn av ay bh bi bo bxr cbk-zam co za dag ary se pdc dv dsb myv ext fur gv gag inh ki glk gan guw xal haw rw kbp pam csb kw km kv koi kg gom ks gcr lo lbe ltg lez nia ln jbo lg mt mi tw mwl mdf mnw nqo fj nah na nds-nl nrm nov om pi pag pap pfl pcd krc kaa ksh rm rue sm sat sc trv stq nso sn cu so srn kab roa-tara tet tpi to chr tum tk tyv udm ug vep fiu-vro vls wo xh zea ty ak bm ch ny ee ff got iu ik kl mad cr pih ami pwn pnt dz rmy rn sg st tn ss ti din chy ts kcg ve
Portal di Ensiklopedia Dunia