Potheker
Potheker

Reputation: 95

Racket: expected: procedure?

I have the following code:

(define numbers '(2 3 5 3 1 22 2))

(define (count val l) 
    (if (null? l)
        0
        (+
            (if (= (first l) val) 1 0)
            (count val (rest l))   
        )
    )
)

(display (count 6 numbers))

(sorry if my code looks awful, only have to use this language for one time)

the compiler says:

count: contract violation
  expected: procedure?
  given: 6
  argument position: 1st
  other arguments...:
   '(3 5 3 1 22 2)

Upvotes: 0

Views: 74

Answers (1)

Will Ness
Will Ness

Reputation: 71119

You are entering the code in the interactions area.

Don't. Enter it in the source code area, and load it. Then it works.

What happens is, the function count already exists, and you are redefining it. But if you do that in the interactions area, your new function will use the already existing one, instead of recursively calling itself as it ought to:

(define (count val l) 
    (if (null? l)
        0
        (+
            (if (= (first l) val) 1 0)
            (count val (rest l))       ;; ****** HERE
        )
    )
)

And the existing function expects a procedure as its first argument, as can be seen in its documentation.

Upvotes: 7

Related Questions