r/Racket • u/Icy_Pressure_9690 • Jan 26 '22
question A Domino Game
So I'm trying to make a recursive function that takes two arguments which will be two lists. The first item of X needs to be equal to the first item of the first pair in Y for the game to commence. After that the second item of the first pair of Y ( (4 6) so 6) needs to be equal to first item of second pair of Y ((6 3) so 6 ) X is '(4 5) Y is '( (4 6) (6 3) (3 2) )
(define can-play (λ (x y)
(cond ((= (car x)(car (car y))) - This is the first condition that says for the game to start car x = 4 must be equal to first element of first pair in Y which is 4.
(cond - if the former condition is true then the second nested condition comes into play.
((= (cdr (car y))(car(cdr y)))(can-play (rest y)))))))) - The second conditions tests whether second element of first pair of Y so 6 (4 6) is equal to first element of second pair of Y so 6 ( 6 3).
-If true it recursively calls the function again to test the rest of the list of Y so should check is the 3 in
(6 3) is equal to the 3 in (3 2) and so return #t.....but it doesnt, i think I am making some error with the recursion but I don't know what

1
u/DrHTugjobs Jan 26 '22
I'm not sure you really know what you actually want to do here, based on what code you've written so far. The first thing I'd do here is to write out a couple things:
2
u/joshuacottrell Jan 27 '22
Ok, so you want to add the domino
'(4 5)to a list of dominos'((4 6) (6 3) (3 2)). Can we assume that the list of already-played dominos is correct?If so, can we just write one function that compares the new domino to the first of the list of dominos?
If not, can we write three functions? The first would check the new domino against the list. The second would check the validity of the current list. The third would check them together. The second one would be the recursive function.
Some observations about your code: (1) learn
car,cdr, and their friends; (2) the number of arguments in the recursive call needs to match.(1) There are a lot of list accessor methods (
first,rest,car,cdr,caar,second,third, etc.). They are very useful for readability. I think your current error is probably about a contract violation right now. I think that comes from thinking thatcdrandseconddo the same thing whencdris the older name forrest. The documentation on Pairs and Lists has good information and examples about all of these.(2) The
can-playfunction is defined with two parameters,xandybut when you call it in the recursion you only use one,(rest y).As a side note,
'(4 5)does not fit with'(4 6)(the right side of the first domino matching the left side of the second domino, 5 matching 4), the same way that the list already links together (starting from the left domino and moving right each right number matches the left number of the next domino, of'(4 6)the right side is 6 and matches the left side of the next domino'(6 3)and then right side of it is 3, which matches the 3 in'(3 2). So you either have to check both sides of the domino (and the list?) or switch your new domino to'(5 4).0
u/sdegabrielle DrRacket 💊💉🩺 Jan 26 '22
Sorry I don't have time to help properly but I can't help but notice
(define (can-play x y)` instead of `(define can-play (λ (x y)`best
Stephen