scheme: (cons) – list error – (“list” #x3) #x14

If one Scheme error wasn’t enough, I’ve got another one. This time it’s from cons.

;The procedure #[compiled-procedure 13 ("list" #x3) #x14 #x103498714] has been called with 1 argument; it requires exactly 2 arguments.

My code looked something like this:

(define (get-last-item lst)
  (define (recurse tsl l)
    (if (null? l)
      (car tsl)
      (recurse (cons (car l)) (cdr l))
  (recurse () lst)

In Scheme, it’s not so easy to realize you’ve missed an argument when you’re coming from a language where anything inside parenthesis are arguments. But in this case, it’s cons.

Why? It needs two arguments, something to car and something to cdr. That recurse line should look something more like this:

      (recurse (cons (car l) tsl) (cdr l))

Now, you may ask, “how did you know it was cons?” Aside from looking for something where I messed up arguments (1 instead of 2), I looked at the internal names. When you see something like #x14 #x103498714 in an error message, and as long as it’s a scheme procedure itself and not one of those fancy special forms, it will always have that representation internally. I ran cons point-blank in the Scheme interpreter and it returned the representation that shown in the error:

#[compiled-procedure 13 ("list" #x3) #x14 #x103498714]

I’m getting better at debugging Scheme code, but it’s still an uphill climb.

Happy cons’ing