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

/ StringRecipes / Cookbook.StringEval

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

Evaluating Scheme Code in a String

Problem

You want to eval Scheme code stored in a string

Solution

Use eval-string from string.ss:

> (require (lib "string.ss"))
> (eval-string "(+ 1 2)")
3

Discussion

(String input ports and read)

See Also

StringInterpolation

-- NoelWelsh - 28 Apr 2004

I was going to base this topic on the Python Cookbook topic, which was a bit different in approach - given a string with embedded expressions, interpolate the evaluated expressions into the string.

>>> number = 20
>>> text = "python"
>>> print "%(text.capitalize())s %(number/9.0).1f rules!" % Eval()
Python 2.2 rules!
So essentially, it's a simple templating system. The problem with using read=/=eval in this case is that you need some sort of escaping system. I'm thinking that you could do this with fprintf and override the port-print-handler, like this:
> (define a '(1 2))
> (define eval-print
    (lambda (obj port) 
      ((global-port-print-handler) (eval obj) port)))
> (port-print-handler (current-output-port) eval-print)
> (fprintf (current-output-port) "~A ~s ~v~n" a a a)
(+ 1 2) (+ 1 2) 3

-- GordonWeakliem - 28 Apr 2004

The solution is at http://people.cs.uchicago.edu/~jacobm/qstr/ This is known as StringInterpolation, right? I think we have a new recipe

-- NoelWelsh - 06 May 2004

CookbookForm
TopicType: Recipe
ParentTopic: StringRecipes
Other Parents:
Next Topic: AligningStrings

 
 
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