diff --git a/module/srfi/srfi-13.mes b/module/srfi/srfi-13.mes index 6621b54c..4e77bd0e 100644 --- a/module/srfi/srfi-13.mes +++ b/module/srfi/srfi-13.mes @@ -96,3 +96,21 @@ (define substring/shared substring) (define string-null? (compose null? string->list)) + +(define (string-fold cons' nil' s . rest) + (let* ((start (and (pair? rest) (car rest))) + (end (and start (pair? (cdr rest)) (cadr rest)))) + (if start (error "string-fold: not supported: start=" start)) + (if end (error "string-fold: not supported: end=" end)) + (let loop ((lst (string->list s)) (prev nil')) + (if (null? lst) prev + (loop (cdr lst) (cons' (car lst) prev)))))) + +(define (string-fold-right cons' nil' s . rest) + (let* ((start (and (pair? rest) (car rest))) + (end (and start (pair? (cdr rest)) (cadr rest)))) + (if start (error "string-fold-right: not supported: start=" start)) + (if end (error "string-fold-right: not supported: end=" end)) + (let loop ((lst (reverse (string->list s))) (prev nil')) + (if (null? lst) prev + (loop (cdr lst) (cons' (car lst) prev)))))) diff --git a/tests/srfi-13.test b/tests/srfi-13.test index 14972d7a..48684033 100755 --- a/tests/srfi-13.test +++ b/tests/srfi-13.test @@ -9,7 +9,7 @@ exit $? ;;; -*-scheme-*- ;;; Mes --- Maxwell Equations of Software -;;; Copyright © 2016,2017 Jan Nieuwenhuizen +;;; Copyright © 2016,2017,2018 Jan Nieuwenhuizen ;;; ;;; This file is part of Mes. ;;; @@ -48,4 +48,12 @@ exit $? 3 (string-index "foo:bar" #\:)) +(pass-if-equal "string-fold" + "oof" + (list->string (string-fold cons '() "foo"))) + +(pass-if-equal "string-fold-right" + "f-o-o-:" + (list->string (string-fold-right (lambda (e p) (cons e (cons #\- p))) '(#\:) "foo"))) + (result 'report)