繁体   English   中英

在scheme / lisp中列出用car和cdr进行呼叫

[英]List calling with car and cdr in scheme/lisp

当前使用方案在列表符号中设置单元格。 我的问题是如何使用缺点正确制作Scheme(Racket博士)列表对。 我的代码说明了我想要的正确位置,但是通过在前面添加来创建列表。 我目前仅在此列= 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))
        )
)

这应该提供所需的输出!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM