format

Q&A's, tips, howto's
Locked
eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

format

Post 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

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post 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"

eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post 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

Locked