How to include a file

Discussion of Common Lisp
nerobot
Posts: 5
Joined: Tue May 11, 2010 3:35 am

How to include a file

Post by nerobot » Tue May 11, 2010 3:39 am

Hi,

Sorry if this has already been asked somewhere else, but I was unable to find a solution anywhere.

Is there a way to include a file, containing lisp code, into the current file. To explain a bit more, I need something that would simply place the whole contents of the include file, as is, into the main file (as if it had been copied and pasted).

i.e.

////MainFile.lisp
...
...
(include population.lisp)
...
...

/// population.lisp
'(list 1)
'(list 2)
...ect


Thanks for any advice, and please let me know if there is any more information needed.

Steven

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: How to include a file

Post by ramarren » Tue May 11, 2010 4:51 am

It is in principle possible, but why would you want to do it? Common Lisp is image based, there is almost no difference between just loading the file and including it this way. It would mostly just confuse source-form locating for no gain whatsoever.

For managing multi-file projects use of ASDF is recommended (small tutorial).

nerobot
Posts: 5
Joined: Tue May 11, 2010 3:35 am

Re: How to include a file

Post by nerobot » Wed May 12, 2010 9:00 am

The reason I needed to do this was because I had a file containing a load of lists that needed to input into the current file so that they could then be used for calling a function. But I instead found a way of placing the lists (which spanned several lines) into a list of lists. However, how still need to know how to expand this so that each contained list can be sent as a separate lists (i.e. sending 20 lists instead of one list containing 20 lists). Not really sure if that makes any sense, but never mind.

Steven

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: How to include a file

Post by nuntius » Wed May 12, 2010 9:07 am

Code: Select all

(dolist (sublist lists) (send sublist))
?

nerobot
Posts: 5
Joined: Tue May 11, 2010 3:35 am

Re: How to include a file

Post by nerobot » Thu May 13, 2010 3:40 am

Unfortunately, dolist will also return a NIL, which the function couldn't handle.

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: How to include a file

Post by ramarren » Thu May 13, 2010 4:22 am

It is hard to determine what your problem is, since you appear to be confused about basic operations of the language and programming in general. What did you use to learn Common Lisp from? Practical Common Lisp is a good free book.

For example:
nerobot wrote:input into the current file
A file is a sequence of bytes/characters. You can write output to file, or read input from file, but it is not clear what "input into" file would mean. And why are you even trying to expand in place in source forms during read/macro time, rather than read data using standard IO operations during runtime?

nerobot
Posts: 5
Joined: Tue May 11, 2010 3:35 am

Re: How to include a file

Post by nerobot » Thu May 13, 2010 4:48 am

Ramarren wrote:It is hard to determine what your problem is, since you appear to be confused about basic operations of the language and programming in general. What did you use to learn Common Lisp from? Practical Common Lisp is a good free book.

For example:
nerobot wrote:input into the current file
A file is a sequence of bytes/characters. You can write output to file, or read input from file, but it is not clear what "input into" file would mean. And why are you even trying to expand in place in source forms during read/macro time, rather than read data using standard IO operations during runtime?
I'm perfectly aware that a file is a sequence of bytes / characters. I have written most of the program without any problems, including reading and writing to files. However, the file that I needed to input contained a set of lists, each spanning several lines. Because the lists spanned several (different for each lisp), I couldn't simply perform a file input operation as this would only give me the individual line or bytes (not the whole list). However, I then read about using "read" instead of "read-line" or "read-char", which then allowed me to read each of the lisps separately into the a variable. Now, I didn't want to perform a "read" operation for each list, separately, the in the file as there are 500 different lists, which would be quite inefficient.

I've read several books on using LISP and have been program with it for several years, but there are still things that I find difficult about it.

So to redefine my problem:

I have now created a variable list containing 500 lists obtained from the file. I now need to send these, individually to a function, rather than as one list.

Sorry if this is starting to sound a bit forceful, or worse ignorant.

Steven

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: How to include a file

Post by ramarren » Thu May 13, 2010 5:07 am

My apologies, I just found it unlikely that someone could have learnt Lisp without understanding how the reader works.
nerobot wrote:Now, I didn't want to perform a "read" operation for each list, separately, the in the file as there are 500 different lists, which would be quite inefficient.
What would be inefficient about that? Any nontrivial processing would dominate the reader runtime anyway. And reading a single list with a number of sublists would not be in any way different than reading those sublists from a stream in a loop.

Code: Select all

(with-open-file (file "somefile" :direction :input)
  (loop with sentinel = (gensym)
        for sublist = (read file nil sentinel)
        until (eql sublist sentinel)
        collect sublist))
(Not sure if this is idiomatic LOOP, ITERATE has file iteration built in, but apparently LOOP doesn't)
nerobot wrote:I have now created a variable list containing 500 lists obtained from the file. I now need to send these, individually to a function, rather than as one list.
What does "send to a function" mean in this context? Functions are called with arguments. Nuntius provided one way to call a function with every element of a list. There is also MAP/MAPCAR which will return a list of results.

nerobot
Posts: 5
Joined: Tue May 11, 2010 3:35 am

Re: How to include a file

Post by nerobot » Thu May 13, 2010 5:20 am

No problem. Perhaps if I send a piece of the code that I've got written it will be better than my crappy explanation :)

(setf in (open "FirstGeneration"))



(run-genetic-programming-system 'AREA-CIRCLE (random 100.0) 10000 *number-of-population*

(second (read in))
(second (read in))
(second (read in))
(second (read in))
(second (read in))
(second (read in))
(second (read in))
(second (read in))
(second (read in))
(second (read in))
(second (read in))
(second (read in))
(second (read in))
(second (read in))
...
...
...)


Now I have had to write "(second (read in)) 500 times (that is what I meant by inefficient, not in running, but in actually writing it down).

If I could do this in a loop then that would be better as my next program would use 5000 lists instead of 500. But dotimes returns NIL after finishing, which is also sent to the function.

Steven

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: How to include a file

Post by ramarren » Thu May 13, 2010 5:37 am

Why not just something like what I shown in previous post?

Code: Select all

(with-open-file (file "somefile" :direction :input)
  (loop with sentinel = (gensym)
        for sublist = (read file nil sentinel)
        until (eql sublist sentinel)
        do
     (run-genetic-programming-system
         AREA-CIRCLE
         (random 100.0)
         10000
         *number-of-population*
         (second sublist))))
nerobot wrote:If I could do this in a loop then that would be better as my next program would use 5000 lists instead of 500. But dotimes returns NIL after finishing, which is also sent to the function.
Again, and sorry if this is due to some kind of language barrier, but this is confusing me, functions are not something that can be sent to. Functions are called with arguments, and return values. You have not explained what are you trying to do with the return value of the form, if it is not called purely for side effects, in which case return value of DOLIST would be irrelevant. Whatever it is, you most likely have to do that inside the loop, or collect all results and then loop over them again.

Post Reply