2
(define (delete atm lis)
  (cond

   ((eq? atm (car lis)) (cdr lis))
   (else (cons (car lis) (delete atm (cdr lis))))))

(delete  'a  '(b c d a))
(delete  'the  '(the more you practice the better you will be))
(delete  'cat  '((dog cat) mouse cat (elephant) (cat) cat))
(delete  'rainy  '( the weather can be (rainy) sunny cloudy and cold))

the output I want are

  1. (b c d)
  2. (more you practice better you will be)
  3. ( (dog cat) mouse (elephant) (cat))
  4. ( the weather can be (rainy) sunny cloudy and cold)

but there are lots of wrong , please help me, thank you

0

3 Answers 3

1

You are not actually removing anything. Your procedure is normally known as remq.

The following should work (untested):

(define (delete atm lis)
  (cond
    ((null? lis) lis)
    ((eq? atm (car lis)) (delete atm (cdr lis)))
    (else (cons (car lis) (delete atm (cdr lis))))))
2
  • But if the atom I want to find isn't the first element in the list, it still has wrong. Can you tell me why?
    – Lilo
    Dec 15, 2010 at 15:33
  • 1
    @Lilo: You better tell us what's "wrong", since leppie's answer is correct.
    – erjiang
    Dec 16, 2010 at 2:25
1

The other two answers (which are identical, by the way) currently only work on the top level of the list. If you also want it to remove your atom from all nested lists, you have to search there too:

(define (delete atm lis)
 (cond
  ((null? lis) lis)
  ((eq? atm (car lis)) (delete atm (cdr lis)))
  ((list? (car lis)) (cons (delete atm (car lis)) (delete atm (cdr lis))))
  (else (cons (car lis) (delete atm (cdr lis))))))

If this is not what you want, perhaps you can specify what exactly it is that is going wrong. You keep saying that something, or lots of things, are wrong, but not specifying what that is. For instance, you could specify what you expect the output of your four examples to be.

0

You need a base case and even when you find the atm you want, you still want to continue recursing through the list.

(define (delete atm lis)
  (cond
   ((null? lis) '())
   ((eq? atm (car lis)) (delete atm (cdr lis)))
   (else (cons (car lis) (delete atm (cdr lis))))))
1
  • Oops sorry this is the same as leppie's. I tested this and it works.
    – user479988
    Dec 15, 2010 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.