-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
不动点组合子 #5
Labels
Comments
Java实现Y组合子 import lombok.val;
import java.util.function.Function;
public class YCombinator {
public static <T, R> Function<T, R> of(Function<Function<T, R>, Function<T, R>> f) {
Function<Function, Function<T, R>> r = p -> {
@SuppressWarnings("unchecked")
val w = (Function<Function, Function<T, R>>) p;
return f.apply(param -> w.apply(w).apply(param));
};
return r.apply(r);
}
} |
Java实现Y组合子 简化版 import java.util.function.Function;
public class YCombinator {
public static <T, R> Function<T, R> of(Function<Function<T, R>, Function<T, R>> f) {
return n -> f.apply(of(f)).apply(n);
}
} |
Go实现Y组合子 type RecFunc func(n interface{}) interface{}
func YComb(f func(RecFunc) RecFunc) RecFunc {
var r = func(p interface{}) RecFunc {
var w = p.(func(v interface{}) RecFunc)
return f(func(param interface{}) interface{} {
return w(w)(param)
})
}
return r(r)
} |
Go实现Y组合子 简化版 type RecFunc func(interface{}) interface{}
func YComb(f func(RecFunc) RecFunc) RecFunc {
return func(n interface{}) interface{} {
return f(YComb(f))(n)
}
} |
Objective-C实现Y组合子 #define YCombinator(ARG_TYPE, RET_TYPE, REC_NAME, IMP_BLOCK) \
^RET_TYPE (^(RET_TYPE (^(^f)(RET_TYPE (^)(ARG_TYPE)))(ARG_TYPE)))(ARG_TYPE) { \
RET_TYPE (^(^r)(id))(ARG_TYPE) = ^(id p) { \
RET_TYPE (^(^w)(id))(ARG_TYPE) = p; \
return f(^RET_TYPE (ARG_TYPE param) { \
return w(w)(param); \
}); \
}; \
return r(r); \
}(^RET_TYPE (^(RET_TYPE (^REC_NAME)(ARG_TYPE)))(ARG_TYPE) { \
return IMP_BLOCK; \
}) |
Objective-C实现Y组合子 简化版 #define YCombinator(NAME, ARG_TYPE, RET_TYPE) \
typedef RET_TYPE(^NAME##RecFunc)(ARG_TYPE); \
NAME##RecFunc NAME(NAME##RecFunc(^f)(NAME##RecFunc)) { \
return ^RET_TYPE(ARG_TYPE n) { \
return f(NAME(f))(n); \
}; \
} |
Kotlin实现Y组合子 object YCombinator {
fun <T, R> of(f: YFunction<YFunction<T, R>, YFunction<T, R>>): YFunction<T, R> {
return object : YFunction<T, R> {
override fun apply(t: T): R {
return f.apply(of(f)).apply(t)
}
}
}
interface YFunction<in T, out R> : Function<R> {
fun apply(t: T): R
}
} |
Kotlin实现Y组合子 高阶函数 object YCombinator {
fun <T, R> of(f: ((T) -> R) -> ((T) -> R)): (T) -> R {
return { t: T -> f(of(f))(t) }
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
不动点组合子(Fixed-point combinator, 或不动点算子, 使用
FIX
表示)是计算其他函数的一个不动点的高阶函数.不动点算子具有以下特性, 对于任何函数
f
都有:不动点组合子中最有名的(也可能是最简单的)是
Y
组合子:另一个常见不动点组合子是图灵不动点组合子, 即
Θ
组合子:实现不动点组合子时用到的类型:
不动点组合子:
参考使用 Lambda 表达式编写递归 系列文章
The text was updated successfully, but these errors were encountered: