3

how to delete an element from a list ex:- list=[1 2 3 4]

I have come up with some code.I think I got wrong somewhere.

 (define delete item
   (lambda (list)
   (cond
    ((equal?item (car list)) cdr list)
     (cons(car list)(delete item (cdr list))))))
12

Your code is almost correct. The item also should be a parameter, so the function may begin with like this:

(define delete
  (lambda (item list)
  ...

Also, your code needs paren around the cdr list and else in the last clause. Then, the code may be like this:

(define delete
  (lambda (item list)
    (cond
     ((equal? item (car list)) (cdr list))
     (else (cons (car list) (delete item (cdr list)))))))
3
  • delete so defined is not tail-recursive. It could also be defined via fold/filter.
    – Rhangaun
    Apr 20 '10 at 5:12
  • 1
    With this procedure, if the element occurs more than once, it just deletes the first one. There can be a procedure that deletes all same elements in list.
    – Adeq Hero
    Oct 10 '12 at 2:28
  • (define (delete item list) (filter (lambda (x) (not (equal? x item))) list))
    – torus
    Mar 20 '15 at 7:25
4

Shido Takafumi wrote a tutorial about Scheme, Yet Another Scheme Tutorial. In chapter 7, exercise 1, the 3rd problem.

A function that takes a list (ls) and an object (x) as arguments and returns a list removing x from ls.

The author gave the solution code bottom of the page.

; 3
(define (remove x ls)
  (if (null? ls)
      '()
      (let ((h (car ls)))
        ((if (eqv? x h)
            (lambda (y) y)
            (lambda (y) (cons h y)))
         (remove x (cdr ls))))))

The code maybe difficult to comprehend for beginner. It's same as the code below.

(define (rm x ls)
  (if (null? ls)
      '()
      (if (eqv? x (car ls))
          (rm x (cdr ls))
          (cons (car ls)
                (rm x (cdr ls))))))

This can delete the same elements in list. :D

1

1) if consider the input list may be a simple list, or you just want to delete the item in the top-level of a nested list for example:

delete 2 from (1 2 3 4) will return (1 2 3)
delete 2 from (1 2 3 (2 3) 3 2 4) will return (1 3 (2 3) 3 4)

as we can see the 2nd example above, it just delete the item in the top-level of the nested list, within the inner list, we doesn't change it.

this code should be:

(define (deleteitem list1 item) 
( cond
    ((null? list1) ’())
    ((equal? (car list1) item) (deleteItem (cdr list1) item)) 
    (else (cons (car list1) (deleteitem (cdr list1) item)))
))

2) if consider the input list may be a nested list

for example:

input list: (1 2 3 (3 2 (2 4 (2 5 6) 2 5 6) 2 4) 2 3 (2 3 4))

and delete the element 2 in the input list

the output list should be: (1 3 (3 (3 (5 6) 5 6) 4) 3 (3 4))

and the code should be:

(define (delete2 list1 item) 
    ( cond
    ((null? list1) '())
    ((pair? (car list1)) (con (delete2 (car list1) item) (delete2 (cdr list1) item)))
    ((equal? (car list1) item) (delete2 (cdr list1) item)) 
    (else (cons (car list1) (delete2 (cdr list1) item)))
))
0

This code seems to work just fine, but only deletes an element that should be in the list:

(define (delete element lst)
    (let loop ([temp lst])
        (if (= element (car temp)) (cdr temp)
            (cons (car temp) (loop (cdr temp))))))
-3
(define (deleteItem(list item))
    (cond
    ((eq ? item (car(list)))cdr(list))
    (cons(car(list)(deleteItem(cdr list)))
    )
)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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