【问题标题】:Unable to get implementation of Y combinator working无法实现 Y 组合器工作
【发布时间】:2012-08-12 18:45:36
【问题描述】:

这是代码 (also here):

#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)))])))))

当我运行它时:

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

截图如下:

我使用 DrRacket 作为 repl。 代码有什么问题?

【问题讨论】:

    标签: functional-programming scheme racket combinators y-combinator


    【解决方案1】:

    length 周围应该有括号:

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

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

    (define poorY
      ((lambda (f) (f f))
       (lambda (length)
         (lambda (ls)
           (cond
             [(null? ls) 0]
             [else (add1 ((length length) (cdr ls)))])))))
    

    另见Y combinator discussion in "The Little Schemer"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      相关资源
      最近更新 更多