【问题标题】:Writing output to file. LISP [duplicate]将输出写入文件。 LISP [重复]
【发布时间】:2014-11-17 18:37:33
【问题描述】:

我编写了一个 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,我可以通过哪些运算符?

【问题讨论】:

  • 您的大部分代码与该问题无关。您可能希望使用较短的示例。

标签: file-io stream lisp common-lisp


【解决方案1】:

您可以通过更改动态变量来重定向标准输出:

(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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 2012-11-01
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    相关资源
    最近更新 更多