views:

201

answers:

4

Given an expression '(lambda (x) x) How can I translate this into a string. I thought symbol->string will do the job but no it cant not a symbol.

e.g for a macro to-string: (to-string (lambda(x) x)) this should return >> "(lambda (x) x)"

Any ideas folks Thanks

A: 

You should walk though Conces. When new con starts write "(" when ends write ")" and use symbol->string for symbols inside conses.

You can extend this with type dispatching. May be pretty print exists in scheme too?

Trickster
Thanks Trickster for the quick response. I thought of this solution too but then I dont know how to deal with cases of variadic functions e.g'(lambda (x . y) (+ x y)) how to actually know the . between x and y and output "(lambda (x . y) (+ x y)) "
well, (x y) is (cons x (cons y nil)) and (x . y) is (cons x y)
Trickster
+1  A: 

Use pretty-format:

(pretty-format v [columns]) → string?
Frank Krueger
unfortunately the Scheme implementation I am using doesnot implement pretty-format :( Any other alternative?
+3  A: 

The expression: '(lambda (x) x)

Is a quoted list.

The expression (lambda (x) x)

Is some kind of compiled, opaque, executable, internal object of the runtime.

symbol->string simply converts a symbol to a string, which is a sequence of characters.

If you're working with a list, you can simply walk the list and print the individual components out. In fact (write '(lambda (x) x)) will simply print the list out.

Many schemes have something akin to (with-output-to-string ... ) that returns a string of all the output written to the standard port.

However, if you do (write (lambda (x) x)), you'll get who knows what. You'll get whatever the implementation provides when dumping an executable function type. Some may print a "disassembly" showing the source code. Others may simply print "#function" or something equally useless.

In short, if you just want to print out a list, there are all sorts of mechanisms for that.

If you want to print out the source code of a compiled function, that's a completely different problem, very implementation dependent, and may well be impossible.

Will Hartung
+2  A: 

Standard Scheme (at least in the R5RS sense) has no way of doing this, so if you want portable code, you need to walk the structure yourself. Tedious, but not too complicated (even for dotted lists). But if you just want some working version, then you should look in your implementation's manual and search for the way to do this. The answer will almost always be simple, for example, in PLT Scheme you'd use (format "~s" '(lambda ...))

Eli Barzilay
That answer is correct, though with implementations supporting SRFI 6, you can just make an output string port, and `write` to it.
Chris Jester-Young
The point is that doing so directly can in some cases be more efficient. If you're going the srfi route, then srfi-28 formalizes `format`. (And yes, the reference implementation uses srfi-6, but implementations will often use whatever builtins they have -- the mzscheme implementation of srfi-26 just uses its own `format`.)
Eli Barzilay