Ugly behaviuor of doargs

Started by ale870, September 04, 2008, 08:10:44 AM

Previous topic - Next topic

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:


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

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


In this case I obtain the following result:


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:


(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

#1
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

#2
I think it is just as it should be:


(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
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

ale870

#3
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

#4
Comma-variables are convention, not spec.
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

Kazimir Majorinc

#5
Quote from: "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.


As Jeff said, comma is trick. An alternative to "let" is "local."
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

ale870

#6
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

#7
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