SRFI-49 formatter somewhere? Sven.Hartrumpf@xxxxxx (01 Dec 2003 21:41 UTC)
Re: SRFI-49 formatter somewhere? redhog@xxxxxx (01 Dec 2003 23:37 UTC)

Re: SRFI-49 formatter somewhere? redhog@xxxxxx 01 Dec 2003 23:37 UTC

A naive formatter should be quite trivial to write. However, one that
is smart when choosing what to format as S-expressions and what to
format as I-expresions, is quite a bit trickier... Hm, now I just
couldn't get to sleep, so I wrote one instaed (it i very naive, but
works):

(define (iformat-head msg)
  (define (iformat-head sep msg)
    (if (and (pair? msg)
	     (not (pair? (car msg))))
	(let ((sl (iformat-head " " (cdr msg))))
	  (cons
	   (format
	    #f "~A~A~A"
	    sep (car msg) (car sl))
	   (cdr sl)))
	(cons "" msg)))
  (iformat-head "" msg))

(define (iformat-body ind msg)
  (cond
   ((null? msg)
    "")
   ((pair? msg)
    (string-append
     (iformat ind (car msg))
     (iformat-body ind (cdr msg))))
   (#t
    (format
     #f "~A.\n~A" ind
     (iformat ind msg)))))

(define (iformat ind msg)
  (if (not (pair? msg))
      (format #f "~A~A\n" ind msg)
      (let ((sl (iformat-head msg)))
	(format
	 #f "~A~A\n~A"
	 ind
	 (if (equal? (car sl) "")
	     "group"
	     (car sl))
	 (iformat-body (string-append ind " ") (cdr sl))))))

--
http://redhog.org
GPG Public key: http://redhog.org/PGP%20Public%20key.asc
Hi! I'm a .signature virus! Copy me into your ~/.signature to help me spread!