2017-03-05 89 views
1

我想從列表中更改一個值,然後用另一個參數「返回」listh中的整個列表。我能夠達到這個價值,但是我不知道如何在這個變化的情況下返回列表清單。狀態由((獲取板狀態)(get-xycoordinate狀態)(獲取取向狀態))組成。 get-board返回板,get-xycoordinate返回(x,y),get-xcoordinate返回x個位置。計劃 - 如何替換/更改列表中的某個位置中的元素

(define (get-board state) 
'(
(0 0 0 0 0 0) 
(0 0 0 0 0 0) 
(0 0 0 0 0 0) 
(0 0 0 0 0 0) 
(0 0 0 0 0 0) 
)) 


(define (put-mark state) 
((+ (list-ref (list-ref (get-board state) (get-xcoordinate state)) (get-ycoordinate state)) 1) (get-xycoordinate state) (get-orientation state))) 

在此先感謝!

回答

0

這裏是一個解決方案

(define (set-list xs i x) 
    (cond 
    [(empty? xs) '()] 
    [(= i 0)  (cons x 
         (cdr xs))] 
    [else  (cons (car xs) 
         (set-list (cdr xs) (- i 1) x))])) 

(define (set-matrix xss i j x) 
    (cond 
    [(empty? xss) '()] 
    [(= i 0)  (cons (list-set (car xss) j x) 
         (cdr xss))] 
    [else   (cons (car xss) 
         (set-matrix (cdr xss) (- i 1) j x))])) 


(set-list '(a b c d e f) 3 'x) ; => '(a b c x e f) 


(set-matrix '((a b c d) 
       (e f g h) 
       (i j k l) 
       (m n o p)) 
      2 3 
      'x) 
; '((a b c d) 
; (e f g h) 
; (i j k x) 
; (m n o p)) 
+0

謝謝!那正是我需要的! –