将输出写入文件。 LISP

时间:2014-11-17 18:37:33

标签: file-io stream lisp common-lisp

我编写了一个LISP程序,它通过princ和write-line运算符将一些输出数据写入CommonLisp控制台。如何重做它将输出数据写入文件? 我的节目:

(defun printTriangle()
    (
        progn
        (let((countx xmin)(county ymin)(koeff1 nil)(koeff2 nil))
            (loop
                (cond ((> county ymax)(return "")))
                (loop
                    (cond ((> countx xmax)(return "")))
                    (if (equal (- countx (car(nth (- (length line1)1) line1))) 0)(setf divisionByZeroKoef1 1)(setf divisionByZeroKoef1 0))
                    (if (equal (- countx (cadar line1)) 0)(setf divisionByZeroKoef2 1)(setf divisionByZeroKoef2 0))
                    (if (or(equal divisionByZeroKoef1 1)(equal divisionByZeroKoef2 1))
                        (
                            progn
                            (setf koeff1 (- county (cadr(nth (- (length line1)1) line1))))
                            (setf koeff2 (- county (caar line1)))
                            (if (or
                                    (and                        
                                        (and(>= (- koeff1 koeffHyp1)-0.8)(<= (- koeff1 koeffHyp1)0.8))
                                        (and(>= (- koeff2 koeffHyp2)-0.8)(<= (- koeff2 koeffHyp2)0.8))
                                        (<= ymin county)
                                        (>= ymax county)
                                        (<= xmin countx)
                                        (>= xmax countx)
                                    )
                                    (equal (memberList line1 (list countx county)) 1)

                                )
                                (princ "*")
                                (princ "-")
                            )
                        )
                        (
                            progn
                            (setf koeff1 (/(- county (cadr(nth (- (length line1)1) line1)))(- countx (car(nth (- (length line1)1) line1)))))
                            (setf koeff2 (/(- county (caar line1))(- countx (cadar line1))))
                            (if (or
                                    (and                                        
                                        (and(>= (- koeff1 koeffHyp1)-0.8)(<= (- koeff1 koeffHyp1)0.8))
                                        (and(>= (- koeff2 koeffHyp2)-0.8)(<= (- koeff2 koeffHyp2)0.8))
                                        (<= ymin county)
                                        (>= ymax county)
                                        (<= xmin countx)
                                        (>= xmax countx)
                                    )
                                    (equal (memberList line1 (list countx county)) 1)
                                )
                                (princ "*")
                                (princ "-")
                            )
                        )
                    )
                    (setf countx (+ countx 1))
                )
                (setf countx xmin)
                (write-line "")
                (setf county (+ county 1))
            )
            (print "The triangle is draw!")
        )
    )
)

我可以通过哪些操作符代替princ和write-line?

1 个答案:

答案 0 :(得分:4)

您可以通过更改动态变量来重定向stdout:

(with-open-file (*standard-output*
                 "my-file-name.txt"
                 :direction :output
                 :if-exists :supersede)
  (print-triangle))

您可以更改print-triangle以获取输出流。

(defun print-triangle (&optional out)
  (princ "output" out))

(with-open-file (handle
                 "my-file-name.txt"
                 :direction :output
                 :if-exists :supersede)
  (print-triangle handle))