在矩阵中找到主要对角线-方案 [英] Find main diagonal in matrix - Scheme

查看:66
本文介绍了在矩阵中找到主要对角线-方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从方矩阵中提取主对角线

I need to extract the main diagonal from a square matrix

(1 2 3)
(4 5 6) -> (1 5 9)
(7 8 9)

我有以下代码,我需要用适当的功能替换....

I have the following code and I need to replace the ... with the appropriate functions.

(define (diag m)
  (if (null? m) '()
      (cons (... m)
            (diag (map ... (... m))))))

输入:(diag '((1 2 3) (4 5 6) (7 8 9))) 输出:(1 5 9)

有什么想法吗?谢谢!

推荐答案

因此,您要问的是,给定列表'((1 2 3) (4 5 6) (7 8 9)),我如何从中获取值1?

So you are asking, given you have the list '((1 2 3) (4 5 6) (7 8 9)) how do I get the value 1 from it?

然后,您在给定相同列表的情况下,如何从中获取((4 5 6) (7 8 9)).

Then you are asking given the same list, how do I get ((4 5 6) (7 8 9)) from it.

然后给出该结果,我该如何使用map制作一个新列表,该列表仅采用其余每个元素列表,因此结果为((5 6) (8 9))

Then given that result how do I make a new list using map that only takes the rest of each element list so that the result is ((5 6) (8 9))

问题代码看起来像是来自SO,它是如何完成问题的 VERY 难题.我说的对吗?

The question code looks like came from SO as an answer with VERY easy challenge on how to complete it. Am I right?

答案当然只是列出每个初学者的策划者都应该知道的访问者:cdr x 2和caar,不一定按此顺序.

The answer is of course just list accessors every beginner schemer should know: cdr x 2 and caar, not necessarily in that order.

这篇关于在矩阵中找到主要对角线-方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆