1

I am currently trying to define a function that takes a list of lists, models that list as a square matrix and returns the diagonal of said matrix.

For example, input ((a b c) (d e f) (g h i)) gives (a e i).

I have a vague idea of how to go about solving this (taking the last element of the last list then the second to last element of the second to last list etc.) but I am not really sure of how to go about programming this in Scheme.

I would appreciate it if somebody could point me in the right direction.

Thanks.

0

1 Answer 1

8

(With apologies to Nethack fans)

As you step back from the altar, you find a little piece of paper lying on the floor from which you can decipher the following text:

0> Calling (DIAG ((A B C) (D E F) (G H I))) 
 1> Calling (DIAG ((E F) (H I))) 
  2> Calling (DIAG ((I))) 
   3> Calling (DIAG NIL) 
   <3 DIAG returned NIL
  <2 DIAG returned (I)
 <1 DIAG returned (E I)
<0 DIAG returned (A E I)

This looks good, but as you start reading the scroll aloud, you smell a foul scent of garbage nearby. With the help of your wand of metamorphosis, you zap the cursed object. The previous text vanishes and another one appears:

0> Calling (DIAG ((A B C) (D E F) (G H I))) 
 1> Calling (DIAG% ((A B C) (D E F) (G H I)) 0) 
  2> Calling (DIAG% ((D E F) (G H I)) 1) 
   3> Calling (DIAG% ((G H I)) 2) 
    4> Calling (DIAG% NIL 3) 
    <4 DIAG% returned NIL
   <3 DIAG% returned (I)
  <2 DIAG% returned (E I)
 <1 DIAG% returned (A E I)
<0 DIAG returned (A E I)

A lit field surrounds you!

1
  • 3
    This is the best answer to a homework question I've ever seen. Commented Oct 27, 2015 at 22:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.