0

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)
Will Ness
  • 70,110
  • 9
  • 98
  • 181
Potheker
  • 93
  • 8

1 Answers1

7

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.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • 2
    Nice catch! (regarding entering the code in the interaction area) – soegaard Jan 30 '20 at 17:31
  • @soegaard thanks. I vaguely remember it being discussed on SO before. The only thing I could find so far is [this answer](https://stackoverflow.com/a/15723029/849891) by you. – Will Ness Jan 30 '20 at 20:22