s c h e m a t i c s : c o o k b o o k

/ Cookbook.StringFormatCode

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Formatting Scheme Code

Problem

You have some Scheme code (or any S-Expression) that you want to format readably

Solution

pretty-print in mzlib is an easy way to do this. Here's what it does with a function definition that has no newlines or indentation:
> (require (lib "pretty.ss"))
> (pretty-print '(define string-pad-center (opt-lambda (a-string width (pad-char #\space)) (let* ((field-width (min width (string-length a-string))) (rmargin (floor (/ (- width field-width) 2)))) (string-pad (string-pad-right a-string (- width rmargin) pad-char) width pad-char)))))
(define string-pad-center
  (opt-lambda
    (a-string width (pad-char #\space))
    (let* ((field-width (min width (string-length a-string)))
           (rmargin (floor (/ (- width field-width) 2))))
      (string-pad
        (string-pad-right a-string (- width rmargin) pad-char)
        width
        pad-char))))

Discussion

pretty-print takes an optional second argument which is a port that the output should be printed to. pretty-print also has a number of options to control formatting. pretty-print-columns is used to set the column width for formatting, when called without arguments, it returns the current setting. pretty-print-depth is used to control how deeply the printer will inspect the structure. Using the same example as above,
> (define ugly-def '(define string-pad-center (opt-lambda (a-string width (pad-char #\space)) (let* ((field-width (min width (string-length a-string))) (rmargin (floor (/ (- width field-width) 2)))) (string-pad (string-pad-right a-string (- width rmargin) pad-char) width pad-char)))))
> (pretty-print-depth 0)      
> (pretty-print ugly-def)
(...)
> (pretty-print-depth 1)      
> (pretty-print ugly-def)
(define string-pad-center (...))
> (pretty-print-depth 2)      
> (pretty-print ugly-def)
(define string-pad-center (opt-lambda (...) (...)))

-- GordonWeakliem - 26 Apr 2004

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest