Two small proposals for v10!

Notices and updates
Locked
Kazimir Majorinc
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Two small proposals for v10!

Post by Kazimir Majorinc »

(1) Aliases +., -., *., /., %. beside/later instead of add sub mul etc. It has some advantages.

(2) Functions println=, print= such that (print= x (+ 2 4) y) produces following output:

x=nil; (+ 2 4)=6; y=nil;

It is not big deal but is useful in "scripting style", for some small programs of the kind we publish on this forum. I also have more "Lispy" version (-> x nil) (-> (+ 2 4) 6) ... but I rarely use it.

These two are in my library, and they are trivial to implement, but I thought it might be of interest out of the box.

Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

Post by Fanda »

Hello newlispers!
Since there is gonna be a new newLISP version, which makes some breaking changes, it is a good time to introduce new things and do some cleanup :-)

I am proposing to change behavior of 'dup'. Currently:

Code: Select all

> (dup 'a 5)
(a a a a a)
> (dup "a" 5)
"aaaaa"
In my opinion, it would be nice if 'dup' always returned a list. If string is needed, use 'join':

Code: Select all

> (dup 'a 5)
(a a a a a)
> (dup "a" 5)
("a" "a" "a" "a" "a")
> (join (dup "a" 5))
"aaaaa"

Another, IMO confusing behavior, is overloaded apply (currently doesn't work?):

Code: Select all

> (apply list '(a b c d) 2)
(((a b) c) d)
More readable would be to introduce new function 'reduce' - works as (apply op list 2):

Code: Select all

> (reduce list '(a b c d))
(((a b) c) d)
> (reduce list 'init '(a b c d))
((((init a) b) c) d)
Greetings, Frantisek

PS: Yes, I am still using newLISP :-) I use it at work to parse text files and at home to generate web pages. It is also a nice calculator, if you need to do some numerical calculations.

DrDave
Posts: 126
Joined: Wed May 21, 2008 2:47 pm

Post by DrDave »

Fanda,

What is it that dont you like about using

Code: Select all

> (apply list '(a b c d) 2)
It did just as it says: It creates a two-membered list on each 'apply', resulting in nestings of two-membered lists.

Maybe you can see it better using a longer list grouped in threes.
Try

Code: Select all

(apply list '(a b c d e f g h) 3)
=>((((a b c) d e) f g) h)
You can see when it reached 'h' that there was not enough members left to append two of them, so the final grouping has just two members instead of three.
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.
"Getting Started with Erlang" version 5.6.2

Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

Post by Fanda »

DrDave wrote:What is it that dont you like about using

Code: Select all

> (apply list '(a b c d) 2)
It combines two different concepts - applying and reducing. When I read the code, I need to count how many parameters 'apply' function has to decipher what action is being taken. That can be confusing. Separate function for reducing would be IMHO better.

Fanda

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

Post by Lutz »

'dup' on strings is mostly used for concatenation to create string fields, e.g. to create blank fields, lines etc. or with binary zeros to reserve buffers passed to imported functions.

Use the extra boolean flag to force a list:

Code: Select all

> (dup "a" 5 true)
("a" "a" "a" "a" "a")
> 
The reduce parameter in 'apply' will be fixed and posted later as 9.9.8.

Locked