如何从方案中的列表中删除元素

发布于 2024-08-15 01:23:34 字数 229 浏览 1 评论 0 原文

如何从列表中删除元素 ex:- list=[1 2 3 4]

我想出了一些代码。我想我在某个地方出错了。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

错爱 2024-08-22 01:23:34

你的代码几乎是正确的。
item 也应该是一个参数,因此函数可以这样开头:

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

此外,您的代码需要在 cdr listelse 周围加上括号在最后一句中。
那么,代码可能是这样的:

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

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)))))))
凉墨 2024-08-22 01:23:34

Shido Takafumi 写了一篇关于Scheme 的教程,Yet Another Scheme Tutorial
第7章中,练习1,第三个问题。

以列表 (ls) 和对象 (x) 作为参数并返回的函数
从 ls 中删除 x 的列表。

作者在页面底部给出了解决方案代码。

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

对于初学者来说,代码可能很难理解。
与下面的代码相同。

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

这可以删除列表中相同的元素。 :D

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

给我一枪 2024-08-22 01:23:34

1)如果考虑输入列表可能是一个简单列表,或者您只想删除嵌套列表顶层的项目
例如:

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)

正如我们在上面的第二个示例中看到的,它只是删除嵌套列表顶层的项目,在内部列表中,我们不更改它。

此代码应该是:

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

2)如果考虑输入列表可能是嵌套列表,

例如:

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

并删除输入列表中的元素 2

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

,代码应该是:

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

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)))
))
心凉怎暖 2024-08-22 01:23:34

这段代码似乎工作得很好,但只删除了应该在列表中的元素:

(define (delete element lst)
    (let loop ([temp lst])
        (if (= element (car temp)) (cdr temp)
            (cons (car temp) (loop (cdr temp))))))

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))))))
不…忘初心 2024-08-22 01:23:34

感谢@adabsurdum指出我的问题

从没有嵌套列表的列表中删除元素


错误代码:

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

上面的代码在Berkeley CS61A方案解释器中测试


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

上面的代码在chez方案解释器中测试

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

甩你一脸翔 2024-08-22 01:23:34
(define (deleteItem(list item))
    (cond
    ((eq ? item (car(list)))cdr(list))
    (cons(car(list)(deleteItem(cdr list)))
    )
)
(define (deleteItem(list item))
    (cond
    ((eq ? item (car(list)))cdr(list))
    (cons(car(list)(deleteItem(cdr list)))
    )
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文