'removing last element of a list(scheme)

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



Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[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)

Solution 4:[4]

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

Solution 5:[5]

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

Solution 6:[6]

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

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

Solution 7:[7]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Clements
Solution 2 Tim
Solution 3 Chris Jester-Young
Solution 4
Solution 5
Solution 6 ryanwebjackson
Solution 7 Aspen