方案中的矩阵加法
我正在尝试添加一个矩阵,但它不起作用......
(define (matrix-matrix-add a b) (map (lambda (row) (row-matrix-add row b)) a)) (define (row-matrix-add row matrix) (if (null? (car matrix)) '() (cons (add-m row (map car matrix)) (row-matrix-add row (map cdr matrix))))) (define (add-m row col) (if (null? col) 0 (+ (car row) (car col) (add-m (cdr row) (cdr col)))))
I am trying to add a matrix and it is not working...
(define (matrix-matrix-add a b) (map (lambda (row) (row-matrix-add row b)) a)) (define (row-matrix-add row matrix) (if (null? (car matrix)) '() (cons (add-m row (map car matrix)) (row-matrix-add row (map cdr matrix))))) (define (add-m row col) (if (null? col) 0 (+ (car row) (car col) (add-m (cdr row) (cdr col)))))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是非常简短的工作实现。 Map擅长摆脱一层递归,当你可以使用它时。
(定义(矩阵相加xy)(映射(lambda(xy)(映射+xy))xy))
Here is very short working implementation. Map is good at getting rid of a layer of recursion, when you can use it.
(define (matrix-add x y) (map (lambda (x y) (map + x y)) x y))
这是一个有效的实现:
示例用法和输出:
Here is a working implementation:
Sample usage and output: