Last active
April 30, 2016 19:48
-
-
Save Yoxem/969b68746647db6b171c880bcc94f5fc to your computer and use it in GitHub Desktop.
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
;;change the [n]-th item of a list [ls] to [new_val]. Return the modified list. | |
;; ([ls LIST] [n INT] [new_val ANY]) -> LIST | |
;; eg. (list-set '("a" "b" "c") 1 2) -> ("a" 2 "c") | |
(define (list-set ls n new_val) | |
(list-set-iter '() ls (modulo n (length ls)) new_val 0) | |
) | |
(define (list-set-iter ls_left ls_right n new_val counter) | |
(if (< counter n) | |
(list-set-iter | |
(append ls_left (list (car ls_right))) | |
(cdr ls_right) | |
n | |
new_val | |
(+ counter 1)) | |
(append ls_left (list new_val) (cdr ls_right)) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment