Page 1 of 1
Ugly behaviuor of doargs
Posted: Thu Sep 04, 2008 3:10 pm
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:
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?
Posted: Thu Sep 04, 2008 4:08 pm
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?
Posted: Thu Sep 04, 2008 4:26 pm
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
Posted: Thu Sep 04, 2008 5:02 pm
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.
Posted: Thu Sep 04, 2008 8:23 pm
by Jeff
Comma-variables are convention, not spec.
Posted: Thu Sep 04, 2008 10:35 pm
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."
Posted: Fri Sep 05, 2008 5:41 am
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!.
Posted: Fri Sep 05, 2008 9:33 am
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.