最初の要素は0番目。
(define delete-nth
(lambda (lis n)
(let loop ((c 0) (l lis) (r '()))
(if (null? l)
(reverse r)
(if (= c n)
(loop (+ c 1) (cdr l) r)
(loop (+ c 1) (cdr l) (cons (car l) r)))))))
(print (iota 10))
(print (delete-nth (iota 10) 3))
(print '(a b c d e f g h i j))
(print (delete-nth '(a b c d e f g h i j) 3))
^o^ > gosh delete-nth.scm (0 1 2 3 4 5 6 7 8 9) (0 1 2 4 5 6 7 8 9) (a b c d e f g h i j) (a b c e f g h i j)
ちゃんと 3 と d が消えている。
これを拡張して、「n番目」を複数とれるようにしてみよう。
(define delete-nth
(lambda (lis . ns)
(let loop ((c 0) (l lis) (r '()))
(if (null? l)
(reverse r)
(if (include? c ns)
(loop (+ c 1) (cdr l) r)
(loop (+ c 1) (cdr l) (cons (car l) r)))))))
(define include?
(lambda (x lis)
(cond ((null? lis) #f)
((= x (car lis)) #t)
(else (include? x (cdr lis))))))
(print (iota 10))
(print (delete-nth (iota 10) 3 5 7))
(print '(a b c d e f g h i j))
(print (delete-nth '(a b c d e f g h i j) 3 5 7))
^o^ > gosh delete-nth2.scm (0 1 2 3 4 5 6 7 8 9) (0 1 2 4 6 8 9) (a b c d e f g h i j) (a b c e g i j)
要素がリストに含まれているか否かを判定する述語が見当たらなかったので、include? を自作した。