Set or add a pair to a list by reference
Posted: Fri Feb 05, 2010 2:03 am
Here's the sample code:
Now, here are the results:
And it works just like I want it to, but three questions evolve from this exercise:
1)How can I pass the 'lst argument by reference so that the 'set-pair function becomes destructive?
Example:
2)I come from a background (most recently) in python and rebol, both which pass variables
by reference.
3)I don't believe that Lutz does anything for no good reason:
Why do most newlisp functions pass arguments by value?
Thanks
tim
Code: Select all
(println "<pre>")
(define (set-pair lst key val) ;; set a pair if found or add it if not found
(unless(set-ref (list key '?) lst (list key val) match)
(push (list key val) attrs -1)))
(set 'attrs '(("ID" "form0") ("method" "POST") ("name" "form0") ("action" "[[action]]") ;; sample target list
("onsubmit" "return checkLoginForm(this);")))
(println "Original List:")
(println attrs)
(set 'attr "action") ;; existing key
(set 'value "mycgiscript.lsp") ;; new value
(set 'attrs(set-pair attrs attr value)) ;; call the function
(println "\nList Pair Modified:")
(println attrs) ;; test the result
(set 'attr "newaction") ;; non-existing (new) key
(set 'value "make weave not lore") ;; value for key
(set 'attrs(set-pair attrs attr value)) ;; call it
(println "\nList Pair Added:") ;; display results
(println attrs)
Code: Select all
Original List:
(("ID" "form0") ("method" "POST") ("name" "form0") ("action" "[[action]]") ("onsubmit"
"return checkLoginForm(this);"))
List Pair Modified:
(("ID" "form0") ("method" "POST") ("name" "form0") ("action" "mycgiscript.lsp") (
"onsubmit" "return checkLoginForm(this);"))
List Pair Added:
(("ID" "form0") ("method" "POST") ("name" "form0") ("action" "mycgiscript.lsp") (
"onsubmit" "return checkLoginForm(this);")
("newaction" "make weave not lore"))
1)How can I pass the 'lst argument by reference so that the 'set-pair function becomes destructive?
Example:
Code: Select all
(set-pair attrs attr value) ;; => 'attrs is changed with calling 'set
by reference.
3)I don't believe that Lutz does anything for no good reason:
Why do most newlisp functions pass arguments by value?
Thanks
tim