This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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))))) |
0 件のコメント:
コメントを投稿