哪种模式适合 1x1 矩阵?
我正在编写一个谓词来检查矩阵是否是(正方形)下三角。
想法是如果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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用模式
[[x]]
匹配[[x]]
- 或[[_]]
因为您实际上并不关心x
的值。即当使用 1x1 矩阵调用时将返回 true。
但应该注意的是,1x1 矩阵已经可以通过
lowertriangle (x:xs) = ...
的情况完美处理,因此您无需显式处理它们。您需要处理的是获得空列表的情况,如下所示:You can match
[[x]]
using the pattern[[x]]
- or[[_]]
since you don't actually care about the value ofx
. I.e.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:也许这就是您正在寻找的:
Maybe this is what you're looking for: