Page 1 of 1
pop-assoc
Posted: Fri Mar 21, 2008 3:10 pm
by Dmi
Hi, Lutz!
I found a segfault:
Code: Select all
dmi@stone:~$ newlisp
newLISP v.9.3.1 on Linux, execute 'newlisp -h' for more info.
> (set 'l '((1 2) (3 4) (5 (6 7))))
((1 2) (3 4) (5 (6 7)))
> (pop-assoc ('l 3))
Segmentation fault
I know that l must not be quoted, but this typo causes failure.
And a question: what for the extra parenthesis are in the syntax?
Oh! and another question:
Is there a non-destructive version for pop-assoc?
Posted: Fri Mar 21, 2008 3:44 pm
by Lutz
Thanks for catching this bug with the quote. Regarding the parentheses: it allows me to pass context names for default functors holding data. This is a syntax also used for 'nth' and 'ref' family of functions and in implicit indexing. It allows reference passing:
Code: Select all
(set 'foo:foo '((a 1) (b 2) (c 3)))
(define (remove ctx key)
(pop-assoc (ctx key)))
> (remove foo 'a)
(a 1)
> foo:foo
((b 2) (c 3))
>
you see that the original list was passed by reference and changed.
Posted: Fri Mar 21, 2008 4:29 pm
by Dmi
nice :-)
Posted: Fri Mar 21, 2008 7:28 pm
by Jeff
Define a new function that uses pop-assoc. The list passed to the function is a copy.
Posted: Fri Mar 21, 2008 7:37 pm
by Lutz
It depends how you pass the list. If it comes from a normal variable then it is passed as a copy.
But if the list is contained in the default functor of a context, as shown in my last post, then the list is passed by reference:
Code: Select all
(set 'foo:foo '((a 1) (b 2) (c 3)))
(define (remove ctx key)
(pop-assoc (ctx key)))
> (remove foo 'a)
(a 1)
> foo:foo
((b 2) (c 3))
>
Although foo:foo is passed to a function (as foo)
it is passed by reference and modified.
No copy is made.
Any data can be passed by reference using this method.
Posted: Sat Mar 22, 2008 9:48 am
by cormullion
When reading code, presumably there's no obvious way of 'seeing' that you're passing a reference rather than a copy...? Is there an argument for reviving the old suggestion that context names have initial capital letters...?
Posted: Sat Mar 22, 2008 12:10 pm
by Lutz
Is there an argument for reviving the old suggestion that context names have initial capital letters...?
Yes, absolutely, I was just sloppy when writing the example. The capitalization would be in the caller, because the function itself works well with both types.