Posted By

magicrebirth on 02/09/10


Tagged

recursion scheme substring namedlet


Versions (?)

Scheme: find a substring


 / Published in: Scheme
 

  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
Posted By: magicrebirth on February 11, 2010

parses a string searching for char "\n"; the search starts at position 'topvalue' and continues by decreasing this value (= towards the beginning of the text)

You need to login to post a comment.