pop-assoc

Notices and updates
Locked
Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

pop-assoc

Post 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?
WBR, Dmi

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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.

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

nice :-)
WBR, Dmi

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

Define a new function that uses pop-assoc. The list passed to the function is a copy.
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post 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...?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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.

Locked