如何访问2d列表中的索引并在scheme / racket中更改它的值?

问题描述 投票:1回答:1

enter image description hereI有以下空列表(在所有内容都初始化为破折号的意义上为空)

((- - -)(- - -)(- - -))

我希望访问此列表中的特定索引(例如0,1)并将其设置为1

((- 1 -)(- - -)(- - -))

怎么能在计划中完成?

scheme racket
1个回答
3
投票

在球拍中,您可以使用for/list将结果累积到列表中。

erow(列表)中的每个元素,而ij跟踪l中的索引位置。

;; [Listof [Listof Any]] Nat Nat Any -> [Listof [Listof Any]]
;; changes the element at (`x`, `y`) position in `l` to `to`
(define (change-at l x y to)
  (for/list ([row l] [i (length l)])
    (for/list ([e row] [j (length row)])
      (if (and (= x i) (= y j))
          to
          e))))

(change-at '((- - -) (- - -) (- - -)) 0 1 1)
;; => '((- 1 -) (- - -) (- - -))
© www.soinside.com 2019 - 2024. All rights reserved.