菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
297
0

scheme的split实现

原创
05/13 14:22
阅读数 73227

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

然后,先这样把。

发表评论

0/200
297 点赞
0 评论
收藏