哪种模式适合 1x1 矩阵?

发布于 2024-10-15 09:58:20 字数 651 浏览 2 评论 0原文

我正在编写一个谓词来检查矩阵是否是(正方形)下三角

想法是如果first行的所有元素(索引0处的元素除外)都为零,则返回true,然后再次调用子矩阵上的谓词获得排除 1行和1列。当子矩阵只有 1 个数字(例如 [[x]])时,递归应该结束,这当然是下三角矩阵。

问题:如何匹配 [[x]],其中 x 只是一个数字?

lowertriangular (???) = True
lowertriangular (x:xs) = all (==0) (tail x) && lowertriangular (map tail xs)

编辑

m = [[1,0,0],[2,-3,0],[4,5,6]]

lt m = all (==0) [0, 0] && lt [[-3, 0], [5, 6]]
     = all (==0) [0, 0] && (all (==0) [0] && lt [6])

lt [6] 应与返回 True 的 [[_]] 匹配。

I'm writing a predicate to check if a matrix is (square) lower triangular.

The idea is to return true if all elements of the first row (except the element at index 0) are all zero, and then call again the predicate on the sub-matrix obtained excluding 1 row and 1 column. Recursion should end when the submatrix is just 1 number (e.g. [[x]]) which is of course lower triangular.

Question: how can I match [[x]] where x is just a number?

lowertriangular (???) = True
lowertriangular (x:xs) = all (==0) (tail x) && lowertriangular (map tail xs)

Edit:

m = [[1,0,0],[2,-3,0],[4,5,6]]

lt m = all (==0) [0, 0] && lt [[-3, 0], [5, 6]]
     = all (==0) [0, 0] && (all (==0) [0] && lt [6])

lt [6] should match [[_]] that returns True.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰之心 2024-10-22 09:58:20

您可以使用模式 [[x]] 匹配 [[x]] - 或 [[_]] 因为您实际上并不关心x的值。即

lowertriangular [[_]] = True

当使用 1x1 矩阵调用时将返回 true。


但应该注意的是,1x1 矩阵已经可以通过 lowertriangle (x:xs) = ... 的情况完美处理,因此您无需显式处理它们。您需要处理的是获得空列表的情况,如下所示:

lowertriangular [] = True

You can match [[x]] using the pattern [[x]] - or [[_]] since you don't actually care about the value of x. I.e.

lowertriangular [[_]] = True

will return true when called with a 1x1 matrix.


However it should be noted that the 1x1 matrices are already perfectly handled by the case lowertriangular (x:xs) = ..., so there's no need for you to handle them explicitly. What you need to handle is the case where you get an empty list, like this:

lowertriangular [] = True
夏见 2024-10-22 09:58:20

也许这就是您正在寻找的:

lowertriangular (x:[]) = True
lowertriangular (x:xs) = all (==0) (tail x) && lowertriangular (map tail xs)

Maybe this is what you're looking for:

lowertriangular (x:[]) = True
lowertriangular (x:xs) = all (==0) (tail x) && lowertriangular (map tail xs)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文