简体   繁体   中英

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.

The user chooses 1,2,3,... for each of the sublists. So if the user types 1, the sublist '(XXX) gets chosen. 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* . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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