$^//.{#}/S1//.$/
Probieren Sie es online!
Das Eingabeformat ist wie folgt:
string
index
Und das Programm ist 1-indiziert.
Erläuterung
Carrot hat mehrere globale Variablen, eine für jeden Typ: String, Float und Array (andere werden in Kürze implementiert). Das Programm startet im String-Modus, in dem alle Operatoren die globale String-Variable beeinflussen. Und ich nenne diese Variablen den "Stapel".
(Beispiel Eingabe: abcdef\n3)
$ Get the first line of the input and set the stack-string to this value
^ Exit caret-mode
stack-string = "abcdef"
/ Operator (behaves differently depending on the argument)
/.{#}/ And the argument to this operator is a regex, so this program gets the matches of this regex into the stack-array
. Any character
{#} Pops a line from the input. So now this evaluates to # of any character where # is the second line of the input (in this case, 3)
stack-array = ["abc"]
And now we just need to get the last character in this string, but first
S1 Join the array on the number 1 and set this to the stack-string. Because the array only contains one element, the number 1 does not appear in the stack-string.
stack-string = "abc"
/ Operator; because the argument is a regex, this retrieves the matches of the regex:
/.$/ Get the last character in the string
stack-array = ["c"]
Nun wird ein Array mit einem Element zurückgegeben, das eine Zeichenfolge der Länge eins enthält, auf der Website jedoch als Zeichenfolge angezeigt.
Wenn wir das Ergebnis wirklich als Zeichenfolge angeben wollten, könnten wir es S","am Ende problemlos tun , aber es spielt keine Rolle, da die Ausgabe auf dem Interpreter immer noch gleich aussieht.