scheme的split實現

再chez中並未找到一個split函數,基於尾遞歸,本身實現了了一個用於字符串拆分的split。app

(define split
      (lambda (str sep)
        (define loop
             (lambda (str sep result)
                  (let ((l_str (string-length str))
                        (l_sep (string-length sep)))
                    (cond
                      ((< l_str l_sep)  (cons (string-append (car result) (substring str 0 1) ) (cdr result)))
                      ((= l_str l_sep)
                       (cond
                         ((string=? (substring str 0 l_sep) sep) result)
                         (else
                           (cons
                             (string-append (car result) (substring str 0 l_sep) )
                            (cdr result)))))
                      (else
                        (cond
                          ((string=? (substring str 0 l_sep) sep) (loop (substring str l_sep l_str) sep (cons "" result)))
                          (else
                            (loop (substring str 1 l_str) sep (cons (string-append (car result) (substring str 0 1) ) (cdr result))))))))))
          (reverse (loop str sep '("")))))

在chez下測試成功。函數

例如:oop

> (split "How are you? I/m fine thank you" " ")
("How" "are" "you?" "I/m" "fine" "thank" "you")
> (split "12@345@@678@@@910" "@@")
("12@345" "678" "@910")。測試

而後,先這樣把。code

相關文章
相關標籤/搜索