Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 在scheme/lisp中使用car和cdr进行列表调用_List_Scheme_Lisp_Racket - Fatal编程技术网

List 在scheme/lisp中使用car和cdr进行列表调用

List 在scheme/lisp中使用car和cdr进行列表调用,list,scheme,lisp,racket,List,Scheme,Lisp,Racket,当前正在使用scheme以列表表示法设置单元格。我的问题是如何使用cons正确地创建scheme(Dr Racket)列表对。我的代码说明了我想要的正确位置,但通过在前面添加创建了一个列表。我目前只处理这个方面的column=1、2、3或4,当row=1时。。。任何帮助都将不胜感激 (define KMMgame 0) (define (KMMStartGame) ( begin (set! KMMgame '( 1(4 9 0 0)(99 0 0 0)(88 0 0 0)(11

当前正在使用scheme以列表表示法设置单元格。我的问题是如何使用cons正确地创建scheme(Dr Racket)列表对。我的代码说明了我想要的正确位置,但通过在前面添加创建了一个列表。我目前只处理这个方面的column=1、2、3或4,当row=1时。。。任何帮助都将不胜感激

(define KMMgame 0)

(define (KMMStartGame)
  ( begin
    (set! KMMgame '(  1(4 9 0 0)(99 0 0 0)(88 0 0 0)(11 0 0 1)
                       (3 8 0 0)(77 0 0 0)(66 0 0 0)(55 0 0 2)
                       (2 0 0 0)(44 0 0 0)(33 0 0 0)(22 0 0 3)
                       (1 0 0 0)(43 0 0 0)(34 0 0 0)(87 0 0 4)))
    (display "Starting Game Now!" ) (newline)
    #t))

;Passes list without the one on front (That is for my player state, irrelevant to my question)
(define ( KMMmove  KMMgame Plane Row Column Token)
        (KMMMove  (car(cdr KMMgame)) (cdr (cdr KMMgame)) Plane Row Column Token) )

;Set the cell to token.
(define (KMMMove KMMgame KMMend Plane Row Column Token)
        (if (= Row 1)
            (if (= Column 1)
                (cons (cons Token (cdr KMMgame)) KMMend)
                (cons (car KMMgame)  (KMMMove (cdr KMMgame) KMMend Plane Row (- Column 1) Token))
            ) 
;Next line accounts for rows greater than one, not working yet so please disregard. Exists to compile.
            (cons (cons (car(cdr (cdr KMMgame))) (KMMMove (car KMMgame) Plane (- Row 1) Column Token)) (cdr(cdr KMMgame))
           )
         )
)
(KMMMove KMMgame 4 1 1 999) 平面4,第1行,第1列,要添加的标记(而不是当前编号999)将打印所需的输出,但在一组额外的括号内:

((999 9 0 0)
 (99 0 0 0)
 (88 0 0 0)
 (11 0 0 1)
 (3 8 0 0)
 (77 0 0 0)
 (66 0 0 0)
 (55 0 0 2)
 (2 0 0 0)
 (44 0 0 0)
 (33 0 0 0)
 (22 0 0 3)
 (1 0 0 0)
 (43 0 0 0)
 (34 0 0 0)
 (87 0 0 4))
(KMMmove KMMgame 4 1 2 999) 平面4,第1行,第2列,标记999,给出了列的正确位置,但在列表的前面与外部配对

(4
 (999 0 0)
 (99 0 0 0)
 (88 0 0 0)
 (11 0 0 1)
 (3 8 0 0)
 (77 0 0 0)
 (66 0 0 0)
 (55 0 0 2)
 (2 0 0 0)
 (44 0 0 0)
 (33 0 0 0)
 (22 0 0 3)
 (1 0 0 0)
 (43 0 0 0)
 (34 0 0 0)
 (87 0 0 4))
第2列的所需输出:

 (4 999 0 0)
 (99 0 0 0)
 (88 0 0 0)
 (11 0 0 1)
 (3 8 0 0)
 (77 0 0 0)
 (66 0 0 0)
 (55 0 0 2)
 (2 0 0 0)
 (44 0 0 0)
 (33 0 0 0)
 (22 0 0 3)
 (1 0 0 0)
 (43 0 0 0)
 (34 0 0 0)
 (87 0 0 4)

要获得这样的列表,我建议使用帮助函数

(define (newGameState KMMgame Row Column Plane Token)
  (if (= Plane 4)
      (steptwo KMMgame Row Column Plane Token)
      (newGameState  KMMgame (+ 4 Row) (+ Plane 1) Column Token)
   )
)
(define (steptwo KMMgame Row Column Plane Token)
        (if (= Row 1)
            (cons (stepthree (car (cdr KMMgame)) Row Column Plane Token) (cdr (cdr KMMgame)))
            (cons (car (cdr KMMgame))(steptwo (cdr KMMgame) (- Row 1) Column Plane Token))
        )
)
(define (stepthree KMMgame Row Column Plane Token)
        (if (= Column 1)
            (cons Token (cdr KMMgame))
            (cons (car KMMgame) (stepthree (cdr KMMgame) Row (- Column 1) Plane Token))
        )
)
这应该会产生期望的输出