0

This code

 (defun arabic_to_roman (filename) (let ((arab_roman_dp '()) (arab nil) (roman nil)) (with-open-file (in filename :direction :input :if-does-not-exist nil) (when in (loop for line = (read-line in nil) while line do (setq arab (subseq line 0 (search "=" line))) (setq roman (subseq line (1+ (search "=" line)) (length line))) (setf arab_roman_dp (acons arab roman arab_roman_dp)) ;(format t "~S ~S~%" arab roman) ))) (with-open-file (stream #p"ar_out.txt" :direction :output :if-exists :overwrite :if-does-not-exist :create ) (write arab_roman_dp :stream stream :escape nil :readably nil)) 'done!)) 

seems to work well. It takes a file with entries like this

1=I 2=II ... 

and builds one large list of dotted pairs. However when I look at the output file, it seems as though soft returns or newlines have been inserted.

((4999 . MMMMCMXCIX) (4998 . MMMMCMXCVIII) (4997 . MMMMCMXCVII) (4996 . MMMMCMXCVI) (4995 . MMMMCMXCV) (4994 . MMMMCMXCIV) (4993 . MMMMCMXCIII) (4992 . MMMMCMXCII) (4991 . MMMMCMXCI) (4990 . MMMMCMXC) ... 

I was expecting the output to look more like just one continuous line:

((4999 . MMMMCMXCIX) (4998 . MMMMCMXCVIII) (4997 . MMMMCMXCVII) (4996 . MMMCMXCVI) (4995 . MMMMCMXCV) (4994 . MMMMCMXCIV) (4993 . MMMMCMXCIII) (4992 . MMMMCMXCII) (4991 . MMMMCMXCI) (4990 . MMMMCMXC) ... 

Is my code the way it is indeed throwing in newlines somehow? I've used the write version of princ which supposedly suppresses newlines. Later I want to read this file back into the program as just one big list, so I don't want newline issues.

1
  • 1
    If you use the Lisp reader to read it back, newlines are not an issue, anyway. Commented Oct 29, 2015 at 10:42

2 Answers 2

6

It looks like the pretty-printer is being invoked (the default is implementation-dependent), to print it with indentation and human-readable line lengths. Use :pretty nil to disable this.

(write arab_roman_dp :stream stream :escape nil :readably nil :pretty nil) 
Sign up to request clarification or add additional context in comments.

Comments

-1

A better way to write it:

  • use functions to create blocks of code, which can be combined
  • less side effects and fewer variables
  • no need to test for in
  • easy to understand control flow

Example:

(defun arabic_to_roman (filename) (flet ((read-it () (with-open-file (in filename :direction :input :if-does-not-exist nil) (loop for line = (read-line in nil) for pos = (position #\= line) while line collect (cons (subseq line 0 pos) (subseq line (1+ pos)))))) (write-it (list) (with-open-file (stream #p"ar_out.txt" :direction :output :if-exists :overwrite :if-does-not-exist :create) (write list :stream stream :escape nil :readably nil :pretty nil)))) (write-it (read-it)) 'done-read-write)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.