Skip to content

Instantly share code, notes, and snippets.

@WillNess
Last active December 23, 2015 08:19
Show Gist options
  • Select an option

  • Save WillNess/6606865 to your computer and use it in GitHub Desktop.

Select an option

Save WillNess/6606865 to your computer and use it in GitHub Desktop.
rplac1 - replace 1st occurrence in nested list
(define (rplac1 xs a b)
(let g ((xs xs) (f #f) (k (lambda (x y) x))) ; http://ideone.com/AbWKxS
(cond
(f (k xs f)) ; shortcut!
((null? xs) (k xs f)) ; http://stackoverflow.com/q/16550176/849891
((not (pair? xs)) ; http://stackoverflow.com/q/16444290/849891
(if (eq? xs a) (k b #t) (k xs f))) ; not f!
(else
(g (car xs) f (lambda (x f)
(g (cdr xs) f (lambda (y f)
(if (not f)
(k xs f)
(k (cons x y) f))))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment