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

标签 scheme lisp

我试图更改列表列表中的一个值,然后使用另一个参数“返回”列表中的整个列表。我能够达到该值,但我不知道如何返回更改后的列表列表。状态由((获取板状态)(获取xy坐标状态)(获取方向状态))组成。其中 get-board 返回 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)))

提前致谢!

最佳答案

这是一种解决方案

(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))

关于方案 - 如何替换/更改列表列表中特定位置的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42610873/

相关文章:

lambda - 仅具有 lambda 表达式的阶乘函数

syntax - Lambda表达式的正确语法,该语法在Scheme中获取任意数量的参数

scheme - 有人使用Scheme 编程语言吗?

lambda - 有没有办法在 Racket 中看到 lambda 的主体?

list - 在 Mathematica 中前置与附加性能

lisp - 比较返回期望值调用函数目录,但在list上的process中却不是这样

lisp - Common-Lisp 包通信问题

syntax - 方案语法转换

scope - 为什么这个函数每次都返回不同的值?

garbage-collection - 在 Scheme/Lisp 实现中不使用垃圾收集器