Ugly behaviuor of doargs

Notices and updates
Locked
ale870
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

Ugly behaviuor of doargs

Post by ale870 »

Hello,

I'm using newlisp version V.9.4.3 on Linux, and I found a bad behaviour of doargs function.
Look at here:

Code: Select all

(define (form-create , locVariable)
	(doargs (i) (println "I: " i) )
)
	
(form-create "a1" "a2" 3 4 "z5")
In this case I obtain the following result:

Code: Select all

I: 3
I: 4
I: z5
As you can see, even if I defined a local variable "locVariable", the function doargs bypassed the first two items "a1" and "a2".
Instead if I create a function without local vars it works:

Code: Select all

(define (form-create)
	(doargs (i) (println "I: " i) )
)
	
(form-create "a1" "a2" 3 4 "z5")

I: a1
I: a2
I: 3
I: 4
I: z5
Is it a bug or I'm wrong?
--

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

I suspect that doargs is similar to args in that it looks only at unbound variables:

"Only the arguments of the current function or macro that remain after local variable binding has occurred are available."

If so, the description for (doargs) could do with a tweak?

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

Post by Kazimir Majorinc »

I think it is just as it should be:

Code: Select all

(define (form-create , locVariable)
   (println ,)
   (println locVariable)
   (doargs (i) (println "I: " i) )
)

(form-create "a1" "a2" 3 4 "z5") 
gives:

a1
a2
I: 3
I: 4
I: z5

ale870
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

Post by ale870 »

But this is not coherent with the concept that variables behind a comma are local variables.
Only alternative I found was using (let) function to define local ones.
--

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

Comma-variables are convention, not spec.
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

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

Post by Kazimir Majorinc »

ale870 wrote:But this is not coherent with the concept that variables behind a comma are local variables.
Only alternative I found was using (let) function to define local ones.
As Jeff said, comma is trick. An alternative to "let" is "local."

ale870
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

Post by ale870 »

Ok, now I see.
I didn't think that comma was a "convention" and not an "official" behaviuor.
Now I will always use "let".

Thank you!.
--

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

Post by DrDave »

You have to look at it like this. In your first definition, you have two formal arguments, comma and locVariable. So, when you called your function thus (form-create "a1" "a2" 3 4 "z5"), it binds "a1" to comma, "a2" to locVariable, and then leaves the specific arguments 3, 4, "z5" for the body expression to deal with.

You can verify this by printing comma and locVariable in the body expression.

As was already stated, using comma is just a visual trick. Because comma is just another symbol, It realy *does* get bound to either a value or nil. You just don't (normally) make any use of it.
...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

Locked