Skip to content

Instantly share code, notes, and snippets.

@Yoxem
Last active April 30, 2016 19:48
Show Gist options
  • Save Yoxem/969b68746647db6b171c880bcc94f5fc to your computer and use it in GitHub Desktop.
Save Yoxem/969b68746647db6b171c880bcc94f5fc to your computer and use it in GitHub Desktop.
;;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