nan
nan

Reputation: 57

How to delete an element from a list in scheme

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))))))

Upvotes: 4

Views: 44348

Answers (6)

57one
57one

Reputation: 1

Thank @ad absurdum for pointing out my problems

delete element from a list without nested lists


bad code:

(define (remove item lst)
  (define (filter-lst l)
      (cond
           ((null? l) nil)
           ((= item (car l)) (filter-lst (cdr l)))
           (else (cons (car l) (filter-lst (cdr l)))))
  )
  (if (null? lst) () (filter-lst lst))
)
;;; Tests
(remove 2 '(4 3 2))
; expect (4 3)
(remove 3 nil)
; expect ()
(remove 3 '(1 3 5))
; expect (1 5)
(remove 5 '(5 5 1 4 5 4))
; expect (1 4 4)

the code above tested in Berkeley CS61A scheme interpreter


(define (remove item lst)
  (cond 
    ((null? lst) '())
    ((equal? item (car lst)) (remove item (cdr lst)))
    (else (cons (car lst) (remove item (cdr lst)))))
)
(remove 2 '(1 3 2))
; expect (1 3)
(remove 3 '())
; expect ()
(remove 3 '(1 3 5))
; expect (1 5)
(remove 5 '(5 3 5 5 1 4 5 4))
; expect (3 1 4 4)

the code above tested in chez scheme interpreter

Upvotes: -1

Jarvis
Jarvis

Reputation: 8564

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))))))

Upvotes: 0

Jie
Jie

Reputation: 177

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)))
))

Upvotes: 1

tisyang
tisyang

Reputation: 313

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

Upvotes: 5

Mafas
Mafas

Reputation: 1

(define (deleteItem(list item))
    (cond
    ((eq ? item (car(list)))cdr(list))
    (cons(car(list)(deleteItem(cdr list)))
    )
)

Upvotes: -4

torus
torus

Reputation: 1049

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)))))))

Upvotes: 13

Related Questions