D,f,@:,v¦Ω_
y:''
Dx,]getchar,`y,+x,`x
$f>y
> Input
>> ∑1
>> Output 2
Try it online! (Whispers v2)
Try it online! (Add++)
Both are encoded as UTF-8, despite Add++ having it's own code page. Whispers takes the sum, Add++ takes the difference. This assumes that both languages take input from STDIN. Add++ exits with an error after having output the difference, which is allowed by default.
How it works (Whispers)
Whispers is very nice to polyglot, as it simply ignores every line that doesn't begin with >
. As a result, the executed code is
> Input
>> ∑1
>> Output 2
This simply takes in input, calculates its sum and outputs it.
How it works (Add++)
Add++ will error as soon as it reaches > Input
, so only the stuff before that matters:
D,f,@:,v¦Ω_
y:''
Dx,]getchar,`y,+x,`x
$f>y
Taking input from STDIN is much longer in Add++ than from ARGV, but I wanted to be consistent across languages. Add++ doesn't have a builtin for reading from STDIN, aside from the ]getchar
command, which simply reads in a character (similar to brainfucks ,
command), and assigns it to the active variable (in this case x
). Therefore, in order to read something from STDIN, we need the following structure:
y:''
Dx,]getchar,`y,+x,`x
As ]getchar
returns the empty string when it reaches the end of the input, we can use a do-while loop to read each character in. We start by declaring a variable y
equal to the empty string. Next, we enter a do-while loop that runs until x
is falsey (i.e. the empty string). This loop reads in a character from STDIN and concatenates it to y
. Eventually, y
is equal to the contents of STDIN.
Next, we define and run a function on the input:
D,f,@:,v¦Ω_
$f>y
As there is no way to evaluate a string in non-functional Add++ code, we're stuck with y
as a string, rather than an array, which is what we need. Therefore, we have to pass the string into a function:
$f>y
The function itself is fairly basic to understand:
D,f, ; Define a function which...
@:, ; Takes 1 argument and outputs its return value
; This argument is the unevaluated string input, and the : flag saves a byte when it comes to outputting the final result
v ; Evaluate the input (convert from a string to an array)
¦Ω_ ; Reduce that array by subtraction
; Return and output this value
Very simply, both languages ignore each other's code, either due to how the code is parsed, or by erroring before it's actually reached.
[code-golf] -[polyglot]
? \$\endgroup\$