Recursive string substitutions in lists in Scheme.
Recursive string substitutions in lists in Scheme, a dialect of Lisp.
Date Created:Tuesday September 16th, 2008 12:35 PM
Date Modified:Tuesday September 16th, 2008 12:54 PM
Here we simply pass in a list, new and old word for replacing.
(define (substitute l old new)
(if (null? l)
'()
(append
(if (list? (car l))
(list (substitute (car l) old new))
(if (equal? (car l) old)
(list new)
(list (car l))
)
)
(substitute (cdr l) old new)
)
)
)
#|
Example call:
(substitute '((ha blah) (ha (ha blah)) ((ha blah) (ha (blah ha)))) 'blah 'ha)
|#
Here we can replace a list of text recursively by passing in an old list and a new list of words to replace.
(define (substitute l old new)
(if (null? l)
'()
(append
(if (list? (car l))
(list (substitute (car l) old new))
(if (member? (car l) old)
(list (item (return-n (car l) old 1) new))
(list (car l))
)
)
(substitute (cdr l) old new)
)
)
)
(define (return-n seed haystack n)
(if (equal? (item n haystack) seed)
n
(return-n seed haystack (+ n 1))
)
)
Downloads:
Download: sub.scm 471 B
Download: sub2.scm 527 B
Please login or Click Here to register for downloads
Substitute words in lists by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com
