Finding the diagonals of square matrix made from lists

巧了我就是萌 提交于 2019-12-13 02:32:24

问题


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:


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



来源:https://stackoverflow.com/questions/33367492/finding-the-diagonals-of-square-matrix-made-from-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!