(define (remove an-i a-loi) (cond [(empty? a-loi) empty] [else (cond [(= (first a-loi) an-i) (remove an-i (rest a-loi))] [else (cons (first a-loi) (remove an-i (rest a-loi)))])])) (remove 0 (list 0 1 2 0 3)) = (remove 0 (list 1 2 0 3)) = (cons 1 (remove 0 (list 2 0 3))) = (cons 1 (cons 2 (remove 0 (list 0 3)))) = (cons 1 (cons 2 (remove 0 (list 3)))) = (cons 1 (cons 2 (cons 3 (remove 0 empty)))) = (cons 1 (cons 2 (cons 3 empty))) #| If you put this code into the definition window of DrScheme and click on EXECUTE, you will see (cons 1 (cons 2 (cons 3 empty))) seven times in the evalution window. Note how the equals-sign really does means equality (as expected). Do not use other symbols instead of "=". In particular, many people want to use arrows. You never wrote "2 + 3 --> 5" in junior high; 2+3 doesn't transform into 5; it *is* 5. And don't leave out the equals-sign entirely: a list of expressions, without claiming any relationship between them, isn't helpful. *Do* use the equals-sign to link your statements, asserting their equality. |# #| Okay, the above actually skips much work the stepper actually has to do. In general, you can skip over steps, and just show the expression immediately before making a recursive call. But you should be able to generate each of the following, if asked: |# (remove 0 (list 0 1 2 0 3)) ; Substitute in this list and number into the body of "remove": ; (The law of scheme: function-call.) ; = (cond [(empty? (list 0 1 2 0 3)) empty] [else (cond [(= (first (list 0 1 2 0 3)) 0) (remove 0 (rest (list 0 1 2 0 3)))] [else (cons (first (list 0 1 2 0 3)) (remove 0 (rest (list 0 1 2 0 3))))])])) ; The law-of scheme for "cond", which says to do two things: ; (a) evaluate the first question, ... ; = (cond [false empty] [else (cond [(= (first (list 0 1 2 0 3)) 0) (remove 0 (rest (list 0 1 2 0 3)))] [else (cons (first (list 0 1 2 0 3)) (remove 0 (rest (list 0 1 2 0 3))))])]) ; ... and (b) if it's false, the result of evaluation is ; a new cond with the first q/a pair discarded: ; = (cond [else (cond [(= (first (list 0 1 2 0 3)) 0) (remove 0 (rest (list 0 1 2 0 3)))] [else (cons (first (list 0 1 2 0 3)) (remove 0 (rest (list 0 1 2 0 3))))])]) ; You should be able to identify which law-of-scheme ; is being used for each step, yielding: ; = (cond [(= (first (list 0 1 2 0 3)) 0) (remove 0 (rest (list 0 1 2 0 3)))] [else (cons (first (list 0 1 2 0 3)) (remove 0 (rest (list 0 1 2 0 3))))]) = (cond [(= 0 0) (remove 0 (rest (list 0 1 2 0 3)))] [else (cons (first (list 0 1 2 0 3)) (remove 0 (rest (list 0 1 2 0 3))))]) = (cond [true (remove 0 (rest (list 0 1 2 0 3)))] [else (cons (first (list 0 1 2 0 3)) (remove 0 (rest (list 0 1 2 0 3))))]) = (remove 0 (rest (list 0 1 2 0 3)))] = (remove 0 (list 1 2 0 3))