mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-23 20:15:05 +02:00
Added (list-insert!)
This commit is contained in:
parent
4e4eff037e
commit
91d8a981fb
1 changed files with 14 additions and 0 deletions
|
@ -46,3 +46,17 @@
|
|||
(new-tail (recur (delete x tail))))
|
||||
(if (eq? tail new-tail) lis (cons x new-tail)))))
|
||||
(recur lis))
|
||||
|
||||
;; Insert obj at index k of list, increasing length of list by one.
|
||||
(define (list-insert! lis k obj)
|
||||
(cond
|
||||
((null? lis) (error "list-insert, lis cannot be null"))
|
||||
((and (> k 0) (null? (cdr lis)))
|
||||
(set-cdr! lis (cons obj '())))
|
||||
((zero? k)
|
||||
(let ((old-car (car lis)))
|
||||
(set-car! lis obj)
|
||||
(set-cdr! lis (cons old-car (cdr lis)))))
|
||||
(else
|
||||
(list-insert! (cdr lis) (- k 1) obj))))
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue