4 ms·
$ txr This is the TXR Lisp interactive listener of TXR 173. Use the :quit command or type Ctrl-D on empty line to exit. 1> (let ((l '#"abcd 123456789 zz")
by kazinator 10y ago
$ txr
This is the TXR Lisp interactive listener of TXR 173.
Use the :quit command or type Ctrl-D on empty line to exit.
1> (let ((l '#"abcd 123456789 zz")
(i '(1 0 1 1 2 1 2 1 1 0 1 0 1 0 1)))
(gun (pop [l (pop i)])))
(#\1 #\a #\2 #\3 #\z #\4 #\z #\5 #\6 #\b #\7 #\c #\8 #\d #\9)
Conversion to string:
2> (let ((l '#"abcd 123456789 zz")
(i '(1 0 1 1 2 1 2 1 1 0 1 0 1 0 1)))
(cat-str (gun (pop [l (pop i)]))))
"1a23z4z56b7c8d9"
What is this gun we have armed ourselves with? It stands for "generate until nil": it generates a lazy list whose elements are formed by successively evaluating the argument expression. If the expression yields nil then the list terminates (without adding that nil as an item).
The expression uses side effects. The pop macro is straight of ANSI CL. Except, in TXR Lisp, list processing stuff like `pop` works on other sequences such as character strings: we can pop the letter a out of a place which holds "abcd", leaving "bcd" in that place. This approach just "popped into my head" immediately.
We also have a syntactic [...] which gives us indexing (and other things).
We also have #"..." which is a "word list literal". Simply, #"foo bar baz" means ("foo" "bar" "baz"). Note there is no quote there to prevent the evaluation of this as a form; so we still have to use one.
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. We can map a string of ASCII digits to the corresponding values like this:
3> [mapcar chr-digit "01234"]
(0 1 2 3 4)
Characters are characters, and numbers are numbers. Dynamic typing: yes please; daft typing: no thanks.
- 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.