Faery
Faery

Reputation: 4650

Can't think how to remove digits from list - Scheme

This is my function:

(define (remove-digit digit list)
  (cond ((null? list ...))
    (( = (car list) digit) (remove-digit digit (cdr list)))
      (else (cons (car list) (if (null? list) (cons(remove-digit digit (cdr list))))))is:

and it should do this:

(1 2 4 5 2 5 6) after (remove-digit 2 list) should be (1 4 5 5 6)

but I can't think what to do when the list becomes null.

Can you please give me a little help or some kind of idea? Thank you very much!

Upvotes: 0

Views: 107

Answers (1)

hoha
hoha

Reputation: 4428

Return an empty list (or the list itself). It is the base case for your function.

(define (remove-digit digit list)
  (cond ((null? list) list)
        ((= digit (car list)) (remove-digit digit (cdr list)))
        (else (cons (car list) (remove-digit digit (cdr list))))))

To deduce base case easily think about "minimal" case of remove-digit with an empty list as a parameter.

(remove-digit digit '())

It should return '() obviously for any digit value one supplies.

Upvotes: 1

Related Questions