Finding the diagonals of square matrix made from l

2019-08-10 04:56发布

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.

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-10 05:06

(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!

查看更多
登录 后发表回答