newLISP Fan Club

Forum => newLISP newS => Topic started by: Dmi on March 21, 2008, 08:10:27 AM

Title: pop-assoc
Post by: Dmi on March 21, 2008, 08:10:27 AM
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?
Title:
Post by: Lutz on March 21, 2008, 08:44:40 AM
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.
Title:
Post by: Dmi on March 21, 2008, 09:29:23 AM
nice :-)
Title:
Post by: Jeff on March 21, 2008, 12:28:08 PM
Define a new function that uses pop-assoc.  The list passed to the function is a copy.
Title:
Post by: Lutz on March 21, 2008, 12:37:01 PM
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.
Title:
Post by: cormullion on March 22, 2008, 02:48:21 AM
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...?
Title:
Post by: Lutz on March 22, 2008, 05:10:34 AM
QuoteIs 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.