dcmicoltacespedes
2020-6-19 00:14:31

How do I do this function recursively?

must be compared (make-piece 6 6) defined as p28, with all the elements of a list (jugador2) that is generated randomly, and must return a single element that contains a number equal to p28 for example

`(list (make-piece 1 3) (make-piece 0 3) (make-piece 3 3) (make-piece 4 6) (make-piece 4 5) (make-piece 4 4) (make-piece 5 6))`

this returns (make-piece 4 6)

it doesn’t matter if it there is other 6, with just the first 6 that it find, fine.

This is code I made, without recursion:

(define (empieza-juego) (if (= jugadores 2) (begin (set! igual-jugador2(cond [(empty? jugador2) ("no hay más fichas")] [(equal? (piece-space1 p28)(piece-space1 (car jugador2))) (car jugador2)] [(equal? (piece-space1 p28)(piece-space2 (car jugador2))) (car jugador2)] [(equal? (piece-space2 p28)(piece-space1 (car jugador2))) (car jugador2)] [(equal? (piece-space2 p28)(piece-space2 (car jugador2))) (car jugador2)];;1 [(equal? (piece-space1 p28)(piece-space1 (second jugador2))) (second jugador2)] [(equal? (piece-space1 p28)(piece-space2 (second jugador2))) (second jugador2)] [(equal? (piece-space2 p28)(piece-space1 (second jugador2))) (second jugador2)] [(equal? (piece-space2 p28)(piece-space2 (second jugador2))) (second jugador2)];;2 [(equal? (piece-space1 p28)(piece-space1 (third jugador2))) (third jugador2)] [(equal? (piece-space1 p28)(piece-space2 (third jugador2))) (third jugador2)] [(equal? (piece-space2 p28)(piece-space1 (third jugador2))) (third jugador2)] [(equal? (piece-space2 p28)(piece-space2 (third jugador2))) (third jugador2)];;3 [(equal? (piece-space1 p28)(piece-space1 (fourth jugador2))) (fourth jugador2)] [(equal? (piece-space1 p28)(piece-space2 (fourth jugador2))) (fourth jugador2)] [(equal? (piece-space2 p28)(piece-space1 (fourth jugador2))) (fourth jugador2)] [(equal? (piece-space2 p28)(piece-space2 (fourth jugador2))) (fourth jugador2)];;4 [(equal? (piece-space1 p28)(piece-space1 (fifth jugador2))) (fifth jugador2)] [(equal? (piece-space1 p28)(piece-space2 (fifth jugador2))) (fifth jugador2)] [(equal? (piece-space2 p28)(piece-space1 (fifth jugador2))) (fifth jugador2)] [(equal? (piece-space2 p28)(piece-space2 (fifth jugador2))) (fifth jugador2)];;5 [(equal? (piece-space1 p28)(piece-space1 (sixth jugador2))) (sixth jugador2)] [(equal? (piece-space1 p28)(piece-space2 (sixth jugador2))) (sixth jugador2)] [(equal? (piece-space2 p28)(piece-space1 (sixth jugador2))) (sixth jugador2)] [(equal? (piece-space2 p28)(piece-space2 (sixth jugador2))) (sixth jugador2)];;6 [(equal? (piece-space1 p28)(piece-space1 (seventh jugador2))) (seventh jugador2)] [(equal? (piece-space1 p28)(piece-space2 (seventh jugador2))) (seventh jugador2)] [(equal? (piece-space2 p28)(piece-space1 (seventh jugador2))) (seventh jugador2)] [(equal? (piece-space2 p28)(piece-space2 (seventh jugador2))) (seventh jugador2)];;7 [else "no hay"])) (display "ficha jugador2: ") (display igual-jugador2) (display "\n") (display "fichas sobrantes jugador2: ") (remove igual-jugador2 jugador2) );parentesis de begin "debe comer" );parentesis de if );parentesis del define Thanks for your time


badkins
2020-6-19 00:18:56

@dcmicoltacespedes you might find <https://www.amazon.com/Little-Schemer-Daniel-P-Friedman/dp/0262560992/ref=sr_1_1?dchild=1&keywords=the+little+schemer&qid=1592525880&sr=8–1|The Little Schemer> of interest. It’s an excellent tutorial for recursion.


badkins
2020-6-19 00:20:26

I don’t want to frustrate, or discourage, you, but it might be worthwhile to take a step back and work on some fundamentals - whether it’s the book I mentioned, or some online tutorials, etc. I appreciate your enthusiasm, but I get the impression you’re trying to skip over the fundamentals and jump right into some problems you want to solve. If so, that may be a frustrating way to go.


badkins
2020-6-19 00:21:06

This forum is probably more useful for answering more specific questions.