开发者

I have a procedure with no arguments that creates a matrix, but returns nothing, how can i access the matrix?

开发者 https://www.devze.com 2023-02-02 00:46 出处:网络
I have a procedure with no arguments that creates a matrix, but retur开发者_开发知识库ns nothing, how can i access the matrix?

I have a procedure with no arguments that creates a matrix, but retur开发者_开发知识库ns nothing, how can i access the matrix?

This is my code:

(define matrix
  (lambda (rows columns)
    (do ((m (make-vector rows))
         (i 0 (+ i 1)))
        ((= i rows) m)
      (vector-set! m i (make-vector columns)))))

(define Mod-matrix!
  (lambda (m i j)
    (vector-ref (vector-ref m i) j)))

(define (board) 
  (mk-w (matrix 8 8) 0 0))

(define (mk-b b l c)
  (cond ((and (< l 8) (< c 8)) (begin
                                (Mod-matrix! b l c p)
                                (mk-b b l (+ c 2))))
        ((and (>= c 8) (< l 8))(mk-b b (+ l 2) 0))
        (else (mk-w b 0 1))))

(define (mk-b b l c)
  (cond ((and (< l 8) (< c 8)) (begin
                                (Mod-matrix! b l c b)
                                (mk-w ti l (+ c 2))))
        ((and (>= c 8) (< l 8)) (mk-white b (+ l 2) 0))))


Your mk-w function is not returning the matrix. Try the following:

(define (mk-w b l c)
 (cond
  ((and (< l 8) (< c 8))
   (begin (Mod-matrix! b l c)
        (mk-w b l (+ c 2))
        b))
  ((and (>= c 8) (< l 8))
   (begin
    (mk-w b (+ l 2) 0)
    b))))

Note that in each case the last expression in the begin block is the matrix itself, this is because the return value of a begin block is the value of the last expression. In your post, those last expressions returned an undefined value.


If you want to access the matrix, have the function return the matrix rather than nil or whatever. It's generally considered bad form, but you could set! the matrix to some variable inside the function.

If you can't change the function, you can't access the matrix.

0

精彩评论

暂无评论...
验证码 换一张
取 消