如何使用字符串中的格式多次调用函数以写入common lisp中的文件?

如何使用字符串中的格式多次调用函数以写入common lisp中的文件?,lisp,common-lisp,Lisp,Common Lisp,我正在尝试为Flam3创建一个包含多个表单的xml文件。我是CommonLisp的新手,我相信有更好的方法来完成我的任务。 与其多次调用sexy函数,我怎么能做一个快速而肮脏的循环呢 以下是相关代码: ;What the xml file should look like ;A nice little template (defun sexy () (format nil "<flame time=\"~d\" palette=\"~d\" zoom=\"~d\" size=\"640

我正在尝试为Flam3创建一个包含多个表单的xml文件。我是CommonLisp的新手,我相信有更好的方法来完成我的任务。 与其多次调用sexy函数,我怎么能做一个快速而肮脏的循环呢

以下是相关代码:

;What the xml file should look like 
;A nice little template
(defun sexy ()
 (format nil
"<flame time=\"~d\" palette=\"~d\" zoom=\"~d\" size=\"640 480\" center=\"~d ~d\"   background=\"0 0 0\" brightness=\"~d\" gamma=\"~d\" vibrancy=\"3\" hue=\"~d\">
<xform weight=\"~f\" color=\"~f\" spherical=\"~d\" coefs=\"~f ~f ~f ~f ~f ~f\"/>
<xform weight=\"~f\" color=\"~f\" julia=\"~d\" coefs=\"~f ~f ~f ~f ~f ~f\"/>
<xform weight=\"~f\" color=\"~f\" rings=\"~d\" coefs=\"~f ~f ~f ~f ~f ~f\"/>
<xform weight=\"~f\" color=\"~f\" butterfly=\"~d\" coefs=\"~f ~f ~f ~f ~f ~f\"/>
</flame>" 
(random 100) (random 100) (random 3) (random 4) (random 4) (random 255) (random 10)  (random 10) 
(random 1.0) (random 1.0) 1 (random 1.0) (random 1.0) (random 1.0) (random 1.0) (random    1.0) (random 1.0) 
(random 1.0) (random 1.0) 1 (random 1.0) (random 1.0) (random 1.0) (random 1.0) (random 1.0) (random 1.0) 
(random 1.0) (random 1.0) 1 (random 1.0) (random 1.0) (random 1.0) (random 1.0) (random 1.0) (random 1.0) 
(random 1.0) (random 1.0) 1 (random 1.0) (random 1.0) (random 1.0) (random 1.0) (random 1.0) (random 1.0) 
))

;Actually write the file using the sexy function
;But call it 10 times to create 10 different structures with random values
(defun xml-maker ()
  (with-open-file (my-stream "test.flam3" :direction :output 
             :if-exists :supersede)
      (format my-stream
"<test> ~a ~a ~a ~a ~a ~a ~a ~a ~a ~a </test>" (sexy) (sexy) (sexy) (sexy) (sexy) (sexy)      (sexy) (sexy) (sexy) (sexy) )))

;Just call the program with the newly written file
(defun fract-maker ()
 (progn (xml-maker)
    (ext:shell "flam3-render < test.flam3")))
;xml文件应该是什么样子的
;一个漂亮的小模板
(德芬性感)
(无格式)
"
" 
(随机100)(随机100)(随机3)(随机4)(随机4)(随机255)(随机10)(随机10)
(随机1.0)(随机1.0)1(随机1.0)(随机1.0)(随机1.0)(随机1.0)(随机1.0)(随机1.0)(随机1.0)(随机1.0)
(随机1.0)(随机1.0)1(随机1.0)(随机1.0)(随机1.0)(随机1.0)(随机1.0)(随机1.0)(随机1.0)(随机1.0)
(随机1.0)(随机1.0)1(随机1.0)(随机1.0)(随机1.0)(随机1.0)(随机1.0)(随机1.0)(随机1.0)(随机1.0)
(随机1.0)(随机1.0)1(随机1.0)(随机1.0)(随机1.0)(随机1.0)(随机1.0)(随机1.0)(随机1.0)(随机1.0)
))
;实际使用sexy函数编写文件
;但是调用它10次,创建10个不同的随机值结构
(定义xml生成器()
(打开文件(我的流“test.flam3”):方向:输出
:如果存在:取代)
(格式化我的流
“~a~a~a~a~a~a~a~a~a”(性感)(性感)(性感)(性感)(性感)(性感)(性感)(性感)(性感)(性感)(性感)(性感)(性感)))
;只需使用新编写的文件调用程序
(除压裂机()
(progn(xml生成器)
(分机:外壳“火焰3渲染<测试火焰3”))

你的意思是这样的吗:

(format my-stream "<test>~{ ~A~} </test>"
    (loop
        :repeat 10 
        :collecting (sexy)))
(格式化我的流“~{~A~}”
(环路
:重复10次
:收集(性感)

为什么不直接输出

(format t "~a ~a" (format nil "$~a$" 10) (format nil "$~a$" 20))
(format t "$~a$" 10)
(format t " ")
(format t "$~a$" 20)
上面生成两个字符串,然后外部格式打印它们

为什么不直接打印呢

(format t "~a ~a" (format nil "$~a$" 10) (format nil "$~a$" 20))
(format t "$~a$" 10)
(format t " ")
(format t "$~a$" 20)
甚至

(format t "$~a$ $~a$" 10 20)
或者类似的

(format t "~{ $~a$~}" '(10 20))

问题是什么?如何重复调用函数
sexy
?是的。在sexy函数上调用loop do时,它不会将字符串添加到文件中。我使用nil参数希望它只返回字符串,但这不起作用。哈哈,谢谢。。但愿不是真的;)我想我不太明白。我还能将字符串写入文件吗?@masterofnothing:commonlisp中的输出进入流。只需将流传递给周围的人即可。在输出函数中创建字符串是浪费的,而且在大多数情况下是不必要的。啊,我很清楚这是有意义的。。。谢谢