newLISP Fan Club

Forum => newLISP in the real world => Topic started by: Tim Johnson on January 05, 2010, 08:32:07 AM

Title: Binding a value to multiple values
Post by: Tim Johnson on January 05, 2010, 08:32:07 AM
Hi:

In python I can do this:

>>> inner = []
>>> outer1 = []
>>> outer2 = []
>>> outer1.append(inner)
>>> outer2.append(inner)
>>> inner.append(1)
>>> outer1
[[1]]
>>> outer2
[[1]]

How may I do the same thing in newlisp?

> (set 'outer1 '() 'outer2 '())
()
> (push inner outer1)
(())
> (push inner outer2)
(())
> (push 1 inner)
(1)
> outer1
(())  ;; was hoping for '((1))
> outer2
(()) ;; was hoping for '((1))

TIA

tim
Title: Re: Binding a value to multiple values
Post by: itistoday on January 05, 2010, 09:22:47 AM
newLISP doesn't do reference passing in most situations (including that one), therefore that won't work because outer1 and outer2 contain copies of inner, not a reference to inner.



If you need to be able to do reference passing in newLISP, try Objective newLISP (link in sig):


$ newlisp ObjNL.lsp
newLISP v.10.1.9-dev on OSX IPv4 UTF-8, execute 'newlisp -h' for more info.

> (new-class 'Container)
Container
> (setf Container:lst '())
()
> (setf inner (instantiate Container))
Container#1
> (setf outer1 (instantiate Container))
Container#2
> (setf outer2 (instantiate Container))
Container#3
> (push inner outer1:lst)
(Container#1)
> (push inner outer2:lst)
(Container#1)
> (push 1 inner:lst)
(1)
> outer1:lst
(Container#1)
> (. (outer1:lst 0) lst)
(1)
Title: Re: Binding a value to multiple values
Post by: Tim Johnson on January 05, 2010, 10:48:19 AM
Hi  itistoday:

I was aware of your object library, and also aware of newlisp's reference passing (or limitations thereof).

Before proceding along your path, or any path that instantiates objects, I'd like to ask your opinion of

the following:

The example that I cite of python can be expanded to more efficient coding without a lot of bulking up

of code and little or no degrading of performance.

My goal (recreating what I have done in python and rebol)

would be to create two outer data structures to offer alternate mappings to the same inner data structures.



One of the data structures would preserve the original order in which the mapping is created, therefore making it

easier codewise to recreate the original source text, but with modification created in the second data structure.

The second data structure would enable easier access to the inner structures.



Perhaps it would more efficient performance to simply map with one data structure even 'tho that might necessitate

more coding. That might be the more "newlispish" way.



Have you done any comparative benchmarking?

thanks

tim
Title: Re: Binding a value to multiple values
Post by: cormullion on January 05, 2010, 11:28:25 AM
My guess is that Objective newLISP would be the best solution - (tho I wish I understood it better... :(). The closest you can get to what was typed above in plain newLISP might be something like this:


> (define inner:inner)
nil
> (define (outer1:outer1) (inner))
(lambda () (inner))
> (define (outer2:outer2) (inner))
(lambda () (inner))
> (inner "1" 1)
> (outer1)
(("1" 1))
> (outer2)
(("1" 1))
> (inner "2" nil)
nil
> (outer1)
(("1" 1))
> (outer2)
(("1" 1))
>


so both outer1 and outer2 reflect  changes made to inner. But you'd be very limited as to what you could do, I think, and I'm not sure it's what you want anyway!



PS: was replying to your first post, not the second, Tim!
Title: Re: Binding a value to multiple values
Post by: Tim Johnson on January 05, 2010, 11:39:09 AM
Thank you cormullion, we have in you an affirmative second opinion :) so I will give it a try

and implement Objective newLISP. Thus you and I may both learn more. Furthermore, I will

not need to build these data structures every time the application runs. The source will be a

text/html file, data structures will be cached and the code to build the data structures need

to be executed only when the cache file goes away or if the text file has a later MTime.



My time is limited, so I will give this my attention when other obligations don't interfere. I hope that

both you and itistoday will stay subscribed to this thread.

stay tuned....

cheers

tim
Title: Re: Binding a value to multiple values
Post by: itistoday on January 05, 2010, 12:45:15 PM
Quote from: "Tim Johnson"Have you done any comparative benchmarking?


I'm not really sure what benchmarking you're referring to. Comparing what to what?
Title: Re: Binding a value to multiple values
Post by: Tim Johnson on January 05, 2010, 12:50:24 PM
Quote from: "itistoday"
Quote from: "Tim Johnson"Have you done any comparative benchmarking?


I'm not really sure what benchmarking you're referring to. Comparing what to what?

Never mind. (see my previous post)
Title: Re: Binding a value to multiple values
Post by: nallen05 on January 11, 2010, 11:37:44 PM
an easier hack in this case might be to just to use the symbol itself as a container and evaluate it upon access


Quote
 (set 'outer1 '() 'outer2 '())

()

> (set 'inner '())

()

> (push 'inner outer1) ; quoted inner

(inner)

> (push 'inner outer2) ; quoted inner

(inner)

> (push 1 inner)

(1)

> (map eval outer1) ; evaluating elements of outer1

((1))

> (map eval outer2) ; evaluating elements of outer2

((1))
Title: Re: Binding a value to multiple values
Post by: Tim Johnson on January 12, 2010, 10:39:13 AM
I like your method. thanks