Scheme - 如何替换/更改列表列表中特定位置的元素

时间:2017-03-05 16:26:54

标签: scheme lisp

我正在尝试从列表列表中更改一个值,然后使用另一个参数“返回”listh中的整个列表。我能够达到该值,但我不知道如何返回更改列表的列表。状态由((get-board state)(get-xycoordinate state)(get-orientation state))组成。 get-board returs 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)))

提前致谢!

1 个答案:

答案 0 :(得分: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))