(but-last) ?
Posted: Tue Mar 24, 2009 2:54 pm
Hi there,
This is a problem that has arisen several times now in my programs (which do some heavy text file parsing)
I get a text line, (parse) it and then I need a list of tokens but the last one.
A silly example: getting the base URI from a given URI:
"http://example.com/path/to/files/file1.html"
There's surely a hundred ways to do this, but I simply (parse) it given all possible token separators and I get the list
("http" "example.com" "path" "to" "files" "file1.html")
I needed a simple (but-last) function to get all elements but the last one to use is some loops:
I came with different solutions, these are my favourites:
You see, the first one is "less optimal" than the second, but it only mentions lst once and, alas, is more useful to on-the-fly generated lists . OTOH performance is not that horrible, so it's a nice hack (instead of slicing, twisting, kind of) :-)
Then, reading more into the documentation, I arrived to chop. Doh!
So...
We have an asymmetry in here:
So, maybe an alias chop = but-last, except-last, almost-all, firsts could be useful?
I mean, we have the "first" and the "rest" of a list, we could have the "firsts" and the "last"? The truth is (chop) is not an intuitive name for this task, even when it's covered by it.
In my view, the fact that newLISP treats strings and lists the same for many different kind of things is not a favor in this case. I'm thinking in the new user, because I'm a kind-of new user, and, well, you know... :-P
Anyway, your thoughts?
laters
This is a problem that has arisen several times now in my programs (which do some heavy text file parsing)
I get a text line, (parse) it and then I need a list of tokens but the last one.
A silly example: getting the base URI from a given URI:
"http://example.com/path/to/files/file1.html"
There's surely a hundred ways to do this, but I simply (parse) it given all possible token separators and I get the list
("http" "example.com" "path" "to" "files" "file1.html")
I needed a simple (but-last) function to get all elements but the last one to use is some loops:
I came with different solutions, these are my favourites:
Code: Select all
(define (but-last lst)
(rest (rotate lst)))
(define (but-last2 lst)
(1 (- (length lst) 1) lst))
(define (but-last3 lst)
(chop lst))
Then, reading more into the documentation, I arrived to chop. Doh!
So...
We have an asymmetry in here:
Code: Select all
'(this is a list)
first rest
chop last
I mean, we have the "first" and the "rest" of a list, we could have the "firsts" and the "last"? The truth is (chop) is not an intuitive name for this task, even when it's covered by it.
In my view, the fact that newLISP treats strings and lists the same for many different kind of things is not a favor in this case. I'm thinking in the new user, because I'm a kind-of new user, and, well, you know... :-P
Anyway, your thoughts?
laters