定义一个表单作为函数名称? [英] define a form as function name?
问题描述
(define((K x)y)x )
(define(((S x)y)z)
((xz)(yz)))
整个文件是 here 。
这个合法的计划是什么? (K x)是一个参数化函数,就像Java中的泛型函数一样?我查阅了麻省理工学院计划参考资料,看起来似乎在这种类型的定义中没有提到任何东西。
在MIT Scheme中试用它
$ $ p $ (define((K x)y)x)
;值:k
((k 3)4)
;值:3
显然,这些是 K从 和
S
>组合子组合逻辑 SKI微积分。
(define k(lambda(x)(lambda(y)x) ))
;值:k
((k 3)4)
;值:3
显然,MIT-Scheme对我们来说是这样,正如常规定义像(define(fun foo)bar)
被翻译为(define fun(lambda(foo)bar))
。
S
combinator会明确定义为
<$ ($ x $($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b(define((add a)b)(+ ab))
; Value:add
(define(add1 a)(+ a 1))
; :add1
(((s add)add1)3)
;值:7
这就是咖啡的语言(如eg Haskell)工作,其中每个函数都是一个参数的函数。 Haskell在这方面非常接近组合逻辑,根本没有使用圆括号,我们可以简单地写出相同的定义
_K xy = x
_S xyz = xz(yz)
c $ c> _S(+)(1+)3 产生 7
。
I'd like to know what this code means in Scheme:
(define ((K x) y) x)
(define (((S x) y) z)
((x z) (y z)))
The whole file is here.
Is this legal Scheme? Is (K x) a parametrized function, something like generic functions in Java? I looked up the MIT Scheme reference, there seems to be nothing mentioned for definition of this kind.
Trying it in MIT Scheme works
(define ((K x) y) x)
;Value: k
((k 3) 4)
;Value: 3
Apparently, these are the definitions for K
and S
combinators from a combinatorial logic SKI calculus.
We can define the same function explicitly,
(define k (lambda (x) (lambda (y) x)))
;Value: k
((k 3) 4)
;Value: 3
Apparently, MIT-Scheme does that for us, just as in case of regular definitions like (define (fun foo) bar)
being translated to (define fun (lambda (foo) bar))
.
The S
combinator would be defined explicitly as
(define S (lambda (x) (lambda (y) (lambda (z)
((x z) (y z))))))
(define ((add a) b) (+ a b))
;Value: add
(define (add1 a) (+ a 1))
;Value: add1
(((s add) add1) 3)
;Value: 7
This is how currying languages (like e.g. Haskell) work, where every function is a function of one argument. Haskell is very close to the combinatorial logic in that respect, there's no parentheses used at all, and we can write the same definitions simply as
_K x y = x
_S x y z = x z (y z)
So that _S (+) (1+) 3
produces 7
.
这篇关于定义一个表单作为函数名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!