scheme - How to delete the first and last elements of a list? -
i'm trying remove both first , last elements of list in racket. there other way of doing instead of:
(cdr (reverse (cdr (reverse my-list))))
here's 1 way it, using racket's built-in procedures:
(define my-list '(1 2 3 4 5 6 7 8 9 10)) (drop-right (rest my-list) 1) => '(2 3 4 5 6 7 8 9)
note: can use cdr
instead of rest
, rest
more idiomatic in racket. more general solution:
; remove `left` number of elements elements left side ; , `right` number of elements right side of list (define (trim lst left right) (drop-right (drop lst left) right)) (trim my-list 1 1) => '(2 3 4 5 6 7 8 9) (trim my-list 2 4) => '(3 4 5 6)
Comments
Post a Comment