简体   繁体   English

根据用户在Racket中的选择从列表列表中删除元素

[英]Removing elements from list of lists based on user choice in Racket

As the question shows, I'm trying to take user input to remove elements from a list of lists. 如问题所示,我正在尝试接受用户输入以从列表列表中删除元素。 The list is formatted as so. 该列表的格式如下。 '((XXX) (XXXX) (XX)) where each sublist contains an arbitrary amount of X's. '((XXX) (XXXX) (XX)) ,其中每个子列表包含任意数量的X。

The user chooses 1,2,3,... for each of the sublists. 用户为每个子列表选择1,2,3,...。 So if the user types 1, the sublist '(XXX) gets chosen. 因此,如果用户键入1,则选择子列表'(XXX) The user then chooses how many of the elements to remove. 然后,用户选择要删除的元素数量。

The current code I have for this is as follows. 我为此的当前代码如下。

(define (humanTurn rows player playerNumber)
    (drawBoard (with-handlers ([exn:fail?
                                  (lambda (exn)
                                      (display 
                                           "ILLEGAL MOVE, PLEASE ENTER A VALID NUMBER\n"))])
                              (getRowAndSticks rows))
               (list (first (rest player)) (first player)) 
               (cond [(equals? playerNumber 1) 2]
                     [(equals? playerNumber 2) 1]
                     [else "error"])))

(define (getRowAndSticks rows)
    (list-tail (list-ref rows (sub1 (getRow)))
               (begin
                   (display "How many sticks:........ ")
                   (read))))

(define (getRow)
    (display "Which row do you choose: ")
    (read))

The problem is that I can't figure out how to append the list back together. 问题是我不知道如何将列表重新附加在一起。 I've only managed to get the list that I've removed the elements from. 我只设法获得了从中删除元素的列表。 I don't see any solution, because I'm not allowed to use variables. 我看不到任何解决方案,因为不允许使用变量。

I am having a hard time following your code, but for joining lists back together use either the list function or the list* . 我很难遵循您的代码,但是要使列表重新结合在一起,请使用list函数或list* The difference between the two is that the latter makes a list of all the arguments except for the last one which is sort of tacked on. 两者之间的区别在于,后者列出了所有自变量的列表,但最后一个被附加了。

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

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