push macro storing in-function but not out of function [duplicate] - lisp

This question already has answers here:
Common lisp push from function
(4 answers)
Closed 3 years ago.
I have a function:
(defun multi-push (L P)
(print (if L "T" "F"))
(print P)
(when L
(multi-push (cdr L) (push (car L) P)))
P)
which I have made in an to attempt to push a list onto another list (I am aware the input list L is reversed. This is by design). The print statements make sense, but when I look at the variable P, it is not mutated as I expect.
Sample REPL output:
CL-USER> bob
(3 3 3)
CL-USER> (multi-push (list 1 2) bob)
"T"
(3 3 3)
"T"
(1 3 3 3)
"F"
(2 1 3 3 3)
(1 3 3 3)
CL-USER> bob
(3 3 3)
What have I done wrong? I thought PUSH (according to [http://clhs.lisp.se/Body/m_push.htm]) mutates its second argument in place. I have also tried variations where I POP L and PUSH it onto P before calling multi-push on L and P again.
one thing of note is that the line (1 3 3 3) is the output of the function of multi-push. This also confuses me.

What push mutates destructively is a binding, not a list. More correctly what push modifies is a 'place' which is
a form which is suitable for use as a generalized reference
where a 'generalized reference' is
a reference to a location storing an object as if to a variable.
These two quotes are from the CLHS glossary: the section which talks about this is 5.1.
In particular:
> (let* ((l1 '(1 2 3))
(l2 l1))
(push 0 l1)
(values l1 l2))
(0 1 2 3)
(1 2 3)
And also note that this is legal CL since it doesn't destructively alter the quoted list structure. push must be a macro since a function can't do what it does: you can't write a function f such that:
(let* ((a (list 1 2 3))
(b a))
(f a b)
(not (eq a b)))
would be true.
You can think of (push x y) as expanding to something like (setf y (cons x y)), except that it will deal with multiple-evaluation properly.

Related

I'm struggling to understand why some Lisp data changes when I alter some value in a list and others doesn't [duplicate]

This question already has an answer here:
Unexpected persistence of data [duplicate]
(1 answer)
Closed 2 years ago.
I'm struggling two understand why when i change the values of l1 list the double-args macro changes the value in the result list and the double-list doesn't. Let me show the steps to be more clear.
I have defined the list l1 with values '(1 2 3 4),
(setq l1 '(1 2 3 4))
then i load this code below
(defmacro double-args (&rest args)
`(let ((ret nil))
( dolist (x ,#args )
(setq ret (append ret (list x x))))
ret) )
(defun macroteste (&rest x) (double-args x))
;; first simple macro example:
(defmacro double-list (a-list)
(let ((ret (gensym)))
`(let ((,ret nil))
(dolist (x ,a-list)
(setq ,ret (append ,ret (list x x))))
,ret)))
;; use the macro:
(defun doublelistmacro (x)
(double-list x))
after this, i had executed the macro, macroteste with the list l1 and stored in l2
(setq l2 (macroteste l1))
then i executed the doublelistmacro with arg l1 and stored in l3
(setq l3 (doublelistmacro l1))
so i got from l2,
((1 2 3 4) (1 2 3 4))
and from l3,
(1 1 2 2 3 3 4 4)
then i had change the second value of l1,
(setf (nth 1 l1) 9)
and i got theses results:
l1
(1 9 3 4)
l2
((1 9 3 4) (1 9 3 4))
l3
(1 1 2 2 3 3 4 4)
why when i changed l1 the l2 has changed too but l3 not?
First, a remark: in code you should not modify literal constants like '(1 2 3 4). The effects are undefined.
In your case the difference boils down to this
CL-USER 2 > (let ((foo (list 1 2 3 4)))
(let ((l1 (list foo foo))
(l2 (loop for e in foo append (list e e))))
(values l1 l2)))
((1 2 3 4) (1 2 3 4))
(1 1 2 2 3 3 4 4)
The first list l1 is a new list with the original list twice as its element.
The second list l2 is a completely new constructed list.
If you change the original list, it will be visible in the first result -> it has them directly as elements.
It will not be visible in the second list, because the list is completely fresh.
Note also that the macros are complicating the issue. As such they also make very little sense.

Writing the Foo Function In LISP With the following Specification

I am struggling to find the right approach to solve the following function
(FOO #'– '(1 2 3 4 5))
=> ((–1 2 3 4 5) (1 –2 3 4 5) (1 2 –3 4 5) (1 2 3 –4 5) (1 2 3 4 –5))
The first Parameter to the foo function is supposed to be a function "-" that has to be applied to each element returning a list of list as shown above. I am not sure as to what approach I can take to create this function. I thought of recursion but not sure how I will preserve the list in each call and what kind of base criteria would I have. Any help would be appreciated. I cannot use loops as this is functional programming.
It's a pity you cannot use loop because this could be elegantly solved like so:
(defun foo (fctn lst)
(loop
for n from 0 below (length lst) ; outer
collect (loop
for elt in lst ; inner
for i from 0
collect (if (= i n) (funcall fctn elt) elt))))
So we've got an outer loop that increments n from 0 to (length lst) excluded, and an inner loop that will copy verbatim the list except for element n where fctn is applied:
CL-USER> (foo #'- '(1 2 3 4 5))
((-1 2 3 4 5) (1 -2 3 4 5) (1 2 -3 4 5) (1 2 3 -4 5) (1 2 3 4 -5))
Replacing loop by recursion means creating local functions by using labels that replace the inner and the outer loop, for example:
(defun foo (fctn lst)
(let ((len (length lst)))
(labels
((inner (lst n &optional (i 0))
(unless (= i len)
(cons (if (= i n) (funcall fctn (car lst)) (car lst))
(inner (cdr lst) n (1+ i)))))
(outer (&optional (i 0))
(unless (= i len)
(cons (inner lst i) (outer (1+ i))))))
(outer))))
Part of the implementation strategy that you choose here will depend on whether you want to support structure sharing or not. Some of the answers have provided solutions where you get completely new lists, which may be what you want. If you want to actually share some of the common structure, you can do that too, with a solution like this. (Note: I'm using first/rest/list* in preference to car/car/cons, since we're working with lists, not arbitrary trees.)
(defun foo (operation list)
(labels ((foo% (left right result)
(if (endp right)
(nreverse result)
(let* ((x (first right))
(ox (funcall operation x)))
(foo% (list* x left)
(rest right)
(list* (revappend left
(list* ox (rest right)))
result))))))
(foo% '() list '())))
The idea is to walk down list once, keeping track of the left side (in reverse) and the right side as we've gone through them, so we get as left and right:
() (1 2 3 4)
(1) (2 3 4)
(2 1) (3 4)
(3 2 1) (4)
(4 3 2 1) ()
At each step but the last, we take the the first element from the right side, apply the operation, and create a new list use revappend with the left, the result of the operation, and the rest of right. The results from all those operations are accumulated in result (in reverse order). At the end, we simply return result, reversed. We can check that this has the right result, along with observing the structure sharing:
CL-USER> (foo '- '(1 2 3 4 5))
((-1 2 3 4 5) (1 -2 3 4 5) (1 2 -3 4 5) (1 2 3 -4 5) (1 2 3 4 -5))
By setting *print-circle* to true, we can see the structure sharing:
CL-USER> (setf *print-circle* t)
T
CL-USER> (let ((l '(1 2 3 4 5)))
(list l (foo '- l)))
((1 . #1=(2 . #2=(3 . #3=(4 . #4=(5))))) ; input L
((-1 . #1#)
(1 -2 . #2#)
(1 2 -3 . #3#)
(1 2 3 -4 . #4#)
(1 2 3 4 -5)))
Each list in the output shares as much structure with the original input list as possible.
I find it easier, conceptually, to write some of these kind of functions recursively, using labels, but Common Lisp doesn't guarantee tail call optimization, so it's worth writing this iteratively, too. Here's one way that could be done:
(defun phoo (operation list)
(do ((left '())
(right list)
(result '()))
((endp right)
(nreverse result))
(let* ((x (pop right))
(ox (funcall operation x)))
(push (revappend left (list* ox right)) result)
(push x left))))
The base case of a recursion can be determined by asking yourself "When do I want to stop?".
As an example, when I want to compute the sum of an integer and all positive integers below it, I can do this recusively with a base case determined by answering "When do I want to stop?" with "When the value I might add in is zero.":
(defun sumdown (val)
(if (zerop val)
0
(+ (sumdown (1- val)) val)))
With regard to 'preserve the list in each call', rather than trying to preserve anything I would just build up a result as you go along. Using the 'sumdown' example, this can be done in various ways that are all fundamentally the same approach.
The approach is to have an auxiliary function with a result argument that lets you build up a result as you recurse, and a function that is intended for the user to call, which calls the auxiliary function:
(defun sumdown1-aux (val result)
(if (zerop val)
result
(sumdown1-aux (1- val) (+ val result))))
(defun sumdown1 (val)
(sumdown1-aux val 0))
You can combine the auxiliary function and the function intended to be called by the user by using optional arguments:
(defun sumdown2 (val &optional (result 0))
(if (zerop val)
result
(sumdown2 (1- val) (+ val result))))
You can hide the fact that an auxiliary function is being used by locally binding it within the function the user would call:
(defun sumdown3 (val)
(labels ((sumdown3-aux (val result)
(if (zerop val)
result
(sumdown3-aux (1- val) (+ val result)))))
(sumdown3-aux val 0)))
A recursive solution to your problem can be implemented by answering the question "When do I want to stop when I want to operate on every element of a list?" to determine the base case, and building up a result list-of-lists (instead of adding as in the example) as you recurse. Breaking the problem into smaller pieces will help - "Make a copy of the original list with the nth element replaced by the result of calling the function on that element" can be considered a subproblem, so you might want to write a function that does that first, then use that function to write a function that solves the whole problem. It will be easier if you are allowed to use functions like mapcar and substitute or substitute-if, but if you are not, then you can write equivalents yourself out of what you are allowed to use.

DELETE + SETF inside a function

I'm trying to write a function that will destructively remove N elements from a list and return them. The code I came up with (see below) looks fine, except the SETF is not working the way I intended.
(defun pick (n from)
"Deletes (destructively) n random items from FROM list and returns them"
(loop with removed = nil
for i below (min n (length from)) do
(let ((to-delete (alexandria:random-elt from)))
(setf from (delete to-delete from :count 1 :test #'equal)
removed (nconc removed (list to-delete))))
finally (return removed)))
For most cases, this works just fine:
CL-USER> (defparameter foo (loop for i below 10 collect i))
CL-USER> (pick 3 foo)
(1 3 6)
CL-USER> foo
(0 2 4 5 7 8 9)
CL-USER> (pick 3 foo)
(8 7 0)
CL-USER> foo
(0 2 4 5 9)
As you can see, PICK works just fine (on SBCL) unless the element being picked happens to be the first on the list. In that case, it doesn't get deleted. That's because the only reassignment happening is the one that goes on inside DELETE. The SETF doesn't work properly (i.e. if I use REMOVE instead, FOO does not change at all).
Is there any scoping rule going on that I'm not aware of?
A (proper) list consists of cons cells that each hold a reference to the next
cell. So, it is actually a chain of references, and your variable has a
reference to the first cell. To make this clear, I rename the binding outside
of your function to var:
var ---> [a|]--->[b|]--->[c|nil]
When you pass the value of the variable to your function, the parameter gets
bound to the same reference.
var ---> [a|]--->[b|]--->[c|nil]
/
from --'
You can update the references in the chain, for example eliminate b:
var ---> [a|]--->[c|nil]
/
from --'
This has an effect on the list that var sees outside.
If you change the first reference, for example eliminating a, this is just the
one originating from from:
var ---> [a|]--->[b|]--->[c|nil]
/
from --'
This has obviously no effect on what var sees.
You need to actually update the variable binding in question. You can do that
by setting it to a value returned by function. Since you already return a
different value, this would then be an additional return value.
(defun pick (n list)
(;; … separate picked and rest, then
(values picked rest)))
Which you then use like this, for example:
(let ((var (list 1 2 3)))
(multiple-value-bind (picked rest) (pick 2 var)
(setf var rest)
(do-something-with picked var)))
Now to the separation: unless the list is prohibitively long, I'd stick to
non-destructive operations. I also would not use random-elt, because it needs
to traverse O(m) elements each time (m being the size of the list),
resulting in a runtime of O(n·m).
You can get O(m) overall runtime by determining the current chance of picking
the current item while linearly running over the list. You then collect the
item into either the picked or rest list.
(defun pick (n list)
(loop :for e :in list
:and l :downfrom (length list)
:when (or (zerop n)
(>= (random 1.0) (/ n l)))
:collect e :into rest
:else
:collect e :into picked
:and :do (decf n)
:finally (return (values picked rest))))
Delete isn't required to modify any structure, it's just permitted to. In fact, you can't always do a destructive delete. If you wanted to delete 42 from (42), you'd need to return the empty list (), which is the symbol NIL, but there's no way that you can turn the list (42), which is a cons cell (42 . NIL) into a different type of object (the symbol NIL). As such, you'll probably need to return both the updated list, as well as the elements that were removed. You can do that with something like this, which returns multiple values:
(defun pick (n from)
(do ((elements '()))
((or (endp from) (zerop n))
(values elements from))
(let ((element (alexandria:random-elt from)))
(setf from (delete element from)
elements (list* element elements))
(decf n))))
CL-USER> (pick 3 (list 1 2 3 2 3 4 4 5 6))
(2 6 4)
(1 3 3 5)
CL-USER> (pick 3 (list 1 2 3 4 5 6 7))
(2 7 5)
(1 3 4 6)
CL-USER> (pick 2 (list 1 2 3))
(2 3)
(1)
CL-USER> (pick 2 (list 1))
(1)
NIL
On the receiving end, you'll want to use something like multiple-value-bind or multiple-value-setq:
(let ((from (list 1 2 3 4 5 6 7)))
(multiple-value-bind (removed from)
(pick 2 from)
(format t "removed: ~a, from: ~a" removed from)))
; removed: (7 4), from: (1 2 3 5 6)
(let ((from (list 1 2 3 4 5 6 7))
(removed '()))
(multiple-value-setq (removed from) (pick 2 from))
(format t "removed: ~a, from: ~a" removed from))
; removed: (3 5), from: (1 2 4 6 7)
delete does not necessarily modify its sequence argument. As the hyperspec says:
delete, delete-if, and delete-if-not return a sequence of the same type as sequence that has the same elements except that those in the subsequence bounded by start and end and satisfying the test have been deleted. Sequence may be destroyed and used to construct the result; however, the result might or might not be identical to sequence.
For instance, in SBCL:
* (defvar f (loop for i below 10 collect i))
F
* (defvar g (delete 0 f :count 1 :test #'equal))
G
* g
(1 2 3 4 5 6 7 8 9)
* f
(0 1 2 3 4 5 6 7 8 9)
*
Note that in your function setf modifies the local variable from, and since delete in the case of first element does not modify the original list, at the end of the function the variable foo maintains the old values.

Adding to the end of list in LISP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
what is the ‘cons’ to add an item to the end of the list?
After watching many tutorials on lisp and searching high and low on google for answers, I still cannot figure out how to add to the end of a list in LISP.
I want my function to add 'a at the end of the list '(b c d) but I only know how to add it in front. Can someone help me use cons correctly to add 'a at the end of the list? Here is my code. Thanks in advance.
(defun AddRt (a list)
(cond
((null list)
0)
(t
(princ (cons a (cons (car list) (cdr list))))
)))
(AddRt 'a '(b c d))
Either push to last, or use nconc:
> (defparameter a (list 1 2 3))
A
> (push 4 (cdr (last a)))
(4)
> a
(1 2 3 4)
> (nconc a (list 5))
(1 2 3 4 5)
> a
(1 2 3 4 5)
note that these are destructive operators, i.e., they modify the object which is the value of a, not just the binding of a.
This is why, BTW, you should never use nconc on quoted lists, like (nconc '(1 2 3) '(4 5 6)).
PS. Note that adding to the end of a list requires its full
traversal and is thus an O(length(list)) operation. This may be a bad
idea if your lists are long, so people often use the
push/nreverse
idiom, e.g.,
(let (range)
(dotimes (i 10 (nreverse range))
(push i range)))
==> (0 1 2 3 4 5 6 7 8 9)
You may use a recursive function. Also, you should avoid using princ inside.
The following function, endcons, does exactly the same thing as cons, except the value is added at the end.
(defun endcons (a v)
(if (null v) (cons a nil) (cons (car v) (endcons a (cdr v)))))
(endcons 'a '(b c d))
Of course, you could also use append:
(append '(b c d) '(a))
See also this related question: what is the 'cons' to add an item to the end of the list?
One way is to reverse the list. Add the element to beginning of the reversed list. And then finally reverse the whole list.
Scheme code:
(define (add-to-tail l x)
(reverse (cons x (reverse l)))
But if this is an operation you need often, then I'd suggest you find a data structure other than (single linked) lists.

working with lists in scheme

I have this function:
(define (unfold f init)
(if (eq? (f init) '())
(list)
(cons (car (f init)) (unfold f (cdr (f init))))))
I want to use it to define a function that does:
(hypothetical-function '(1 2 3 4 5))
which should return:
'((1 2 3 4 5) (2 3 4 5) (3 4 5) (4 5) (5)
Okay, you want
(define (tails xs) (unfold foo xs))
and for that you need to define the appropriate foo. Now, what should foo do? It should return a pair, the first component of which becomes the car of the resulting list, and the second component of which becomes the seed for the recursive call - unless the unfolding is to stop, when foo should return an empty list. So
(define (foo xs)
(if (stop-condition)
'()
(cons car-of-result next-seed)))
Filling in the remaining details is left as an exercise to the reader.