Question

I'm trying to use this:

(vector-set! (vector-ref array (car xy)) (cdr xy) step_count)

where xy is a list like '(0 0) passed in as a parameter.

But I get this error:

vector-set!: contract violation
  expected: exact-nonnegative-integer?
  given: '(0)
  argument position: 2nd
  other arguments...:
   '#(0 0 0 0 0)
   1

There is a problem with (cdr xy), I think I need to turn '(0) into 0 but I can't find a mechanism in Racket that can do that.

I know a line like this:

(vector-set! (vector-ref array (sub1 (car xy))) (sub1 (cdr xy)) (read))

works when you form xy with:

(define xy (cons (read) (read)))

Can someone point me in the right direction?

Thanks for reading.

Was it helpful?

Solution

When you use cdr on a list, it means "the rest of the list". The rest of a list is always a list, not a single element of a list.

If you want the second element of the list xy, then you need to use cadr.

(You could also use second.)

(define xy (list 1 2 3))
(cdr xy)    ; '(2 3)
(cadr xy)   ;   2
(second xy) ;   2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top