0

I am trying to create a procedure to delete n number of elements from a list starting at an index i, so far this is what I got:

(define (remove L i n)
 (cond ((null? L)
        '())
       ((and (= i 0) (= n 0))
        L)
       (else (cons (car L) (remove (cdr L) (- i 1) (+ n 1))))

I might be missing a condition in there but I am kinding of confused.

1 Answers1

0

You have to check/decrement i and n separately

(define (remove L i n)
  (cond ((null? L)
         empty)
        ((> i 0)
         (cons (car L) (remove (cdr L) (sub1 i) n)))
        ((> n 0)
         (remove (cdr L) i (sub1 n)))
        (else
         L)))

(remove '(0 1 2 3 4 5 6) 3 2)
;; => '(0 1 2 5 6)

(remove '(0 1 2 3 4 5 6) 5 100)
;; => '(0 1 2 3 4)

(remove '(0 1 2 3 4 5 6) 100 200)
;; => '(0 1 2 3 4 5 6)

(remove '() 0 10)
;; => '()
Mulan
  • 129,518
  • 31
  • 228
  • 259
  • can you please tell me which class this is for ? – Mulan Nov 01 '17 at 05:30
  • hey @naomik this is for a functional programming class that I am taking. I was actually trying to delete n number of elements from a list at an index and then appending another list to it. I tried what you suggested and I got it working –  Nov 02 '17 at 00:31
  • Click the checkmark underneath the up- and down-vote buttons. It should turn green. – Alexis Dumas Nov 02 '17 at 00:40