LISP - write a list to file -


i need write numeric list file , put return @ end of line.

i have tried code work first element of list.

(defun write-segment (filename segment)   (cond ((null segment)          (with-open-file (out filename                                :direction :output                                :if-exists :append                                :if-does-not-exist :create)            (format out "~%")))         (t (with-open-file (out filename                                  :direction :output                                  :if-exists :append                                  :if-does-not-exist :create)              (format out "~d " (first segment))              (write-segment filename (cdr segment)))))) 

some 1 can me resolve problem?

from description , code, not 100% sure if following fits after, try anyway. code in snippet walks list of numerical values , writes them output file:

(defun write-numeric-list(filename l)   (with-open-file (out filename :direction :output :if-exists :append :if-does-not-exist :create)     (dolist (segment l)       (format out "~d " segment))     (format out "~%"))) 

sample call:

(write-numeric-list "output.txt" (list 1 2 -42)) 

this code opens output file once entire list, instead of once each element of list, in original version. may want adjust :if-exists , :if-does-not-exist options depending on preconditions in particular situation.

in fact, format can walk list own, using advanced format control strings. control strings aren't everybody's cup of tea, reference, here version of code using them:

(defun write-numeric-list(filename l)   (with-open-file (out filename :direction :output :if-exists :append :if-does-not-exist :create)     (format out "~{~d ~}~%" l))) 

Comments