implicit indexing beyond the list

Started by Dmi, June 06, 2005, 04:57:27 PM

Previous topic - Next topic

Dmi

> (set 'q '(a b))
(a b)
> (q 1)
b
> (q 2)
b

but from last call I expect "nil" or something similar.

Does I wrong?

I want a list where last few elements will be optional (all together).

Can I know (through one operator) is such element presents in list and (if present) got its value?



Thanks.
WBR, Dmi

Lutz

#1
When you overrun the index it will pick the last (or first on negative index). But arrays will give an "index out of bounds" error.



But the indexing built-in functions (args idx) and (main-args idx) will return 'nil when you overrun the index in either direction.



You can check the length of a list using (length lst).



'nil' in newLISP is always a boolean value never the empty list or end of a list like in other LISP.



Lutz

Dmi

#2
Ok. Thanks Lutz!



So, will be function
(define (ntl l k) (if (> (length l) k) (l k) nil))
a good choice?

Or the worse practice? ;-)



And just for my interest: is that behavior of newLisp a useful feature (and - when?) or simply caused by a design?



And also: as far as I see, newLisp has not useful possibility for car|cdr recursion?

I'm quite happy with newLisp's iterators, but for better understanding...
WBR, Dmi

Lutz

#3
If that function is good or bad depends on the problem to solve at hand. I.e. when you are iterating through a list you will probably use (dolist (x mylist)...) which would adjust to the length automatically. If you would want to pick the last element you would use (last mylist) or (mylist -1) etc..



What I want to say is, in most cases the length of a list is not really of concern when the code you write can adapt to it and refer to positions in relative terms. When the length is important and you do random access into different positions in the list then arrays with bounds error control may be the better solution.



Regarding car/cdr recursion. This type of pattern is very common in traditional LISP but not seen frequently in newLISP code. newLISP code tends to use recursion a lot less and relies much more on iterative code.



But still if your program solution needs it you can employ cdr/car recursion with the equivalent first/rest and you can adjust the default stack size of 1024 to any number you wish. I.e. for the Ackerman benchmark to 100000 here: http://newlisp.org/benchmarks/ackermann.newlisp.txt">http://newlisp.org/benchmarks/ackermann.newlisp.txt . I think apart from this benchmark I have never felt the need.



To sum it up: newLISPs programming style is somewhere in the middle between traditional, more functional LISP style and the more procedural style of scripting languages like Perl or Python.



Last not least, welcome to the group Dmi, I hope you have fun with newLISP and it can help you with you programming tasks.



Lutz

Dmi

#4
Thanks, Lutz!

For your explanation and for newLisp. It's very nice!

I trying to familarize myself with it now :-)
WBR, Dmi