1

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))))))

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.