S-99-20


S-99-20 Remove the K'th element from a list.

Example:

 (remove-at '(a b c d) 2) 
 ; => (a c d) 

Solution:

 (define (remove-at xs n) 
   (if (< n 0) 
       (reverse (remove-at (reverse xs) (- n))) 
       (cond ((null? xs) '()) 
             ((zero? n) xs) 
             ((= n 1) (cdr xs)) 
             (else 
              (cons (car xs) (remove-at (cdr xs) (- n 1))))))) 

Testing:

 > (remove-at '() 3) 
 () 
 > (remove-at '(a b c d) 2) 
 (a c d) 
 > (remove-at '(a b c d) -2) 
 (a b d) 
 > (remove-at '(a b c d) 0) 
 (a b c d) 
 > (remove-at '(a b c d) 1000) 
 (a b c d)