Page 1 of 1
Print something in file
Posted: Fri Jun 03, 2011 7:02 pm
by alcibiade123
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.
Re: Print something in file
Posted: Sat Jun 04, 2011 5:23 am
by Indecipherable
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?
Re: Print something in file
Posted: Sat Jun 04, 2011 9:23 am
by edgar-rft
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
Re: Print something in file
Posted: Wed Jun 08, 2011 5:20 pm
by alcibiade123
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
Posted: Wed Jun 08, 2011 7:43 pm
by alcibiade123
@Indecipherable
Yes I want to print the whole table.
Re: Print something in file
Posted: Thu Jun 09, 2011 5:38 am
by marcoxa
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
Re: Print something in file
Posted: Tue Jun 14, 2011 5:05 pm
by alcibiade123
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.
Re: Print something in file
Posted: Wed Jun 15, 2011 1:23 am
by marcoxa
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
Re: Print something in file
Posted: Wed Jun 15, 2011 4:19 pm
by JamesF
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
Posted: Mon Jun 20, 2011 6:28 am
by paradigmshift
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)))))))