Mercenary85 wrote:nuntius wrote:Yes. (setf x y) stores y into the place currently known as x.
So my above code should work then?
The code you created is very close to a solution, don't give up yet

. But, unfortunately, it does not do anything since
read-integers-from-file is not defined yet. Maybe you meant the function
read-setup to be that function? In this case you need to rename your function to
read-integers-from-file or the function you are calling to
read-setup.
In any case,
read-setup will not read the integers from the file. Nothing is returned by your call to
loop; in order to make
loop to construct and return a list, you should use collect.
For example, all the numbers are printed, but only an empty list is returned (
nil = empty list):
Code: Select all
cl-user> (loop for i from 1 to 10
do (print (list i)))
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10) nil
On the other hand, here a list with numbers is created and returned:
Code: Select all
cl-user> (loop for i from 1 to 10
collect i)
(1 2 3 4 5 6 7 8 9 10)
Another thing: you don't need to use
coerce. The function
read does not return a line from the file, it returns the integers directly.
One thing is not clear: the format of the file. You said that the file contains only numbers, which, to me, means something like this:
but your buddy's code suggest that the file contains a list of numbers enclosed by parenthesis, which is this:
In the first case, you need a loop. In the second case, you just need a single read from the file, because the function
read will automatically construct a list when it encounters parenthesis.
By the way, your buddy's code has a bunch of problems, I don't recommend you to use it:
- He uses variables (meant to be local) without declaring them with let;
- He uses variables (meant to be global) without declaring them using defvar or defparameter, like you did with *imalist*;
- He opens a file and doesn't close it afterwards. This is not a problem in you code because you are using with-open-file