Skip to content

Instantly share code, notes, and snippets.

@Lifelovinglight
Last active August 22, 2016 14:57
Show Gist options
  • Save Lifelovinglight/456335a3328e696c4c357ab91b6cb889 to your computer and use it in GitHub Desktop.
Save Lifelovinglight/456335a3328e696c4c357ab91b6cb889 to your computer and use it in GitHub Desktop.
Improved implementation of space intercalation for chatbot
(use-modules (srfi srfi-1))
(define (ae string)
(if (string-null? string)
string
(let loop ((result (make-string (1- (* 2 (string-length string)))))
(index 0)
(len (string-length string))
(blank-line #t))
(if (= index len)
(if blank-line
(make-string 0)
result)
(let ((char (string-ref string index)))
(string-set! result (* 2 index) char)
(when (< index (1- len))
(string-set! result (1+ (* 2 index)) #\space))
(loop result (1+ index) len (not (or (not blank-line)
(not (char-whitespace? char))))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment