zoukankan      html  css  js  c++  java
  • scheme的split实现

    再chez中并未找到一个split函数,基于尾递归,自己实现了了一个用于字符串拆分的split。

    (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下测试成功。

    例如:

    > (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")。

    然后,先这样把。

  • 相关阅读:
    动画,缩放
    本地公司
    调研 公司信息
    外贸 内贸 经商
    情感 爱情 恋爱
    H5源码
    世界 生活
    标识
    Jackson
    java数据结构
  • 原文地址:https://www.cnblogs.com/evening/p/10234085.html
Copyright © 2011-2022 走看看