For the Compleat Fan
pjot
Posts: 733 Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:
Post
by pjot » Sun Apr 06, 2008 6:44 am
Hi,
In some languages (C, AWK, Scriptbasic) it is possible to add a value to the same variable like this:
This actually means 'value = value + 5'.
In newLisp this has to be written as:
Can we have maybe a new operator like '+=', '-=' and so on, so these type of calculations can be written as follows:
Code: Select all
(set 'value (+= 5))
- or even better:
(+= 'value 5)
What do you think?
Peter
Lutz
Posts: 5289 Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:
Post
by Lutz » Sun Apr 06, 2008 6:49 am
for + and - we have already inc and dec:
Code: Select all
> (set 'x 0)
0
> (inc 'x)
1
> (inc 'x)
2
> (inc 'x 2)
4
> (inc 'x 2)
6
> (dec 'x 3)
3
> (dec 'x 3)
0
>
pjot
Posts: 733 Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:
Post
by pjot » Sun Apr 06, 2008 6:52 am
OK, and of course now the inevitable question: what about the other operators? ;-)
pjot
Posts: 733 Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:
Post
by pjot » Sun Apr 06, 2008 7:01 am
...and also, how about strings?
Now we need to append like this:
Code: Select all
(set 'str "0123")
(set 'str (append str "4567"))
How about something like:
Code: Select all
(set 'str "0123")
(app 'str "4567")
So the 'app' behaves like an 'inc'?
newdep
Posts: 2038 Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands
Post
by newdep » Sun Apr 06, 2008 7:10 am
you want smaler code?
you dont like => (push srt <text> -1) ?
-- (define? (Cornflakes))
cormullion
Posts: 2038 Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:
Post
by cormullion » Sun Apr 06, 2008 8:14 am
pjot wrote:
This actually means 'value = value + 5'.
Personally I'd rather see the latter than the former, and it looks better in Lisp form too - (set 'value (+ value 5)). It's more readable - although I'll agree it's a bit more typing.
Perhaps this is a good reason for writing macros to develop your preferred set of shorthand operators...
Code: Select all
(define-macro (scale s x)
(set s (mul (eval s) x)))
(set 'a 12)
(scale a 2)
24
Lutz
Posts: 5289 Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:
Post
by Lutz » Sun Apr 06, 2008 8:37 am
Then there is also this for destructive string append:
Code: Select all
> (set 'str "123")
"123"
> (write-buffer str "456")
3
> str
"123456"
>
When you give 'write-buffer' a string instead of a file handle, it appends to it.
pjot
Posts: 733 Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:
Post
by pjot » Sun Apr 06, 2008 8:44 am
Thanks for the answers.
Personally I'd rather see the latter than the former, and it looks better in Lisp form too - (set 'value (+ value 5)). It's more readable - although I'll agree it's a bit more typing.
It's not a matter of typing, but rather shortness and elegance of code. Contrary to Cormullion, I find the '(set 'value (+ value 5))' not very elegant, but it's a matter of taste, I guess :-)
Peter