3 ms·
> I won't bother implementing the apparent requirement that the indices be digit characters from a string; I consider it a serious blemish on the language, if i
by cousin_it 10y ago
> I won't bother implementing the apparent requirement that the indices be digit characters from a string; I consider it a serious blemish on the language, if it's doing it implicitly.
There's no such requirement, < (and by extension <<) returns the same result for 0 1 0 2, "0102" or "abac".
- kazinator 10y agoI don't mean that the program requires the input in that form, but that the problem specification (in its strict interpretation) specifies it, and I'm missing that requirement in my solution. Obviously, there is here a language-level requirement in K that 0, "0" or "a" all denote an index zero, at least in the exemplified situation. Though that may help get points on http://codegolf.stackexchange.com http://codegolf.stackexchange.com, it comes across as rather arbitrary. We could easily acheive the same thing without pushing it into the language, at the cost of making a function call, which could have a one-letter name. 12> (defun $ (x) (cond ((numberp x) x) ((chr-digit x)) ((chr-isalpha x) (- (chr-tolower x) #\a)))) 13> ($ #\b) 1 14> [mapcar $ "cdba"] (2 3 1 0) 15> [mapcar $ #(1 3 9 5)] #(1 3 9 5) 16> [mapcar $ "012a3"] (0 1 2 0 3) We basically have to operate under the assumption that writing a function is unacceptable, and I could cater to that view easily by having this function in the TXR Lisp standard library in the next release (probably not under the name $, to reserve that for users). I am rather too convinced of its lack of utility to do such a thing.
- Avshalom 10y ago>Obviously, there is here a language-level requirement in K that 0, "0" or "a" all denote an index zero, at least in the exemplified situation. no, 0 just sorts before 1, "0" before "1" and "a" before "b"
- kazinator 10y agoI see so something like "abac" is basically mapped to 0 1 0 2 by mapping the lowest-order element in that sequence to 0, and the others following suit. In which case, it would be expected that, say, "xyxz" could be used in place of "abac". "Auto-indexifying" a sequence in this way does seem like a useful little operation to have.
- Avshalom 10y agoGrade up (or grade down) returns the index of an array in sorted order. So >"weasels" would return: 0 3 6 5 1 4 2.