I am trying to use Racket to make a full deck of playing cards for a BlackJack game. However, I have two problems:
When I use the following code
;This function creates a list of all the card faces. (define faces '(2 3 4 5 6 7 8 9 10 J Q K A)) ;This function creates a list of all card suits. (define suits '(Clubs Diamonds Hearts Spades)) ;make-deck: Creates a new (unshuffled) 52 card deck ; Returns: The deck (a list of cards) ; Parameters: none (define make-deck (letrec ([pair-items (lambda (x y) (cond [(null? x) '()] [(null? y) '()] [(displayln (cons (car x) (cons (car y) (pair-items x (cdr y)))))] ))]) (pair-items faces suits)) )I get
(2 Spades) (2 Hearts . #<void>) (2 Diamonds . #<void>) (2 Clubs . #<void>)And I do not know what the issue is.
I have no idea how to reset the
iteratorto pair the next element in thefacesfunction with the whole of thesuitsfunction. Do I uselet?
The trivial way is to use
cartesian-productto generate the list of cards (You can also usefor*/listto iterate over yourfacesandsuitslists to get the same effect). Both demonstrated below:A big issue with your code is using
(cond ... [(displayln ...)]).displaylnalways returns a void value, which, since it's not#f, is always going to count as true in acond. And without a test-body part of that cond-clause, what the true test-expr returns is what thecondevaluates to, so that's what you're getting added to the lists your function is printing (But not returning). You should probably start by splitting the display of data apart from the creation of it.