[plt-scheme] format string with named parts

From: Eli Barzilay (eli at barzilay.org)
Date: Wed Feb 10 13:41:34 EST 2010

On Feb 10, Todd O'Bryan wrote:
> On Tue, Feb 9, 2010 at 10:40 PM, Eli Barzilay <eli at barzilay.org> wrote:
> >
> > Basic output:
> >
> >  #lang at-exp scheme
> >  (require scribble/text)
> >  (define foo "FOO")
> >  (define bar "BAR")
> >  (output @list{@foo and @bar})
> >
> > (It's easy to make it return a string instead, I just didn't provide
> > another function (or a keyword argument or something) because there
> > was no need and I didn't see any interface that was obviously better.)
> >
> > This is using the same functionality that the web server templates are
> > using, but you don't need the extra overhead of using the web server
> > or separate files.  Here's how you'd do it:
> >
> >  #lang scribble/text
> >  @(define foo "FOO")
> >  @(define bar "BAR")
> >  @foo and @bar
> >  @(define (choose x y) @list{@x before @y or @y before @|x|?})
> >  @choose[foo bar]
> >
> 
> The nice thing about the Python version is that you can just have a
> string with placeholders for values you're going to include
> later. Am I correct that I really need to write a function (like
> choose above) that expects the values I want to go into the string
> so that the @-exprs don't get evaluated before they have values?
> 
> Obviously, that's not much more difficult, but my brain just didn't
> go there automatically. Clearly, I need to spend more time
> programming in Scheme. :-)

I'm not following the problem there.  I wrote a function just to show
that you can, I could have had just this

  #lang scribble/text
  @(define foo "FOO")
  @(define bar "BAR")
  @foo and @bar
  @foo before @bar or @bar before @|foo|?

with the same output.

The text is basically just code, so it follows the same rules.  For
example, if you want to plug in values into (+ 1 2) later, then you
make that into a (lambda (x y) (+ x y)) function...

(Also, you could use promises to delay evaluation of any part, if this
is what you're after.)

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!


Posted on the users mailing list.