Page 1 of 1

destructive setq ?

Posted: Mon May 21, 2012 10:13 pm
by newdep
Hi Lutz,

The problem below looks familiar to me but i cant find/recall the explenation in the manual,
perhpas you can clear me up.. I would intepret it as a bug , as its not explained as a feature in the manual.

The question is , why doesn't the use of -1 or 'last apply to setq/setf for strings?
As the impression is made inside the manual that setq/setf can be used for string manipulation.

Problem is, with common sense, im unable to append 2 strings without deleting the last char. of the first.

Code: Select all

> (setq A "Hello/")
"Hello/"
> (setq (A -1) "world")
"world"
> A
"Helloworld"
> (setq (A -1) "!")
"!"
> A
"Helloworl!"
> (setq (last A) "!")
"!"
> A
"Helloworl!"
>

Re: destructive setq ?

Posted: Tue May 22, 2012 4:38 am
by Lutz
It behaves exactly like it should, analogous to lists:

Code: Select all

> (set 'A '(w o r l d))
(w o r l d)
> (setf (A -1) '!)
!
> A
(w o r l !)
>
the -1 refers to the last element, which gets replaced. To append to a string or list destructively use either 'push':

Code: Select all

> (set 'A "world")
"world"
> (push "!" A -1)
"world!"
or 'extend':

Code: Select all

> (set 'A "world")
"world"
> (extend A "!")
"world!"
> A
"world!"
ps: setf and setq work/are the same in newLISP, but I prefer use setf when used destructive