I'm new to scheme and I don't know what's wrong -
i'm writing function takes single list of knights fight. code running fights working(jousting-game), i'm writing tournament system , can't tournament round work. said, takes list of knights , has them fight recursively until has fought, , returns 2 lists, 1 of winners, 1 of losers. i've tried know , no matter error , code refuses work, , don't understand why. here's i've written far:
(define (playtourneyround knightlist) ( (cond ((> (length knightlist) 1) (let ( (winner (jousting-game (car knightlist) (cadr knightlist))) (winners (list)) (losers (list)) (results (playtourneyround (cddr knightlist))) ) (if (= winner 1) (winners (append winners (list (car knightlist)))) (winners (append winners (list (cadr knightlist))))) (winners (append (car results))) (losers (list (cadr knightlist) (cadr results))) (list winners losers) ) ) ((= (length knightlist) 1) (list knightlist) ) ) (list '() '()) ) )
can please explain me why i'm getting error "call of non-procedure: #", , how can avoid error in future? i'm sure don't understand scheme/lisp that's important, , use explanation.
thanks help, problem has been solved
first think should know scheme lexically scoped. variable declaration meaningful in code frame declared or child frames.
also opened double parenthesis, not want do, unless inner set returns function , want apply it.
your pretty printing way off. cond
statements should on single line, or lined on second parenthesis of clause. if
has 3 clauses, , should again on same line, or on subsequent lines aligned first argument. function body of let
statement should lined "e" or "t" of let. trailing parnethesis on own line frowned upon.
calling length repeatedly in recursion bad form, length
o(n) operation length of list check if list null or cdr null
and need , inner function you're trying do. (inner define, letrec, or named let do)
if you're appending (append <some-list> (list <some list element>))
you're doing poorly. first off append
o(n) length of first argument. go ahead , accumulate results in reverse order, , reverse @ end.
(define (playtourneyround knightlist) (let loop ((knights-l knightlist) ;;named let (winners (list)) (losers (list))) (cond ((null? knight-l) (map reverse (list winners losers))) ;;length = 0 ((null? (cdr knight-l) ;;lenght = 1 (loop (cdr knight-l) (cons (car knight-l) winners) losers)) (else ;; length > 1 (let* ((winner (jousting-game (car knight-l) (cadr knight-l))) ;;assuming jousting-game return winning knight (loser (if (equal? winner (car knight-l)) (cadr knight-l) (car knight-l)))) (loop (cddr knight-l) (cons winner winners) (cons loser losers)))))))
Comments
Post a Comment