1

Here's the code (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)))])))))

When I run it:

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

The screenshot looks like this:

enter image description here

I'm using DrRacket as the repl. What's wrong with the code?

Will Ness
  • 62,652
  • 8
  • 86
  • 167
Hanfei Sun
  • 39,245
  • 33
  • 107
  • 208

1 Answers1

9

There should be parentheses around length :

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

Instead of typing the same long lambda expression twice, you can also try

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

See also Y combinator discussion in "The Little Schemer" .

Community
  • 1
  • 1
Will Ness
  • 62,652
  • 8
  • 86
  • 167