[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Format strings are wrong

Where a basic implementation of an extend (string ...) function using
mzscheme's module system could alleviate the necessity for the most
common historical uses of (format ...) for basic string formatting:

(module string mzscheme
  (provide (rename new-string string))
  (define (new-string . params)
    (letrec ((loop (lambda (val rest space)
           (if space " " "")
             ((string? val) val)
             ((number? val) (number->string val))
             ((symbol? val) (symbol->string val))
             ((list? val) (string-append
                            "(" (loop (car val) '() #f)
                            (loop (cadr val) (cddr val) #t) ")"))
             ((char? val) (string val))
             (else (begin (error'string) "")))
           (if (pair? rest) (loop (car rest) (cdr rest) space) "")))))
      (loop (car params) (cdr params) #f))))

(require string)

(define size 3)
(define what 'apples)

(string "I have " size " " what ", a character: " #\c
        ", and a list: " '(a 3.12 1/2 (f) symbol) ".")

-> "I have 3 apples, a character: c, and a list: (a 3.12 1/2 (f) symbol)."