Re: Pico, difficulty with circular lists
On Fri, Apr 11, 2008 at 04:46:14PM +0200, Alexander Burger wrote: > Exact. The function 'MUL' is defined before that, but as you just read > the file (without executing it), it will be undefined when you hit that > read macro. Well, and while thinking about it: Inspecting files in such a way (with 'read') has its limits. 'lint', for example, will have the same problems. This is perhaps the reason that most debugging tools work directly in the heap. Cheers, Alex -- Software Lab. Alexander Burger Bahnhofstr. 24a, D-86462 Langweid [EMAIL PROTECTED], www.software-lab.de, +49 8230 5060
Re: Pico, difficulty with circular lists
Hi Jon, > the 'length' function, I've written a new function 'longer' that > works OK, like this: > > # Returns N+1 if Lst is longer than N, otherwise NIL > (de longer (Lst N) > (for (I . X) Lst > (T (> I N) I) > NIL) ) Yep, this is a good solution. > [rcsim/lib.l:42] !? (MUL X VX) > MUL -- Undefined > ? > > I guess the trouble could be the backquotes in this line: > > (+ `(MUL X VX) `(MUL Y VY)) Exact. The function 'MUL' is defined before that, but as you just read the file (without executing it), it will be undefined when you hit that read macro. Cheers, Alex -- Software Lab. Alexander Burger Bahnhofstr. 24a, D-86462 Langweid [EMAIL PROTECTED], www.software-lab.de, +49 8230 5060
Re: Pico, difficulty with circular lists
Hi again, > (length '(de *Day . (Mon Tue Wed Thu Fri Sat Sun .))) .. Why does 'length' have such problems here? Circular lists are generally rather dangerous. PicLisp supports (kind of ;-) only what I would call "primary circular lists", i.e. lists that circulate back to the first cell I understand. I've now lowered my standards a bit. Instead of using the 'length' function, I've written a new function 'longer' that works OK, like this: # Returns N+1 if Lst is longer than N, otherwise NIL (de longer (Lst N) (for (I . X) Lst (T (> I N) I) NIL) ) # Checking a function/structure already in memory (de ifwarn (Prg) (when (and (lst? Prg) (not (longer Prg 100))) (when (and (= (car Prg) 'if) (<> (length Prg) 4)) (println Prg) ) (mapcar ifwarn Prg) NIL) ) # Checking a file (.l or other) (de ifwarnfile (Path) (in Path (while (read) (ifwarn @))) ) As you can see, I now use 'read' (instead of 'str' as I did earlier). However, even 'read' seems not to be "bullet proof", as can be seen here: (ifwarnfile "rcsim/lib.l") : (ifwarnfile "rcsim/lib.l") [rcsim/lib.l:42] !? (MUL X VX) MUL -- Undefined ? I guess the trouble could be the backquotes in this line: (+ `(MUL X VX) `(MUL Y VY)) Or? /Jon
Re: Pico, difficulty with circular lists
Hi Jon, > (length '(de *Day . (Mon Tue Wed Thu Fri Sat Sun .))) > .. > Why does 'length' have such problems here? Circular lists are generally rather dangerous. PicLisp supports (kind of ;-) only what I would call "primary circular lists", i.e. lists that circulate back to the first cell (a b c .) but not e.g. (a b . (c d .)) which circulates back to the third cell. Though the system itself (for example the garbage collector) can handle arbitrary circular lists internally, reading, printing or mapping may cause problems. It is up to the application to be prepared for possible circularities, and act appropriately. The reason is that it would impose enormous overhead to keep track of all visited cells and in all circumstances to detect circularities. System functions like 'length', 'size', comparisons - but also some tools like 'who' - keep track of the first cell to detect primary circularities, but not more. I found, though, that this is surprisingly useful, and covers most practical cases. So your idea to use the return value of 'length' (i.e. 'T' for circular lists) is very good (at least in cases where the runtime of repeated length calculations does not hurt), but it cannot solve all problems. An improved solution could keep a list of cells during traversal, to detect at least a circularity up to a certain size. Cheers, Alex -- Software Lab. Alexander Burger Bahnhofstr. 24a, D-86462 Langweid [EMAIL PROTECTED], www.software-lab.de, +49 8230 5060
Re: Pico, difficulty with circular lists
Only simple circular lists seem to work properly. '(a b . (c d e .)) is equivalent to '(a b c d e c d e ...) : (de X . (a b .)) -> X : X -> (a b .) : (de X . (a . (b c .))) # X redefined -> X : (cdr X) -> (b c .) : X -> (a b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c b c and so on forever. John On 10 Apr 2008, at 3:35 AM, Jon Kleiser wrote: Hi Alex, After you made me aware of the circular list in lib/form.l, I changed my 'ifwarn' function to skip circular lists by putting in the (nT (length Prg)) test, like this: (de ifwarn (Prg) (when (and (lst? Prg) (nT (length Prg))) (when (and (= (car Prg) 'if) (<> (length Prg) 4)) (println Prg) ) (mapcar ifwarn Prg) NIL) ) However, this fix may not be the ultimate, because in lib/misc.l there's a tougher problem, that can be illustrated by this ... (length '(de *Day . (Mon Tue Wed Thu Fri Sat Sun .))) .. which I'm not able to abort with Ctrl-C or Ctrl-D. Ctrl-Z does it. Why does 'length' have such problems here? /Jon