I am trying to create a list with inner lists with following command but it is not working:
(list 1 2 3 (list a b c))
*** - SYSTEM::READ-EVAL-PRINT: variable B has no value
I am not sure which is variable B here.
Edit: I restarted clisp and tried again. This time it is variable A!
$ clisp
i i i i i i i ooooo o ooooooo ooooo ooooo
I I I I I I I 8 8 8 8 8 o 8 8
I \ `+' / I 8 8 8 8 8 8
\ `-+-' / 8 8 8 ooooo 8oooo
`-__|__-' 8 8 8 8 8
| 8 o 8 8 o 8 8
------+------ ooooo 8oooooo ooo8ooo ooooo 8
Welcome to GNU CLISP 2.49 (2010-07-07) <http://clisp.cons.org/>
Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2010
Type :h and hit Enter for context help.
[1]> (list 1 2 3 (list a b c))
*** - SYSTEM::READ-EVAL-PRINT: variable A has no value
The following restarts are available:
I tried to set values of variables a, b, c, but still it does not work.
[5]> (let ((a 1)(b 2)(c 3)))
NIL
[6]> (list 1 2 3 (list a b c))
*** - SYSTEM::READ-EVAL-PRINT: variable A has no value
The following restarts are available:
USE-VALUE :R1 Input a value to be used instead of A.
STORE-VALUE :R2 Input a new value for A.
ABORT :R3 Abort main loop
Break 1 [7]>
You need to assign the variables before you can use them.
(defvar a 10)
(defvar b 20)
(defvar c 30)
(list 1 2 3 (list a b c))
This should return:
(1 2 3 (10 20 30))
When you use LET, it creates local variables, and their scope is just the body of the LET.
(let ((a 1) (b 2) (c 3))
(list 1 2 3 (list a b c)))
=> (1 2 3 (1 2 3))
Related
I'm doing an algorithm that randomizes a TSP (array of citys) based on 1 TSP.
(do ((i 0 (+ i 1)))
((= i n-population))
(setf (aref population i) (shuffle TSP 100))
)
And as far as I know im filling up i positions of the array population with (shuffle TSP 100) that is beeing called each iteration, but the algorithm is setting all array positions and not just i position.
[Note. An earlier version of this answer contained a mistake which would badly alter the statistics of the shuffling: please check below for the corrected version and a note as to what the problem was.]
Given your code, slightly elaborated to turn it into a function:
(defun fill-array-with-something (population n-population TSP)
(do ((i 0 (+ i 1)))
((= i n-population))
(setf (aref population i) (shuffle TSP 100))))
Then each element of population from 0 to (1- n-population) will be set to the result of (shuffle TSP 100). There are then two possibilities:
(shuffle TSP 100) returns a fresh object from each call;
(shuffle TSP 100) returns the same object – probably TSP – from each call.
In the first case, each element of the array will have a distinct value. In the second case, all elements below n-population will have the same value.
Without knowing what your shuffle function does, here is an example of one which will give the latter behaviour:
(defun shuffle (vec n)
;; shuffle pairs of elts of VEC, N times.
(loop with max = (length vec)
repeat n
do (rotatef (aref vec (random max))
(aref vec (random max)))
finally (return vec)))
And we can test this:
> (let ((pop (make-array 10))
(tsp (vector 0 1 2 3 4 5 6 7 8 9 )))
(fill-array-with-something pop (length pop) tsp)
pop)
#(#(2 8 7 1 3 9 5 4 0 6) #(2 8 7 1 3 9 5 4 0 6) #(2 8 7 1 3 9 5 4 0 6)
#(2 8 7 1 3 9 5 4 0 6) #(2 8 7 1 3 9 5 4 0 6) #(2 8 7 1 3 9 5 4 0 6)
#(2 8 7 1 3 9 5 4 0 6) #(2 8 7 1 3 9 5 4 0 6) #(2 8 7 1 3 9 5 4 0 6)
#(2 8 7 1 3 9 5 4 0 6))
As you can see all the elements are mysteriously the same thing, which is because my shuffle simply returned its first argument, having modified it in place.
You can check this by either explicitly checking the result of shuffle, or by, for instance, using *print-circle* to see the sharing. The latter approach is pretty neat:
> (let ((*print-circle* t)
(pop (make-array 10))
(tsp (vector 0 1 2 3 4 5 6 7 8 9 )))
(fill-array-with-something pop (length pop) tsp)
(print pop)
(values))
#(#1=#(4 6 7 0 1 2 5 9 3 8) #1# #1# #1# #1# #1# #1# #1# #1# #1#)
And now it's immediately apparent what the problem is.
The solution is to make sure either that shuffle returns a fresh object, or to copy its result. With my shuffle this can be done like this:
(defun fill-array-with-something (population n-population tsp)
(do ((i 0 (+ i 1)))
((= i n-population))
(setf (aref population i) (shuffle (copy-seq TSP) 100))))
Note that a previous version of this answer had (copy-seq (shuffle TSP 100)): with my version of shuffle this is a serious mistake, as it means that the elements in population are related to each other but get increasingly shuffled as you go along. With (shuffle (copy-seq TSP) 100) each element gets the same amount of shuffling, independently.
And now
> (let ((*print-circle* t)
(pop (make-array 10))
(tsp (vector 0 1 2 3 4 5 6 7 8 9 )))
(fill-array-with-something pop (length pop) tsp)
(print pop)
(values))
#(#(8 3 4 1 6 9 2 5 0 7) #(8 6 5 1 3 0 4 2 9 7) #(5 0 4 7 1 6 9 3 2 8)
#(3 0 7 6 2 9 4 5 1 8) #(8 2 5 1 7 3 9 0 4 6) #(0 5 6 3 8 7 2 1 4 9)
#(4 1 3 7 8 0 5 2 9 6) #(6 9 1 5 0 7 4 2 3 8) #(2 7 5 8 0 9 6 3 4 1)
#(5 4 8 9 6 7 2 0 1 3))
I suspect that the problem is in OP function SHUFFLE which has not yet been shared; my suspicion is that SHUFFLE is shuffling the *TSP* array itself in place instead of creating a shuffled copy of that array. The POPULATION values are then all referencing the same shuffled *TSP* array.
To solve this problem, SHUFFLE should return a shuffled array instead of shuffling the array in place. Here is a function that performs a Fisher-Yates shuffle on a vector:
(defun shuffle-vector (vect)
"Takes a vector argument VECT and returns a shuffled vector."
(let ((result (make-array (length vect) :fill-pointer 0)))
(labels ((shuffle (v)
(if (zerop (length v))
result
(let* ((i (random (length v)))
(x (elt v i)))
(vector-push x result)
(shuffle (concatenate 'vector
(subseq v 0 i)
(subseq v (1+ i))))))))
(shuffle vect))))
Testing in the REPL:
CL-USER> (defvar *TSP* #("Village" "Town" "City" "Metropolis" "Megalopolis"))
*TSP*
CL-USER> (defvar *n-population* 5)
*N-POPULATION*
CL-USER> (defvar *population* (make-array *n-population*))
*POPULATION*
CL-USER> (dotimes (i *n-population*)
(setf (aref *population* i) (shuffle-vector *TSP*)))
NIL
CL-USER> *population*
#(#("Megalopolis" "City" "Metropolis" "Town" "Village")
#("Megalopolis" "Metropolis" "Town" "City" "Village")
#("City" "Megalopolis" "Town" "Village" "Metropolis")
#("City" "Megalopolis" "Village" "Metropolis" "Town")
#("Megalopolis" "Town" "Metropolis" "City" "Village"))
I have a .txt file like this:
0
1
2
3
4
5
6
7
8
0
1
2
3
4
5
6
7
8
And i want LISP to read the text file and generate two lists, the first one with the first nine values of the text file and the second one with the next nine values. Like these: List1 = (0 1 2 3 4 5 6 7 8), List2 = (0 1 2 3 4 5 6 7 8).
I have these code:
(DEFUN text()
(SETQ l NIL)
(LOOP
(UNLESS (NULL l) (SETQ LST1 (LIST (READ l)(READ l)(READ l)(READ l)(READ l)(READ l)(READ l)(READ l)(READ l))
LST2 (LIST (READ l)(READ l)(READ l)(READ l)(READ l)(READ l)(READ l)(READ l)(READ l))
eb (LST1) em (LST2))
(CLOSE l) (RETURN (ASTAR LST1 LST2)))
(SETQ l (OPEN filename :if-does-not-exist nil))))
Don't use SETQ with undeclared global variables, use local bindings with let. Global variables make your code fragile w.r.t. side-effects and clutter the global namespace.
Don't copy paste or painfully rewrite the same thing over and over, use a loop
Don't call open/close yourself unless necessary, prefer with-open-file.
You don't need to write in uppercase
Two nested loops
(defun read-18-lines-of-integers-in-two-lists (file)
(with-open-file (in file)
(loop repeat 2
collect (loop repeat 9
collect (parse-integer (read-line in))))))
Example
(read-18-lines-of-integers-in-two-lists #P"/tmp/test")
=> ((0 1 2 3 4 5 6 7 8) (0 1 2 3 4 5 6 7 8))
I am trying to make a (somehow) delicate sorting function in Lisp. I know there is lambda operator that should make my work a lot easier, but I couldn't find anything helpful so I hope you can help me.
As an input I have a nested list like this one:
((o1 10 15 20) (o2 5 14 20) (o3 7 8 8))
The output should be a nested list like this one:
((o1 1 1 1) (o2 3 2 1) (o3 2 3 3))
To be more specific, the first element from o1 is compared to the first element from o2 and o3 and the return should be it's position (in the example above, 10 is greater than 5 and 7 so it will be on the first position in the resulting list) and so on.
The highest number will get the first position.
(it's like a score function. Some students make an application and their features numbers are compared. The one with highest number of features will get the first place, but when comparing the number of different technologies used, he may get the 2nd or 3rd place).
Thanks and I hope you can help me
A little exploration is in order.
[3]> (setq a '((o1 10 15 20) (o2 5 14 20) (o3 7 8 8)))
((O1 10 15 20) (O2 5 14 20) (O3 7 8 8))
[4]> (setq b (apply #'mapcar #'list a))
((O1 O2 O3) (10 5 7) (15 14 8) (20 20 8))
[5]> (setq c (mapcar #'(lambda(x)(sort x #'>)) (cdr b)))
((10 7 5) (15 14 8) (20 20 8))
[6]> (mapcar #'(lambda(g)(cons (car g) (mapcar #'1+
(mapcar #'position (cdr g) c)))) a)
((O1 1 1 1) (O2 3 2 1) (O3 2 3 3))
Now stir, form, and bake at 190 C until ready.
Is it possible to tell dolist to start at (or even better after) a certain element in the given list? As I may not want to evaluate all the elements before.
If there is no way to do so, is there any other macro which might do the job?
Considering this example:
(defvar *liste* #(1 2 3 4 5 6))
(dolist (x *liste* :start-after: '4)
(FORMAT t "~a~%" x))
resulting in:
5
6
Which Lisp dialect are we talking about?
Assuming Common Lisp.
#(1 2 3 4 5 6) is not a list. It is a vector.
CL-USER > (let ((v #(1 2 3 4 5 6)))
(loop for i from 4 below (length v)
do (print (aref v i))))
5
6
NIL
With a list:
CL-USER 1 > (mapc #'print (nthcdr 4 '(1 2 3 4 5 6)))
5
6
(5 6)
What's wrong with NTHCDR?
http://www.lispworks.com/documentation/HyperSpec/Body/f_nthcdr.htm#nthcdr
If I had a N lists each of length M, how could I write a nice clean function to return a single list of length M, where each element is the sum of the corresponding elements in the N lists?
(starting to learn lisp - go easy!)
This is a job for the map and apply functions. Here is a way to do it, with an EDIT suggested by Nathan Sanders:
(define (add-lists . more)
(apply map + more))
For a more matlab like syntax:
(define (piecewise func)
(lambda more
(apply map func more)))
(define pw piecewise)
((pw +) '(1 2 3 4 5) '(6 7 8 9 0))
((pw -) '(1 2 3 4 5) '(6 7 8 9 0))
((pw *) '(1 2 3 4 5) '(6 7 8 9 0))
((pw /) '(1 2 3 4 5) '(6 7 8 9 0.1))
outputs:
(7 9 11 13 5)
(-5 -5 -5 -5 5)
(6 14 24 36 0)
(1/6 2/7 3/8 4/9 50.0)
Just this works in MIT scheme.
(map + '(1 2 3) '(4 5 6) '(7 8 9))
;Value 28: (12 15 18)