无法实现Y组合器的工作

Han*_*Sun 1 scheme functional-programming combinators y-combinator racket

这是代码(也在这里):

#lang racket
(define poorY
  ((lambda length
    (lambda (ls)
      (cond
        [(null? ls) 0]
        [else (add1 ((length length) (cdr ls)))])))
  (lambda length
    (lambda (ls)
      (cond
        [(null? ls) 0]
        [else (add1 ((length length) (cdr ls)))])))))
Run Code Online (Sandbox Code Playgroud)

当我运行它:

> (poorY '(9 7 8))
. . application: not a procedure;
 expected a procedure that can be applied to arguments
  given: '(#<procedure>)
  arguments...:
   '(#<procedure>)
Run Code Online (Sandbox Code Playgroud)

屏幕截图如下所示:

在此输入图像描述

我正在使用DrRacket作为代表.代码有什么问题?

Wil*_*ess 9

周围应该有括号length:

(define poorY
  ((lambda (length)  ;; here
    (lambda (ls)
      (cond
        [(null? ls) 0]
        [else (add1 ((length length) (cdr ls)))])))
  (lambda (length)   ;; and here
    (lambda (ls)
  ......
Run Code Online (Sandbox Code Playgroud)

您也可以尝试两次输入相同的长lambda表达式,而不是两次

(define poorY
  ((lambda (f) (f f))
   (lambda (length)
     (lambda (ls)
       (cond
         [(null? ls) 0]
         [else (add1 ((length length) (cdr ls)))])))))
Run Code Online (Sandbox Code Playgroud)

另见"The Little Schemer"中的Y组合子讨论.