Loading [MathJax]/extensions/tex2jax.js

2010年7月19日月曜日

Insert an element at a given position into a list

;; P21 (*) Insert an element at a given position into a list.
;; Example:
;; * (insert-at 'alfa '(a b c d) 2)
;; (A ALFA B C D)
(define (insert-at elm lst pos)
(let loop ((lst lst) (pos pos) (acc '()))
(if (or (= pos 1) (null? lst))
(append (reverse acc) (cons elm lst))
(loop (cdr lst) (- pos 1) (cons (car lst) acc)))))
view raw p21.ss hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿