(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))))))
;;; -*-scheme-*-
;;; Mes --- Maxwell Equations of Software
-;;; Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
+;;; Copyright © 2016,2017,2018 Jan Nieuwenhuizen <janneke@gnu.org>
;;;
;;; This file is part of Mes.
;;;
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)