newLISP Fan Club

Forum => newLISP newS => Topic started by: hs on March 05, 2008, 04:57:00 AM

Title: list index out of bounds
Post by: hs on March 05, 2008, 04:57:00 AM
can we have it graciously not to stop the running program?



personally i like the old way of returning the last element

it's because my program input can sometimes be an empty list (i have no control of my data source)



(set 'b '())

(b 0);program stops running

(b -1);progam stops running

(0 1 b);program stops running

(-1 1 b);proram stops running



i can put a condition of (if (= b '()) (do something)

but sometimes my input can be empty, one, or two elements

so my old codes of (input 2), (input 1), etc break

yes i can put conditions too

but that makes my program long and ugly

my past few weeks were of maintenance nightmare



my main gripe is that newlisp just stops running due to list index out of bound

it's like watching your favorite dvd and your software media player (being too smart) stops running due to a scratch

while the hardware dvd player (being dumb) just keep running the dvd till the end



sorry for my rants
Title:
Post by: cormullion on March 05, 2008, 05:17:46 AM
Yes, I have some sympathy, as you might have read earlier this year (//http://www.alh.net/newlisp/phpbb/viewtopic.php?t=2093, and //http://www.alh.net/newlisp/phpbb/viewtopic.php?t=2084).



It's true I miss the old friendly and relaxed behaviour, but I appreciate the reasons for the sterner approach -  I've rationalized the 'improvement' now as a lesson to me not to write sloppy code in the first place... :)
Title:
Post by: hs on March 05, 2008, 05:43:44 AM
yes, i do notice my code is less sloppier than it used to be

but now it consists of batallions of special cases

big, bulky code, mentally exhausting



is there any 'best practice' to avoid index out of bound cases, especially when u have less control over the list



basically i want my code to just run, not suddenly stops due to special rare case
Title:
Post by: HPW on March 05, 2008, 05:54:31 AM
Maybe you can make your own 'error-event'.

Not sure if it will work in this case.
Title:
Post by: Lutz on March 05, 2008, 06:37:30 AM
The empty list a logical false:


(if lst (lst 0))

only returns the first element if not empty, else returns nil.
Title:
Post by: hs on March 05, 2008, 06:45:53 AM
i'll have to dig the error event, thx



my recent case is that i have a newlisp program in remote server to get info about time from some website, write the time to a file to be served via ajax



after regex, the time string t can be say "17:00". "17", or ""



(set 'T (parse t ":"))

;;;T can be

("17" "00") ;(T 0) (T 1) work

("17") ;(T 1) break, program stops running

(); (T 0) break, newlisp stops



my fix was to use couple if statements

the older newlisp would probably give 17:17

which is okay, because at time 17:01, the whole program (if using older newlisp) would work as nothing happens ... accuracy is not very important in my case



newlisp stops fetching and updating the ajax file, that's big problem, especially at remote server, especially when my eyes are not on that ajax file
Title:
Post by: cormullion on March 05, 2008, 07:12:05 AM
Yes, I see... I write code like that too :)



Perhaps a quick solution might be to pad out the original time string a bit.


(set 'T (0 2 (append (parse t ":") '("00" "00"))))


so you'll always end up with two strings in T.
Title:
Post by: cormullion on March 05, 2008, 08:30:01 AM
Actually, though, you could just use slices rather than indexes. Slicing doesn't generate errors as readily as indexing:


>(set 't "")
""
> (set 'T (parse t ":"))
()
> (0 1 T)
()
> (T 0)

list index out of bounds
>
Title:
Post by: hs on March 05, 2008, 11:22:13 AM
oh yeah i forgot to mention that i have to adjust the time to my local time

say from 17:00 to 19:02 ... so i have to get 2 values



either select or slicing returns a list, not an element

(0 1 (parse t ":")) ;gives '("17")

(1 1 (parse t ":")) ;gives '("00")



so eventually i will need to use

(set 'hr ((parse t ":") 0))

(set 'mt ((parse t ":") 1))



i keep wondering, what function to use to extract an element from a list ... (first) comes to mind



i find inconsistency for (fist) .. (by accident, just now)



(set 'b '())

(if b (b 0)) ;gives '()

(b 0) ;list index out of bounds

(first b) ;gives nil ... shouldn't it give error too?



thx
Title:
Post by: cormullion on March 05, 2008, 11:55:08 AM
Quote from: "hs"i keep wondering, what function to use to extract an element from a list ... (first) comes to mind


Yes. pop will do it too, although it's destructive.


Quote from: "hs"
(set 'b '())

(if b (b 0)) ;gives '()

(b 0) ;list index out of bounds

(first b) ;gives nil ... shouldn't it give error too?


Yes, perhaps it should. But, at a guess, the functional nature of newLISP has overrided that: the first function has successfully analysed the list and returned nil as the answer. Perhaps it managed to find out that the list was empty without having to use index numbers... :)