Print something in file

Discussion of Common Lisp
Post Reply
alcibiade123
Posts: 4
Joined: Fri Jun 03, 2011 6:32 pm

Print something in file

Post by alcibiade123 » Fri Jun 03, 2011 7:02 pm

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:

Code: Select all

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

Code: Select all

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

Indecipherable
Posts: 47
Joined: Fri Jun 03, 2011 5:30 am
Location: Behind you.
Contact:

Re: Print something in file

Post by Indecipherable » Sat Jun 04, 2011 5:23 am

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:

Code: Select all

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

Code: Select all

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

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: Print something in file

Post by edgar-rft » Sat Jun 04, 2011 9:23 am

Hints: lookup the syntax of LOOP and FORMAT (and then use FORMAT instead of PRINT)

See also from Practical Common Lisp:
If you feel adventurous read also:
Happy Lisp hacking...

- edgar

alcibiade123
Posts: 4
Joined: Fri Jun 03, 2011 6:32 pm

Re: Print something in file

Post by alcibiade123 » Wed Jun 08, 2011 5:20 pm

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

alcibiade123
Posts: 4
Joined: Fri Jun 03, 2011 6:32 pm

Re: Print something in file

Post by alcibiade123 » Wed Jun 08, 2011 7:43 pm

@Indecipherable

Yes I want to print the whole table.

marcoxa
Posts: 85
Joined: Thu Aug 14, 2008 6:31 pm

Re: Print something in file

Post by marcoxa » Thu Jun 09, 2011 5:38 am

Code: Select all

(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

alcibiade123
Posts: 4
Joined: Fri Jun 03, 2011 6:32 pm

Re: Print something in file

Post by alcibiade123 » Tue Jun 14, 2011 5:05 pm

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 :

Code: Select all

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

marcoxa
Posts: 85
Joined: Thu Aug 14, 2008 6:31 pm

Re: Print something in file

Post by marcoxa » Wed Jun 15, 2011 1:23 am

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

JamesF
Posts: 98
Joined: Thu Jul 10, 2008 7:14 pm

Re: Print something in file

Post by JamesF » Wed Jun 15, 2011 4:19 pm

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.

paradigmshift
Posts: 1
Joined: Mon Jun 20, 2011 6:22 am

Re: Print something in file

Post by paradigmshift » Mon Jun 20, 2011 6:28 am

another solution:

Code: Select all

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

Post Reply