Page 1 of 1

format

Posted: Wed Jun 22, 2005 7:55 pm
by eddier
Lutz,

I've found use on several occasions to have an optional list of variables or values in format.

For instance, suppose L = ("bob" "this" "that" 3 2 10 "a" 96 "----" 456). We could write

Code: Select all

(println (format "%s,%d,%s,%d\n" (select L 0 4 1 7)))
vs

Code: Select all

(println (format "%s,%d,%s,%d\n" (L 0) (L 4) (L 1) (L 7))))
Implicit indexing helps, but selecting from a list would be nicer.

Eddie

Posted: Wed Jun 22, 2005 8:38 pm
by Sammo
> (define (myformat str) (apply format (cons str (flat (args)))))
(lambda (str) (apply format (cons str (flat (args)))))

> (myformat "%s,%d,%s,%d\n" (select L 0 4 1 7))
"bob,2,this,96\n"

Posted: Wed Jun 22, 2005 9:20 pm
by eddier
Thanks Sammo!

Works like a charm :) I didn't know I could use "apply" on functions with a variable number of arguments without defining a lambda or helper function. I can use this knowlege to make more elegant solutions for a lot of other functions as well :)

Eddie