سؤال

I have to remove the 3rd item in a list and return the rest I feel like Im on my way to it, but I get an error. How do I finish this out?

(define main
  (lambda (ls)(cons(car ls)
               (cddr ls)))

sorry I do not know how to put the code in as every one else does for racket so it only comes in like this format. But I really need help on this one. the error i get is an arteri block saying I need 2 arguments.

هل كانت مفيدة؟

المحلول

You can try this in the body of the procedure, assuming that the list has at least three elements:

(cons (first ls)                      ; take 1st element
      (cons (second ls)               ; together with 2nd element
            (rest (rest (rest ls))))) ; grab the rest starting from 4th

The above will work in Racket. But if you prefer to use more standard procedures:

(cons (car ls)                        ; take 1st element
      (cons (cadr ls)                 ; together with 2nd element
            (cdddr ls)))              ; grab the rest starting from 4th
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top