LISP 71
Replace function Guest on 13th November 2021 11:35:05 PM
  1. ;;replace second param with first param
  2. (defun replace (new old l)
  3.   (cond
  4.    ( (null l) l)
  5.    ( (eq old (car l)) (cons new( cdr l)))
  6.    ( t ( (cons (car l) ( replace new old (cdr l)))))
  7.    )
  8.   )
  9. ;;demo replace
  10. (replace 3 6 '(6 5 7))
  11. ;(3 5 7)
  12.  
  13. ;;recursion replace all second param with first param
  14. (defun replaceall (new old l)
  15.   (cond
  16.    ( (null l) l)
  17.    ( (eq old (car l)) (cons new (replaceall new old (cdr l))))
  18.    ( t (cons (car l) (replaceall new old (cdr l))))
  19.   )
  20. )
  21. ;;demo replaceall
  22. (replaceall 3 6 '(6 5 7 6))
  23. ;(3 5 7 3)

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.