删除第一个元素的方案函数

发布于 2024-08-31 04:57:30 字数 76 浏览 3 评论 0原文

编写一个方案函数,删除给定项目的第一个顶级出现 从项目列表中。 例如给定列表(abc)项目b,结果列表(ac)

请帮助我

write a scheme function that remove the first top level occurrence of a given item
from a list of items.
eg given list (a b c) item b, result list (a c)

plz help me

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

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

发布评论

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

评论(6

静若繁花 2024-09-07 04:57:30

想想你想实现什么目标。

您有一个内容列表,并且正在尝试删除某个元素。

 example: trying to remove b
 (a a a a b a a b a ...)
 anything before first b should be kept and anything after it also..
 so we have to spit out from our function:
 a a a a + a a b a ...

如果我们将其简化为递归操作:

 at any point looking through the list you can:

 1. inspect element at front of the list and give it out to the result
    and recursively inspect the rest of the list
 2. stop if you found your element and 
    give out the rest of the list as you've accomplished your task

Think what are you trying to accomplish.

You have a list of stuff and you are trying to remove a certain element.

 example: trying to remove b
 (a a a a b a a b a ...)
 anything before first b should be kept and anything after it also..
 so we have to spit out from our function:
 a a a a + a a b a ...

if we were to reduce this to operating with recursion:

 at any point looking through the list you can:

 1. inspect element at front of the list and give it out to the result
    and recursively inspect the rest of the list
 2. stop if you found your element and 
    give out the rest of the list as you've accomplished your task
云裳 2024-09-07 04:57:30

不确定你想要什么,但首先简单地从一个索引开始,这几乎就是你必须用Scheme“思考”的方式,首先从“如果它是第一个元素怎么办?”开始,答案当然是它应该是列表的其余部分。然后“好吧,如果它不是第一个怎么办”,那么答案是“它应该限制第一个,直到将相同过程应用于其余部分的结果。”,这就是方案在这方面需要的所有信息,并且在很多情况下真的。

    (define (slice-out lst k)
      (if (<= k 0) (cdr lst) ; if we want to remove the first (0) element, surely the result is simply the tail of the list?
          (cons (car lst) ; if it's higher than 0, we just cons the first element...
                (slice-out (cdr lst) (- k 1))))) ; to the result of the same method applied to the tail but with one lower k.

> (slice-out '(a b c d e) 2)
===>(a b d e)

如果列表对于索引来说太短,则此函数将返回错误。

然而,如果你想通过与另一个对象的某种相等性来切片,这个例子就足够了,我们现在不再将它切片到我们达到 0 的地方,但如果它与搜索示例相同:

(define (slice-out-by-equality lst search)
  (if (equal? (car lst) search) (cdr lst)
      (cons (car lst)
            (slice-out-by-equality (cdr lst) search))))

> (slice-out-by-equality '(a b c d e) 'c)
===> (a b d e)

使用完全相同的原理,但是这个会返回如果未找到该项目,则会出现错误。

重点是,scheme 有多种形式的相等比较,所以,我们真正想要的是这样的:

(define (make-slice-out comparison)
  (lambda (lst search)
    (let loop ((lst lst))
      (cond 
        ((null? lst) '())
        ((comparison (car lst) search) (cdr lst))
        (else (cons (car lst) (loop (cdr lst))))))))

这个例子展示了Scheme 的全部内容,不确定你是否了解它,但我们在这里使用了一个闭包,这个函数实际上将任何二进制比较函数作为参数,然后计算出您想要的函数,它也被清理了,如果找不到它,它不会再犯错误,它返回它只是返回旧列表,因为如果它到达末尾列表,还没有删除任何内容,它只是再次将其精简为 () 。

> ((make-slice-out =) '(1 2 3 6 3) 6)
===> (1 2 3 3); we just made an anonymous function here.

但是记住我们原来的函数,我们现在可以在提供谓词“等于?”时像这样简单地定义它。我们的新函数实际上评估我们的旧函数(重要的是它现在已经被清理):

(define slice-out-by-equality (make-slice-out equal?))

而且,还有更多的二进制比较,这个更奇特的例子怎么样:

(define slice-out-less-than (make-slice-out <))

我们用这种方式创建了一个函数,它切掉第一个严格小于的元素比我们的搜索词大,因此这是有效的:

> (slice-out-less-than '(573 284 238 174 92 47) 100)
====> (573 284 238 174 47)

尽管 47 也小于 100,但 92 是其中的第一个。

Not sure what you want, but first start it simply with an index, this is pretty much how you have to 'think' with Scheme, first start with 'What if it were the first element?', the answer then is of course that it should be the rest of the list. And then 'Well, what if it's not the first', then the answer is 'It should cons the first up to the result of the same procedure applied to the rest.', that's all the info Scheme needs in this, and many cases really.

    (define (slice-out lst k)
      (if (<= k 0) (cdr lst) ; if we want to remove the first (0) element, surely the result is simply the tail of the list?
          (cons (car lst) ; if it's higher than 0, we just cons the first element...
                (slice-out (cdr lst) (- k 1))))) ; to the result of the same method applied to the tail but with one lower k.

> (slice-out '(a b c d e) 2)
===>(a b d e)

This function returns an error if the list is too short for the index.

However, if you want to slice out by some equality to another object, this example suffices, we now no longer slice it out of we reach 0, but if it's identical to a search example:

(define (slice-out-by-equality lst search)
  (if (equal? (car lst) search) (cdr lst)
      (cons (car lst)
            (slice-out-by-equality (cdr lst) search))))

> (slice-out-by-equality '(a b c d e) 'c)
===> (a b d e)

Using the very same principles, This one however returns an error if the item isn't found.

Point is that scheme has many flavours of equality comparisons, so, what we actually want is this:

(define (make-slice-out comparison)
  (lambda (lst search)
    (let loop ((lst lst))
      (cond 
        ((null? lst) '())
        ((comparison (car lst) search) (cdr lst))
        (else (cons (car lst) (loop (cdr lst))))))))

This example shows what Scheme is all about, not sure if you're known with it, but we've used a closure here, this function actually takes as argument any binary comparison function and then evaluate to the function you want, it's also sanitized, it doesn't make an error any more if it's not found, it returns it simply returns the old list because if it reaches the end of the list, with nothing removed yet, it just conses it up to () again.

> ((make-slice-out =) '(1 2 3 6 3) 6)
===> (1 2 3 3); we just made an anonymous function here.

But remembering our original function, we can now define it simply like this, when supplying the predicate 'equal?' our new function actually evaluates to our old function (with the important asset that it's now sanitized):

(define slice-out-by-equality (make-slice-out equal?))

And, there's more binary comparison, how about this more exotic example:

(define slice-out-less-than (make-slice-out <))

We made a function this way that slices out the first element that is strictly less than our search term, thus this works:

> (slice-out-less-than '(573 284 238 174 92 47) 100)
====> (573 284 238 174 47)

Even though 47 is also less than 100, 92 is the first of those that is.

末蓝 2024-09-07 04:57:30

有那些功能 car 和 cdr 允许您获取列表的一部分。函数append 允许您将两个列表合并为一个。我想它们可能会派上用场。还要检查 cons 函数并确保您了解列表实际上是什么以及它与对有什么关系。

例如,您可以按如下方式进行。拿出你的清单,剪下第一个元素并检查它是什么。如果是要删除的,请将其丢弃。如果它不是要删除的元素,请处理列表的其余部分,然后将该元素附加回开头。

There are those function car and cdr that allow You to take parts of the list. Function append allows You to joint two lists into one. I think they might come handy. Also check out the cons function and make sure You understand what a list actually is and what does it have to do with pairs.

You can proceed as follows for example. Take Your list, cut the first element out and check what it is. If it's the one to be removed discard it. If it's not the element to be removed, process the rest of the list and then append that element back at the beginning.

请叫√我孤独 2024-09-07 04:57:30

像这样的东西(如果它是作业):

(define (remove-first-occurence some-list find-symbol accum)
  (cond 
    [(empty? some-list) accum]
    [else (cond
           [(= (first some-list) find-symbol) (cons accum (rest some-list))]
           [else (remove-first-occurence (rest some-list) find-symbol (cons (first some-list) accum))]
           )]))

(remove-first-occurence '(1 2 3 4 3) 3 empty)

Something like this (if it is a homework):

(define (remove-first-occurence some-list find-symbol accum)
  (cond 
    [(empty? some-list) accum]
    [else (cond
           [(= (first some-list) find-symbol) (cons accum (rest some-list))]
           [else (remove-first-occurence (rest some-list) find-symbol (cons (first some-list) accum))]
           )]))

(remove-first-occurence '(1 2 3 4 3) 3 empty)
等风来 2024-09-07 04:57:30

(定义(删除首次出现列表元素累加)
(条件
((空?列表)累加)
(别的
(cond((= (汽车列表) 元素) (cons Accum (cdr 列表)))
(else(删除首次出现(cdr列表)元素(cons(汽车列表)accum)))



)

(删除首次出现 '(1 2 3) 2 '())

(define (remove-first-occurence list element accum)
(cond
((null? list) accum)
(else
(cond((= (car list) element) (cons accum (cdr list)))
(else(remove-first-occurence (cdr list) element(cons (car list) accum)))
)
)
)
)

(remove-first-occurence '(1 2 3) 2 '())

岁月流歌 2024-09-07 04:57:30
perl2scheme -s \
'use strict; sub remove_first { \
grep { $_ ne $_[0] || $first++ } @{ $_[1] }; } \
print join(",",remove_first("b", ("a","b","c"));'

实现 perl2scheme 的剩余琐碎任务留给读者作为练习。

perl2scheme -s \
'use strict; sub remove_first { \
grep { $_ ne $_[0] || $first++ } @{ $_[1] }; } \
print join(",",remove_first("b", ("a","b","c"));'

The trivial remaining task of implementing perl2scheme is left as an excercize for the reader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文