Page 1 of 1

New to lisp and file IO

Posted: Thu Aug 23, 2012 11:57 pm
by samohtvii
Hi all,

I am quite new to LISP and i am having a few problems reading from a file.
I'm not quite sure what LISP is capable of so maybe there is an easier way around this question but i am looking to create a list tree:
e.g. ((Emu)(Dog Beaver))
Firstly I read that lisp can read that straight from a file and make a list using defparameter.
So i have
1 - (defparameter *s* (open animals))
Then i get kinda lost.
2 - (read *s*) ???

anyways any help would be great.

so far my file animals has "((Emu)(Dog Beaver))" or would i have to have the animals in order and then read into the list?

Thanks

Re: New to lisp and file IO

Posted: Fri Aug 24, 2012 2:16 am
by ramarren
First, LISP is not a language, it is a family of languages. Common Lisp is one of the languages of this family, and I assume you are asking about that since this is a CL subforum, but you should be clear about the difference when writing.

The file chapter in Practical Common Lisp explains this. You should probably read at least some introductory chapters of that as well.

Re: New to lisp and file IO

Posted: Fri Aug 24, 2012 7:53 am
by saulgoode
samohtvii wrote: So i have
1 - (defparameter *s* (open animals))
Then i get kinda lost.
2 - (read *s*) ???
:
:

so far my file animals has "((Emu)(Dog Beaver))"
The effect of reading an expression from a file is basically equivalent to what you would obtain by quoting the expression.

(defparameter *s* '((Emu)(Dog Beaver)) )

*s* ==> ((EMU)(DOG BEAVER))
(car *s*) ==> (EMU)
(cadr *s*) ==> (DOG BEAVER)
(caar *s*) ==> EMU
(cadadr *s*) ==> BEAVER

Note: If you wish to retain the original case of the symbols, you should set your readtable appropriately.