删除列表的最后一个元素(方案)

发布于 2024-10-17 15:47:58 字数 239 浏览 2 评论 0原文

所以我必须删除方案中列表的最后一个元素。

例如,假设我有一个列表 (1 2 3 4)。我需要返回:

(1 2 3)

我的想法:

reverse(list)
car(list)
reverse(list)

scheme(racket)中有reverse函数吗?

So I have to remove the last element of a list in scheme.

For example, let's say I have a list (1 2 3 4). I need to return:

(1 2 3)

My idea:

reverse(list)
car(list)
reverse(list)

Is there a reverse function in scheme(racket)?

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

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

发布评论

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

评论(9

诠释孤独 2024-10-24 15:47:58

你写道:“倒车,汽车,倒车”。我相信你的意思是写“反向,cdr,反向”。这个解决方案没有任何问题;它与列表的大小成线性关系,就像使用标准列表的任何解决方案一样。

作为代码:

;; all-but-last: return the list, not including the last element
;; list? -> list?
(define (all-but-last l) (reverse (cdr (reverse l))))

如果列表的多次遍历或不必要的另一个列表副本的构造让您烦恼,您当然可以通过直接编写来避免它。

鉴于您的几乎解决方案,我将假设这不是家庭作业。

在球拍中,它看起来像这样:

#lang racket

(require rackunit)

;; all-but-last : return the list, except for the last element
;; non-empty-list? -> list?
(define (all-but-last l)
  (cond [(empty? l) (error 'all-but-last "empty list")]
        [(empty? (rest l)) empty]
        [else (cons (first l) (all-but-last (rest l)))]))

(check-equal? (all-but-last '(3 4 5))
              '(3 4))

You wrote: "reverse, car, reverse". I believe you meant to write "reverse, cdr, reverse". There's nothing wrong with this solution; it's linear in the size of the list, just like any solution to this that uses the standard lists.

As code:

;; all-but-last: return the list, not including the last element
;; list? -> list?
(define (all-but-last l) (reverse (cdr (reverse l))))

If the multiple traversal of the list or the needless construction of another list copy bothers you, you can certainly avoid it, by writing the thing directly.

Given your almost-solution, I'm going to assume that this isn't homework.

Here's what it would look like, in racket:

#lang racket

(require rackunit)

;; all-but-last : return the list, except for the last element
;; non-empty-list? -> list?
(define (all-but-last l)
  (cond [(empty? l) (error 'all-but-last "empty list")]
        [(empty? (rest l)) empty]
        [else (cons (first l) (all-but-last (rest l)))]))

(check-equal? (all-but-last '(3 4 5))
              '(3 4))
娇纵 2024-10-24 15:47:58

SRFI 1(使用 在 Racket 中激活(需要 srfi/1 )) 有一个 drop-right 函数:

 (drop-right '(1 2 3 4) 1)   ; => (1 2 3)

SRFI 1 (activate in Racket using (require srfi/1)) has a drop-right function:

 (drop-right '(1 2 3 4) 1)   ; => (1 2 3)
自由如风 2024-10-24 15:47:58

有一个reverse,但是使用它不会很有效。我建议使用以下递归函数。

(define (remove-last lst)
    (if (null? (cdr lst))
        '()
        (cons (car lst) (remove-last (cdr lst)))))

(remove-last '(1 2 3 4)) ; returns '(1 2 3)

if 检查它是否位于列表的最后一个元素。

There is a reverse, but using it would not be very efficient. I suggest the following recursive function.

(define (remove-last lst)
    (if (null? (cdr lst))
        '()
        (cons (car lst) (remove-last (cdr lst)))))

(remove-last '(1 2 3 4)) ; returns '(1 2 3)

The if checks whether it is at the last element of the list.

风筝有风,海豚有海 2024-10-24 15:47:58

我会执行一个递归函数,如果后面的元素不是最后一个元素,则沿着列表向下移动并附加该元素(使用 cons),如果不是最后一个元素,则不附加任何内容。

我已经很多年没有做过计划了,所以这就是我能做的。

有人可以运行如何实现它(除非这是家庭作业,否则他们可能不应该!)

I would do a recursive function that goes down the list and attaches the element (using cons) if the element after it is not the last, and appends nothing if it isn't.

I haven't done scheme for years though so that's as far as I can go.

Someone can run with how to implement it (unless it's homework then they probably shouldn't!)

溺孤伤于心 2024-10-24 15:47:58

我做了一些比以下更简单的事情:reverse(list),car(list),reverse(list)来获取最后一个元素,请查看:

(define (last-one liste)
  (if(null? (cdr liste))
     null
     (cons (car liste) (last-one (cdr liste)))
  )
)

I've done something simpler than: reverse(list), car(list), reverse(list) to get the last element, check out:

(define (last-one liste)
  (if(null? (cdr liste))
     null
     (cons (car liste) (last-one (cdr liste)))
  )
)
溺ぐ爱和你が 2024-10-24 15:47:58

正在寻找其他方法的人可以检查一下:

(define (removing-last xx)
(remove (list-ref xx (- (length xx) 1)) xx))

Those who are looking for another way can check this out:

(define (removing-last xx)
(remove (list-ref xx (- (length xx) 1)) xx))
明月松间行 2024-10-24 15:47:58

我会编写一个简单的递归,将典型的“empty?mylist”基本情况更改为“empty?(rest mylist)”,这样当输入列表只有 1 个元素时我可以返回空。

(define (removelast mylist)
  (cond
    [(empty? (rest mylist)) empty]
    [(cons? mylist) (cons (first mylist) (removelast (rest mylist)))]))

(removelast (list 1 2 3 4 5))

顺便说一句,这段代码位于 Racket/PLT Scheme 中,它是 Scheme 的子集。

I would write a simple recursion, altering the typical "empty? mylist" base case to "empty? (rest mylist)," so that I can return empty when the input list is only 1 element.

(define (removelast mylist)
  (cond
    [(empty? (rest mylist)) empty]
    [(cons? mylist) (cons (first mylist) (removelast (rest mylist)))]))

(removelast (list 1 2 3 4 5))

By the way, this code is in Racket/PLT Scheme, a subset of Scheme.

挽容 2024-10-24 15:47:58
(define
  (exceptLast ls)
  (reverse (cdr (reverse ls)))
)
(define
  (exceptLast ls)
  (reverse (cdr (reverse ls)))
)
铜锣湾横着走 2024-10-24 15:47:58

(已编辑:名称已更改;在空和单元素列表上发出错误信号)

方案列表是可变的,因此可以删除多元素列表的最后一个元素:

(define (remove-last-pair-from-multi-element-list! xs)
  (define who "remove-last-pair-from-multi-element-list!")
  (cond
    [(null? xs) (error who "cannot remove nonexistent pair" xs) ]
    [(not (pair? xs)) (error who "argument is not a list" xs) ]
    [(or (null? (cdr xs)) (not (pair? (cdr xs))))
     (error who "cannot remove last pair from single element list" xs) ]
    [else
     (let next-x ([p xs] [h (cdr xs)])
       (cond
         [(eq? p h) (error who "circularity in list" xs) ]
         [(or (null? (cddr p)) (not (pair? (cddr p))))
          (set-cdr! p '())
          xs ]
         [else (next-x (cdr p)
                       (if (and (pair? h) (pair? (cdr h)))
                           (cddr h)
                           xs)) ])) ]))
                      
(define was1-4 (list 1 2 3 4))
(display was1-4) (newline)
(remove-last-pair-from-multi-element-list! was1-4)
(display was1-4)

可以在 DrRacket 中尝试此方案代码,方法是在其前面添加前缀:

#lang r6rs
(import (rnrs) (rnrs mutable-pairs))

For #langracket,可以尝试将 set-cdr! 替换为
不安全版本

(Edited: name changed; signals error on null and single element lists)

Scheme lists are mutable, so the last element of a multi-element list can be removed:

(define (remove-last-pair-from-multi-element-list! xs)
  (define who "remove-last-pair-from-multi-element-list!")
  (cond
    [(null? xs) (error who "cannot remove nonexistent pair" xs) ]
    [(not (pair? xs)) (error who "argument is not a list" xs) ]
    [(or (null? (cdr xs)) (not (pair? (cdr xs))))
     (error who "cannot remove last pair from single element list" xs) ]
    [else
     (let next-x ([p xs] [h (cdr xs)])
       (cond
         [(eq? p h) (error who "circularity in list" xs) ]
         [(or (null? (cddr p)) (not (pair? (cddr p))))
          (set-cdr! p '())
          xs ]
         [else (next-x (cdr p)
                       (if (and (pair? h) (pair? (cdr h)))
                           (cddr h)
                           xs)) ])) ]))
                      
(define was1-4 (list 1 2 3 4))
(display was1-4) (newline)
(remove-last-pair-from-multi-element-list! was1-4)
(display was1-4)

This Scheme code can be tried in DrRacket by prefixing it with:

#lang r6rs
(import (rnrs) (rnrs mutable-pairs))

For #lang racket, one could try replacing set-cdr! with
an unsafe version.

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