Hi, Lutz!
I found a segfault:
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?
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:
(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.
nice :-)
Define a new function that uses pop-assoc. The list passed to the function is a copy.
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:
(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.
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...?
Quote
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.