旋转矩阵,为什么它不能作为函数工作,但它可以在行命令中工作?
Rotating a matrix, why isn't it working as a function, yet it does in line command?
旋转矩阵的一种方法是对其进行转置,然后反转所有行。
我用两个函数解决了这个问题,map(returns 转置)和 reverse(完成 90° 旋转),在控制台中我执行以下操作:
(reverse (apply map list (list (list 1 2 3 4) (list 5 6 7 8) (list 9 10 11 12) (list 13 14 15 16)) ) )
Result: {{4 8 12 16} {3 7 11 15} {2 6 10 14} {1 5 9 13}}
说的对,但是我做函数的时候没有用
(define (transpose2 matriz)
(reverse (apply map list matriz))
然后做:
> (transpose2 (list (list 1 2 3 4) (list 5 6 7 8) (list 9 10 11 12) (list 13 14 15) ) )
它只是抛出一个错误:
mcar:违约
预期:mpair?
给出:()
我试过导入 (srfi :41)(我也在 r6rs 上工作)但它也不起作用。不过此函数适用于 3 x 3 矩阵。
我该如何解决这个问题?
您的输入有误。
(transpose2 (list (list 1 2 3 4)
(list 5 6 7 8)
(list 9 10 11 12)
(list 13 14 15))) ;; here are only 3 elements while in the others 4!
(transpose2 (list (list 1 2 3 4)
(list 5 6 7 8)
(list 9 10 11 12)
(list 13 14 15 16))) ;; this works flawlessly - you just forgot 16 before!
DrRacket 给出错误:
map: all lists must have same size; arguments were: #<procedure:list> '(1 2 3 4) '(5 6 7 8) '(9 10 11 12) '(13 14 15)
您使用的是哪种 Scheme 解释器?
#lang racket
(define m (list (list 1 2 3 4)
(list 5 6 7 8)
(list 9 10 11 12)
(list 13 14 15 16)))
(define (transpose2 mat)
(reverse (apply map list mat)))
(reverse (apply map list m))
(transpose2 m)
;; both give:
;; '((4 8 12 16) (3 7 11 15) (2 6 10 14) (1 5 9 13))
旋转矩阵的一种方法是对其进行转置,然后反转所有行。 我用两个函数解决了这个问题,map(returns 转置)和 reverse(完成 90° 旋转),在控制台中我执行以下操作:
(reverse (apply map list (list (list 1 2 3 4) (list 5 6 7 8) (list 9 10 11 12) (list 13 14 15 16)) ) )
Result: {{4 8 12 16} {3 7 11 15} {2 6 10 14} {1 5 9 13}}
说的对,但是我做函数的时候没有用
(define (transpose2 matriz)
(reverse (apply map list matriz))
然后做:
> (transpose2 (list (list 1 2 3 4) (list 5 6 7 8) (list 9 10 11 12) (list 13 14 15) ) )
它只是抛出一个错误:
mcar:违约 预期:mpair? 给出:()
我试过导入 (srfi :41)(我也在 r6rs 上工作)但它也不起作用。不过此函数适用于 3 x 3 矩阵。
我该如何解决这个问题?
您的输入有误。
(transpose2 (list (list 1 2 3 4)
(list 5 6 7 8)
(list 9 10 11 12)
(list 13 14 15))) ;; here are only 3 elements while in the others 4!
(transpose2 (list (list 1 2 3 4)
(list 5 6 7 8)
(list 9 10 11 12)
(list 13 14 15 16))) ;; this works flawlessly - you just forgot 16 before!
DrRacket 给出错误:
map: all lists must have same size; arguments were: #<procedure:list> '(1 2 3 4) '(5 6 7 8) '(9 10 11 12) '(13 14 15)
您使用的是哪种 Scheme 解释器?
#lang racket
(define m (list (list 1 2 3 4)
(list 5 6 7 8)
(list 9 10 11 12)
(list 13 14 15 16)))
(define (transpose2 mat)
(reverse (apply map list mat)))
(reverse (apply map list m))
(transpose2 m)
;; both give:
;; '((4 8 12 16) (3 7 11 15) (2 6 10 14) (1 5 9 13))