Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - itistoday

#421
newLISP newS /
December 07, 2007, 07:59:09 AM
Is it possible for there to be a hypothetical built-in function called 'unsafe-ref' that returned an internal reference (pointer) to something?  Then you'd be able to do things like this without having to walk down the tree/list twice (once to find the indices, and once again for nth-set to follow them back down to update the node).  It would also add greater flexibility to what newLISP could do I think... at the cost of potential crashes. :-p
#422
newLISP newS /
December 07, 2007, 07:49:27 AM
Thank you both!  Using 'ref' and 'nth-set', that's what I was looking for!  Coming from far-away lands that always had references, I'm not used to this method of mutably modifying things.



Thanks again!
#423
newLISP newS /
December 06, 2007, 06:49:49 PM
OK, I fixed the problem, the code had a bug in the indexing, it should be:


(define (TREE-label tree) (tree 1))
(define (TREE-left tree) (tree 2))
(define (TREE-right tree) (tree 3))
(define (set-TREE-label tree value)
  (nth-set (tree 1) value))


And also, I was right, this method does not allow you to easily modify the data inside the tree... Here's an example of what I want to do:


(set 'my-tree (make-depth-3-binary-tree))
(set-TREE-label (find-subtree my-tree 3) 2000)

(println "3: " (find-subtree my-tree 3))
(println "2000: " (find-subtree my-tree 2000))


I want that to print:


3: nil
2000: (labeled-binary-tree 2000 () ())


But instead it does the opposite because nothing was changed.  So my question is ... how do you do this in newLISP without jumping through crazy hoops or copying large amounts of data?  Or similarly, as I asked in the first post, how do you add children to an already existing tree without copying the entire thing or generating a million contexts? (and with those you've got to deal with the overhead of newLISP's red-black tree for symbol lookup).
#424
newLISP newS /
December 06, 2007, 06:26:36 PM
Thanks for your replies and the binary tree example, but I'm still worried that it won't work the way I want.  For example, what will this do:


(set-TREE-label (find-subtree tree 5) 2000)

Will that update what's in the tree itself, or will that only update a copy of what was in the tree?





Edit: I'm trying to test this on my own but I'm getting nil ... e.g. this prints out nil:


(set 'my-tree (make-depth-3-binary-tree))
(println (find-subtree my-tree 3))


(I fixed the problem, see below)
#425
Anything else we might add? /
December 06, 2007, 03:21:29 PM
Quote from: "newdep"My second contribution is (although still in progress)

(load "http://www.nodep.nl/downloads/newlisp/nlist.lsp">//http://www.nodep.nl/downloads/newlisp/nlist.lsp")


Wow, I just spent some time using that to go through the newLISP files on your server, they're amazing!  My favorite has to be worm.lsp!



Great job! :-D



And I love how you can load them just by using them as an argument to newlisp:


$ newlisp http://www.nodep.nl/downloads/newlisp/worm.lsp
#426
newLISP newS /
December 06, 2007, 01:12:32 PM
On a similar note, how would one implement something like a binary search tree in newLISP without having to generate named contexts?  It doesn't seem ... possible.
#427
newLISP newS / Re: (length my-list) - O(n) or O(1) ?
December 04, 2007, 02:07:12 PM
Quote from: "Cyril"Oops! Wrong! It's O(n)! Very, very evil typo. Sorry, folks. And nobody have corrected me. :-(


Are you saying that's what I get for trusting you? ;)



Yeah.  That's a problem I think...  Lutz?
#428
Anything else we might add? /
December 04, 2007, 07:04:20 AM
Quote from: "cormullion"However, none of the newLISP users from this forum are into IRC, so the channel has been quiet for most of the year.


Ah, oh well.  It's just nice sometimes when you're a newLISPnewBIE and have a question that needs a quick answer.


QuoteBut if you like talking to yourself, it's kind of fun... :)


I might take you up on that. ;-)
#429
newLISP newS /
December 04, 2007, 07:01:32 AM
Thanks guys!


Quote from: "Lutz"For 'last' the optimization was never done, but is trivial to add for 9.2.9.


That would be just hunky-dory! :-D
#430
I'm trying to create a tree class using http://www.alh.net/newlisp/phpbb/viewtopic.php?p=11096">the data-type macro Lutz introduced.  The class is created like so:


(data-type (tree data children))

data can be anything, and children is a list of other trees.  So say I want to define a function (or macro, don't know which) called tree:add-child that takes a tree node and a child node and adds the child to the parent node's children list.  How do I do this without copying data?  I just want to update the list inside the parent without having to duplicate and replace it.



If the data is an integer, then an example of a node containing 2 leaf nodes is this:


(tree 0 ((tree 1 ()) (tree 2 ())))

Also, I do not want this to be a solution involving creating a context for every object, because I want to be able to refer to these things anonymously and in general that's not a very elegant solution as it requires that you have a naming scheme for your context symbols (like tree1, tree2, etc...).



Any help is much appreciated!
#431
newLISP newS / (length my-list) - O(n) or O(1) ?
December 03, 2007, 07:00:06 PM
Does (length) calculate the size of a list by iterating through its elements or is there a value stored somewhere internally?
#432
Anything else we might add? /
December 03, 2007, 06:29:14 PM
OK, this is getting frustrating, I'm starting to get the impression that newLISP just isn't designed for object oriented programming (edit: which appears to be what Lutz said above, now that I'm reading through this thread in more detail...).  Using my previous code this does not work:


(set 'points '())
(dotimes (i 5)
(push (point i i) points)
)
(println (point:move '(points 0) 200 200))


I'm guessing you've probably gotta jump through some hoops by using temporary variables to get that to work...  Yeah, this works:


(set 'points '())
(dotimes (i 5)
(push (point i i) points)
)
(set 'tmp (points 0))
(println (point:move 'tmp 200 200))
(nth-set (points 0) tmp)


This is slightly disappointing... I'm hoping some newLISP guru will put my newbie-self in place and show me the proper way to do this...



Edit:  I think it's possible to actually get this to work by writing code to anticipate this situation in data-type.  For example it could check (list? _str) and if so then do the temporary variable thing itself.  Anyone wanna give this a shot?  I won't be able to work on this right now because I've got an another project, but once that's done if no one's done it I'll give it a shot and post here if I'm successful.
#433
Anything else we might add? /
December 03, 2007, 06:19:58 PM
OK, I haven't been able to figure out how to get it working using the previous paradigm, but I did fix it by changing the semantics a bit.



The data-type function is the same except the setter/getters are functions instead of macros:


(define-macro (data-type)
(let (ctx (context (args 0 0)))
    (set (default ctx) (expand '(fn () (cons ctx (args))) 'ctx))
    (dolist (item (rest (args 0)))
(set 'idx (+ $idx 1)
(sym item ctx)
(expand '(lambda (_str _val)
(if (set '_val (eval _val))
          (nth-set ((eval _str) idx) _val)
          ((eval _str) idx)
)
)
'idx
)
)
)
ctx
)
)


Next, forget about the macro-like syntax of (point:x pt 5).  Instead, quote the point:


(point:x 'pt 5)

That works.  Now I've even been able to get compound functions (not macros) to work:


(define (point:move _pt _x _y)
(point:x _pt _x)
(point:y _pt _y)
(eval _pt)
)

(point:move 'pt 8 7)


That works too.  And not only that, but using the colon syntax for getting works too! (but *not* setting)

Except you use the previous format of not quoting the object.


(:x pt)

If anyone figures out how to get this working using macros though... let me know.  It seems to me that newLISP macros have something funky going on with them besides simply changing their parameters to be quoted...
#434
Anything else we might add? / IRC channel
December 03, 2007, 05:31:33 PM
Is there an IRC channel for newLISP?  I think it would be really helpful to the entire community if there was...
#435
Anything else we might add? /
December 03, 2007, 05:24:40 PM
Hi, while playing around with Lutz's original macro I tried to do stuff like this:


(:x pt 2)

And it didn't work.  :p



So I set about to fix that.  I had planned on reading this thread when I was finished and low and behold I saw that michael had done a similar thing.  I then copied some of the suggestions made in this thread to make the code even nicer.  However, I'm still having problems, and forgive me but I only skimmed over this thread.



(BTW michael, here are smaller versions of those pictures, just remove the -big from the URL: http://www.kenrockwell.com/bmw/images/m3-2007/top-790.jpg">pic1, http://www.kenrockwell.com/bmw/images/m3-2007/exploded-789.jpg">pic2)



So here is my version as it is now:


(define-macro (data-type)
(let (ctx (context (args 0 0)))
    (set (default ctx) (expand '(fn () (cons ctx (args))) 'ctx))
    (dolist (item (rest (args 0)))
(set 'idx (+ $idx 1)
(sym item ctx)
(expand '(lambda-macro (_str _val)
(if (set '_val (eval _val))
          (nth-set ((eval _str) idx) _val)
          ((eval _str) idx)
)
)
'idx
)
)
)
ctx
)
)


However, (:x pt 5) still doesn't work!  It runs, it just doesn't modify pt. Oddly enough (point:x pt 5) does.  Can anyone help me out here as to why this is happening?



Also, how do I write a compound method like this:


(define-macro (point:move _pt _x _y)
(:x _pt _x)
(:y _pt _y)
)

; this doesn't work either

(define-macro (point:move _pt _x _y)
(point:x _pt _x)
(point:y _pt _y)
)

; if I try this:
(define-macro (point:move _pt _x _y)
(println (point:x _pt))
)

; I get this error:
invalid function : ((eval MAIN:_str) 1)


So that when I do (:move pt 5 6) [or (point:move pt 5 6)], point should contain the values 5 and 6.  Many thanks!