把它的论点应用于自身的功能? [英] Function which applies its argument to itself?
问题描述
fn x => xx
这会产生以下错误(新泽西州v110.72的标准ML):
stdIn:1.9-1.12错误:运算符不是函数[circularity]
运算符:'Z
in expression:
xx
我可以理解为什么这是不被允许的 - 例如,我我不确定如何写下它的类型 - 但它并不完全没有意义;例如,我可以将身份函数传递给它并将其取回。
是否有此函数的名称? (有没有办法在SML中表达它?)
类似ML的系统。即使使用身份识别功能也不行,因为第一个 x
和第二个 xx
必须是(_ a - > _a) - >类型的该函数的不同实例(_a - > _a)
和 _a - > _a
,对于某些类型 _a
。
事实上,类型系统是旨在禁止像
(λx。xx)(λx。xx)
在无类型的lambda演算中。在动态类型化的语言Scheme中,你可以编写这个函数:
$ p $ (define(apply-to-self x)(xx) )
并获得预期结果
> (define(id x)x)
> (eq?(apply-to-self id)id)
#t
Consider the following SML function:
fn x => x x
This produces the following error (Standard ML of New Jersey v110.72):
stdIn:1.9-1.12 Error: operator is not a function [circularity]
operator: 'Z
in expression:
x x
I can sort of see why this isn't allowed -- for one, I'm not really sure how to write down what its type would be -- but it's not completely nonsensical; for instance, I could pass the identity function to it and get it back.
Is there a name for this function? (Is there a way to express it in SML?)
There is no way to express this function in a language with an ML-like type system. Even with the identity function it wouldn't work, because the first x
and the second in x x
would have to be different instances of that function, of type (_a -> _a) -> (_a -> _a)
and _a -> _a
, respectively, for some type _a
.
In fact, type systems are designed to forbid constructs like
(λx . x x) (λx . x x)
in the untyped lambda calculus. In the dynamically typed language Scheme, you can write this function:
(define (apply-to-self x) (x x))
and get the expected result
> (define (id x) x)
> (eq? (apply-to-self id) id)
#t
这篇关于把它的论点应用于自身的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!