Scheme: find a substring


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



Copy this code and paste it in your HTML
  1. (let loop ((i 0)
  2. (j 20) ;; comparison value: it decreases at each recursion (except the first one)
  3. (topvalue 20)) ;; komodo value : must be equal to j at the beginning
  4. (let ((txt "Toyota denies previous banking problem is safety"))
  5. (if (equal? (- topvalue i) j) ;; the first time
  6. (loop (+ i 1) j topvalue)
  7. (begin (print (substring txt (- topvalue i) j))
  8. (if (string=? (substring txt (- topvalue i) j) " ")
  9. (string-append (substring txt 0 (- topvalue i))
  10. "\n"
  11. (substring txt (- topvalue i) (string-length txt)))
  12. (if (< i topvalue) ;;avoid negative indexes in substring
  13. (loop (+ i 1) (- j 1) topvalue)))))))

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.