newLISP Fan Club

Forum => newLISP in the real world => Topic started by: eddier on June 22, 2005, 12:55:16 PM

Title: format
Post by: eddier on June 22, 2005, 12:55:16 PM
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

(println (format "%s,%d,%s,%dn" (select L 0 4 1 7)))


vs

(println (format "%s,%d,%s,%dn" (L 0) (L 4) (L 1) (L 7))))


Implicit indexing helps, but selecting from a list would be nicer.



Eddie
Title:
Post by: Sammo on June 22, 2005, 01:38:55 PM
> (define (myformat str) (apply format (cons str (flat (args)))))

(lambda (str) (apply format (cons str (flat (args)))))



> (myformat "%s,%d,%s,%dn" (select L 0 4 1 7))

"bob,2,this,96n"
Title:
Post by: eddier on June 22, 2005, 02:20:19 PM
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