Page 1 of 1
Reading a file, store it and display it
Posted: Sun Mar 06, 2016 5:07 pm
by phdenis40
Hello all,
I'm a newbie in LISP and I'm face to several problems. I'm trying to read a text file and store it in a list thanks to a lisp program but it's not working.
Could you please tell me what's wrong with this script?
lisp code:
Code: Select all
(defun parser ()
;Initialisation de la liste dico
(setq dico '())
;Initialisation d'une liste ligne
(setq ligne '(hello))
;Lecture du fichier dico
(with-open-file (stream "./dico.txt"))
(loop while (not (null ligne))
(setq ligne (read-line str))
(setq dico (cons ligne dico))
)
)
Text file to parse:
Code: Select all
MotCle01 MotResultat01
MotCle02 MotResultat02
MotCle03 MotResultat03
MotCle04 MotResultat04
Re: Reading a file, store it and display it
Posted: Mon Mar 07, 2016 12:01 pm
by David Mullen
You're using the extended form of LOOP, which requires each Lisp form or series of forms to be preceded by a keyword. WHILE is one such keyword, and DO is another—that's what you need here:
Code: Select all
(loop while (not (null ligne))
do (setq ligne (read-line str))
(setq dico (cons ligne dico)))
Secondly, the with-open-file doesn't enclose the code that's reading from the stream. Outside of with-open-file, the file isn't open, and the stream variable doesn't exist. It needs to be like this:
Code: Select all
(with-open-file (stream "./dico.txt")
(loop while (not (null ligne))
do (setq ligne (read-line stream))
(setq dico (cons ligne dico))))
Finally, you probably also want to return the
dico result from the function.
Re: Reading a file, store it and display it
Posted: Mon Mar 07, 2016 1:41 pm
by phdenis40
Thanks for the answer, now the syntax of the program is correct but the behavior is quite strange for me.
You're right about the fact that I want to be able to return the list "dico" because I want to parse it in order to find an atom, or display all atoms.
So, why on the parser function, when I invoke the format instruction nothing happen to my console?
I'm wondering :
- For the 1st / 2nd function, how to pass or return a variable without passing by global variable?
Code: Select all
(defun parser ()
;Initialisation de la liste dico
(setq dico '())
;Initialisation d'une liste ligne
(setq ligne '(hello))
;Lecture du fichier dico
(with-open-file (stream "./dico.txt"))
(loop while (not (nil ligne))
do (setq ligne (read-line stream))
(format t "Field 01: ~S~% and associated Field 02: ~S~%"
car(ligne) cdr(ligne))
(setq dico (cons ligne dico)))
)
(defun display(MyListe)
(cond
((eq MyListe nil) nil)
;Parcours de la base
(listp (car MyListe) (cdr MyListe))
;Affiche le car et le cdr de MyListe
(print (car MyListe) (cdr MyListe)))
)
Re: Reading a file, store it and display it
Posted: Tue Mar 08, 2016 3:22 pm
by David Mullen
You're not getting any error messages on the console?
Re: Reading a file, store it and display it
Posted: Thu Mar 10, 2016 1:04 pm
by Goheeca
The definition of a function in Common Lisp consists of
- defun
- the name of function
- a list of arguments
- and an implicit progn
The value of a
progn expression is the last expression in that
progn. That means if you want return the 'dico' in your function, just write it down as a last expression in that function.
Re: Reading a file, store it and display it
Posted: Fri Mar 11, 2016 5:10 am
by phdenis40
Hello,
Unfortunately, I've no error message.
Nothing is printed on the screen linked to the parsing of the text file.
Do you know if a Lisp Debugger is available?
I'm quite lost ...