kkk
kkk

Reputation: 33

Scheme list (RECURSION)

I have a list of lists [(1 2 3) (4 5 6) (7 8 9)] which is for a matrix. I am looking for how to get the diagonal( which should be (1 5 9). My code provides me with an error "OPERATOR IS NOT A PROCEDURE". i dont know exactly what i did wrong in this. Help would be appreciated.THE CODE IS BELOW.

(define (diagonal lst)
(if(null? lst)
(0)
((cons (list-ref (reverse (car lst)) (- (length lst) 1)) (diagonal (cdr lst))))))

Upvotes: 1

Views: 98

Answers (2)

soegaard
soegaard

Reputation: 31145

The diagonal of

a b c d 
e f g h
c d e f
g h i j

consists of the a (the first element of the first list) followed by the diagonal of

  f g h
  d e f
  h i j

The matrix above consists of the rows of the original matrix with the first row omitted and the first column removed.

(define (diagonal m)
   (if (empty-matrix? m) 
       '()
       (cons (first (first m))
             (diagonal (remove-first-column (remove-first-row m))))))

 (define (remove-first-column m)
    ...fill-in...)

 (define (remove-first-row m)
    ...fill-in...)

Upvotes: 0

Mathieu Borderé
Mathieu Borderé

Reputation: 4367

(0) -> you cannot evaluate the procedure 0, because it's not a procedure. Drop the parentheses.

Upvotes: 1

Related Questions