How to remove the first item from the list?

How to remove the first item from a list in a schema?

Suppose I have the following list

'((apple bob car) (cat dig) (e)))

How can I just get rid of appleand leave the rest alone?

+3
source share
4 answers

Three basic list operations in a Schema

  • cdrwhich means "rest" or "give me a list without the first element"
  • carwhich means "first" or "give me the first item in the list"
  • conswhich means adding lists

assuming s is a list ((apple bob machine) (cat dig) (e))

Intermediate steps

(car s)       ; (apple bob car)
(cdr (car s)) ; (bob car)
(cdr s)       ; ((cat dig) (e))

Final expression

(cons (cdr (car s))) (cdr s))

Result

((bob car) (cat dig) (e))
+8
source

-, , . ,

((cat dig) (e))

(apple bob car).

apple, () , cdr. , , , , ( ).

, , , . , :

(define removeFirst
  (lambda (input)
    (cond
      ((list? (car input)) (cons (removeFirst (car input)) (cdr input)))
      (else (cdr input))
    )
  )
)

> (removeFirst '((apple bob car) (cat dig) (e)))
((bob car) (cat dig) (e))
+5

, , , , - . , . , set! , .

+1

(cdr x) ( "x" - ) , . , , , apple : '((apple bob car) (cat dig) (e))). , , , (apple bob car).

To remove only the "apple", you need to find out if the first item in the list that you received was itself a list, and if it collected a list consisting of this list with the first element removed (hint: recursion), and then the remaining part of your original list. If this first item is not a list, you simply return the rest of the list.

0
source

Source: https://habr.com/ru/post/1737753/


All Articles