This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Print something in file

10 posts · 4384 views

hello everyone,

I want to do this in lisp :

<code> <tab> <the true char> <tab> <In lisp representation >
65 . A . #\A
66 . B . #\B
67 . C . #\C
68 . D . #\D
69 . E . #\E
70 . F . #\F


And I want to put this into a file.

Behold what I tried to do:
(defun alpha(a b f)
(setq file(open f :direction :output))
(loop
(cond
((equal a b)(return))
((print (code-char a)file)(incf a))
)))
This program is supposed to print all charcteres between a and b. And to put them into a file f.
For exemple, for (alpha 65 70 file), I'am suppose to get what I wrote before :

<code> <une tabulation> <the true char> <tab><In lisp representation >
65 . A . #\A
66 . B . #\B
67 . C . #\C
68 . D . #\D
69 . E . #\E
70 . F . #\F

I know that program alpha is false. Maybe I have to do something like this :
(print (string-concat  a (code-char 9)(string(code-char a) (code-char a)))
But string concat send me a an error with a because it's not a string.
I don't know if you understand what I want to do, but all your help is needed !!!

Sorry for my english, I am actually french so ...

Thank you.

Re: Print something in file

alcibiade123 wrote:hello everyone,

I want to do this in lisp :

<code> <tab> <the true char> <tab> <In lisp representation >
65 . A . #\A
66 . B . #\B
67 . C . #\C
68 . D . #\D
69 . E . #\E
70 . F . #\F


And I want to put this into a file.

Behold what I tried to do:
(defun alpha(a b f)
(setq file(open f :direction :output))
(loop
(cond
((equal a b)(return))
((print (code-char a)file)(incf a))
)))
This program is supposed to print all charcteres between a and b. And to put them into a file f.
For exemple, for (alpha 65 70 file), I'am suppose to get what I wrote before :

<code> <une tabulation> <the true char> <tab><In lisp representation >
65 . A . #\A
66 . B . #\B
67 . C . #\C
68 . D . #\D
69 . E . #\E
70 . F . #\F

I know that program alpha is false. Maybe I have to do something like this :
(print (string-concat  a (code-char 9)(string(code-char a) (code-char a)))
But string concat send me a an error with a because it's not a string.
I don't know if you understand what I want to do, but all your help is needed !!!

Sorry for my english, I am actually french so ...

Thank you.
Do you want to print out the whole table, or only the characters?
Don't take the FUN out of DEFUN !

Re: Print something in file

Hi everyone,

Thank you for your answer, behold what I found :

(defun alphabet (a b)(setq b (+ b 1))
(loop
(cond
((equal a b)(return))
((princ a)(princ #\tab)(princ(code-char a))(princ #\tab)(prin1 (code-char a))(terpri)(incf a))
)))

With this function I have exacty what I wanted :

(alphabet 65 70)

65 A #\A
66 B #\B
67 C #\C
68 D #\D
69 E #\E
70 F #\F

But I always want to put this result into a file...
To write something into a file I found this on my course :

(setq file(open "file" :direction :output))

So I did this :

(print "dodo" file)
"dodo"

The file is created but if I do more file in my prompt, I have nothing.

more file
>
I don't know why it doesn't work ...
I wanted to use this method to put "alphabet" into a file but like "dodo" I have nothing into my file.

Sorry for my english.. again

Re: Print something in file

@Indecipherable

Yes I want to print the whole table.

Re: Print something in file

(defun tabulate-characters (a b &optional (out *standard-output*))
  (loop for cc from a below b
        do (format out "~D~C~A~C#\\~:C~%"
                   cc
                   #\Tab
                   (code-char cc)
                   #\Tab
                   (code-char cc))))

(defun print-characters (a b dest)
  (with-open-file (d dest
                     :direction :output
                     :if-exists :supersede
                     :if-does-not-exist :create)
    (tabulate-characters a b d)))
Note that the #\Tab character is semi-standard.

Cheers
--
MA
Marco Antoniotti

Re: Print something in file

Thank you marcoxa, your solution works very well.
But can you tell me why my function didn't work ? I mean for the (setq file(open "file" :direction : output)) Because when I tried to adapt my function to yours with this :
(defun  alphabet (a b &optional (out *standard-output*))
   (setq b (+ b 1))
       (loop                 
             (cond             
                     ((equal a b)(return))
                     ((princ a)(princ #\tab)(princ(code-char a))(princ #\tab)(prin1 (code-char a))(terpri)(incf a))
              )
       )
)
(setq file(open "file" :direction :output))
    (defun print-characters (a b dest)
          (with-open-file(d dest
                                          :direction :output
                                          :if-exists :supersede 
                                          :if-does-not-exist :create)                                                                   
                                          (alphabet a b d)))
It didn't work. So why ???
Can somebody please tell me why ? It's realy important for me to understand my mistakes.

Re: Print something in file

Well. We have to figure out *where* and *how* it did not work. To begin with, you are not passing the output stream to your output functions (PRINC and PRIN1).

MA
Marco Antoniotti

Re: Print something in file

Two consistent problems in your code are that you're using setq where you should be using let, and that you're using C-style parenthesis closing instead of lisp-style. The latter issue is just a stylistic one, but it does make it harder for us to read your code.
I think you should also re-read the documentation for with-open-file, because (setq file(open "file" :direction :output)) is unnecessary.

After that, it would probably help if you reasoned through your code, describing to yourself exactly what you expect to happen and why. I find this very useful myself, because it often shows exactly where I've gone wrong.

Re: Print something in file

another solution:
(defun alpha (start end fname)
  (with-open-file (*standard-output*
                   fname
                   :direction :output
                   :if-exists :supersede)
                  (loop for i from start to end
                        do (format t "~{~a~4t~a~4t~a~%~}" (list i
                                                                (code-char i)
                                                                (prin1-to-string (code-char i)))))))