destructive setq ?

Pondering the philosophy behind the language
Post Reply
newdep
Posts: 2019
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

destructive setq ?

Post 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!"
>
-- (define? (Cornflakes))

Lutz
Posts: 5279
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: destructive setq ?

Post 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

Post Reply