scheme: replace a string

/ Published in: Scheme
Save to your folder(s)

Written for the impromptu-scheme environment.. other schemes might need different procedures..

Copy this code and paste it in your HTML
  1. (define string-replace
  2. (lambda (s match replacement)
  3. (let ((ll (string->list s))
  4. (match1 (char match))
  5. (replacement1 (char replacement)))
  6. (if (= (string-length match) 1)
  7. (let ((z (map (lambda (x)
  8. (if (equal? x match1)
  9. replacement1
  10. x))
  11. ll)))
  12. (list->string (list-flatten z)))
  13. ;z)
  14. (print "i can match only single characters for now")))))

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.