Reading a file, store it and display it

Discussion of Common Lisp
Post Reply
phdenis40
Posts: 15
Joined: Sun Mar 06, 2016 5:00 pm

Reading a file, store it and display it

Post by phdenis40 » Sun Mar 06, 2016 5:07 pm

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

David Mullen
Posts: 78
Joined: Mon Dec 01, 2014 12:29 pm
Contact:

Re: Reading a file, store it and display it

Post by David Mullen » Mon Mar 07, 2016 12:01 pm

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.

phdenis40
Posts: 15
Joined: Sun Mar 06, 2016 5:00 pm

Re: Reading a file, store it and display it

Post by phdenis40 » Mon Mar 07, 2016 1:41 pm

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

David Mullen
Posts: 78
Joined: Mon Dec 01, 2014 12:29 pm
Contact:

Re: Reading a file, store it and display it

Post by David Mullen » Tue Mar 08, 2016 3:22 pm

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

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Reading a file, store it and display it

Post by Goheeca » Thu Mar 10, 2016 1:04 pm

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.

phdenis40
Posts: 15
Joined: Sun Mar 06, 2016 5:00 pm

Re: Reading a file, store it and display it

Post by phdenis40 » Fri Mar 11, 2016 5:10 am

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

Post Reply