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.

Reading a file, store it and display it

6 posts · 6582 views

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:
(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:
MotCle01 MotResultat01
MotCle02 MotResultat02
MotCle03 MotResultat03
MotCle04 MotResultat04

Re: Reading a file, store it and display it

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

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

You're not getting any error messages on the console?

Re: Reading a file, store it and display it

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.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: Reading a file, store it and display it

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