How to delete an element from a list in scheme

↘锁芯ラ 提交于 2019-12-18 06:57:28

问题


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

回答1:


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



回答2:


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




回答3:


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



回答4:


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



回答5:


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


来源:https://stackoverflow.com/questions/1905222/how-to-delete-an-element-from-a-list-in-scheme

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!