# Plus or minus polyglots!

Your submission must take a list of numbers (in whatever list format your language supports, or using multiple function / command-line parameters) or a string of numbers separated by any character that is not 0123456789. In one language, it must add all of them and output the sum. In another language, it must output them subtracted from each other in order. Example:

12
5
7
2


In one language, it must output 26, and in another it must output -2. Note that all numbers inputted will be positive integers less than 100. There will never be more than 20 numbers provided, so you will never output greater than 2000 or less than -1899. Any questions? Comment below!

• Is trailing whitespace in the output allowed? – Business Cat Apr 18 '17 at 13:10
• Are two different versions of the same language allowed? See the Python 2 / 3 Answer by HyperNeutrino – Mr. Xcoder Apr 18 '17 at 13:12
• @Mr.Xcoder it's allowed. – programmer5000 Apr 18 '17 at 13:24
• @BetaDecay you think that's a problem, check out code-golf! – programmer5000 Apr 18 '17 at 23:46
• @programmer5000 You mean [code-golf] -[polyglot]? – Erik the Outgolfer Apr 19 '17 at 8:15

# Jelly / 05AB1E, 3 bytes

00000000: c6 71 53                                         .qS


This is a hexdump (xxd) of the submitted program.

Try it online!

## Jelly: Sum

Jelly uses the Jelly code page, so it sees the following characters.

İqS


Try it online!

### How it works

İqS  Main link. Argument: A (array)

İ    (ignored)
q   Unrecognized token. This breaks the link; nothing to the left is executed.
S  Take the sum of A.


## 05AB1E: Difference

05AB1E uses Windows-1252, so it sees the following characters.

ÆqS


Try it online!

### How it works

Æ     Take the difference of the input.
q    Quit.
S   (ignored)


# Python 2 / 3, 52 bytes

lambda l:sum(l[:1]+[x*int(1-(1/2)*4)for x in l[1:]])


int(1-(1/2)*4) returns 1 in Python 2 because 1/2 evaluates first to 0, and then 0 * 4 = 0.

int(1-(1/2)*4) returns -1 in Python 3 because 1/2 evaluates first to 0.5, and then int(0.5 * 4) = int(2.0) = 2.

• Is using the same language, with other versions even allowed? – Mr. Xcoder Apr 18 '17 at 13:07
• @Mr.Xcoder I don't see why not, I've seen other languages using either different versions of C, Java, Python, and Befunge. I'll ask on meta though or see if I can find a related meta post. – HyperNeutrino Apr 18 '17 at 13:08
• Good use of math tricks due to version changes, though. – Mr. Xcoder Apr 18 '17 at 13:10
• @Mr.Xcoder Thanks! I've asked the question on meta here; I should hopefully get an answer soon. – HyperNeutrino Apr 18 '17 at 13:12
• @programmer5000 Integer division. – HyperNeutrino Apr 18 '17 at 13:26

# C and C++ (both from GCC), 8175 73 bytes

int c;int f(int*a,int l){auto d=.5;c=*a++;for(;--l;a++)c+=*a-4*d**a;c=c;}


Takes a pointer to array and length.

Explanation: still using @Steadybox' explanation :p In C, auto d=.5 declares an integer variable with the auto storage class (which is the default), which is then initialized to 0, whereas in C++11 it declares a double, which is initialized to 0.5.

C - plus language: Try it online!

C++ - minus language: Try it online!

• Should I consider it ironic that C is the plus language and that C++ is the minus language? – user Aug 30 at 19:03
• @user that was intentional :p – betseg Sep 1 at 10:47

# 05AB1E / Jelly, 5 bytes

00000000: 4f71 7f5f 2f                             Oq._/


05AB1E sees:

Oq_/
Explanation:

Oq_/ 1 implicit argument.
O     Take the sum of the first input item.
q    Quit.
_/ Not functional.

Jelly sees:

Oq
_/
Explanation:

_/ Main link. Arguments: z
_/ Subtract z's elements in order.

Oq Helper link. Not functional.
• Does this use the Jelly codepage? – programmer5000 Apr 18 '17 at 13:00
• @programmer5000 This is a raw bytestream that uses CP-1252 for 05AB1E and JELLY for Jelly. Hence the 7f. – Erik the Outgolfer Apr 18 '17 at 13:01
• Oh, didn't realize that! – programmer5000 Apr 18 '17 at 13:02

# Actually / Jelly, 4 bytes

00000000: e4 81 5f 2f                                      .._/


This is a hexdump (xxd) of the submitted program. It cannot be tested in raw form online; TIO doesn't support the CP437 encoding.

## Actually: Sum

Actually uses CP 437, so it sees the following characters.

Σü_/


Try it online!

### How it works

Σ     Take the sum on the input.
ü    Print and empty the stack.
_   Natural logarithm. Ignored since the stack is empty.
/  Float division. Ignored since the stack is empty.


## Jelly: Difference

Jelly uses the Jelly code page, so it sees the following characters.

ỵ¹_/


Try it online!

### How it works

ỵ¹_/  Main link. Argument: A (array)

ỵ     Unrecognized token. Splits the link.
¹    Identity; yield A.
_/  Reduce (/) A by subtraction (_).

• I read that as actually, Jelly. :) – programmer5000 Apr 18 '17 at 13:40
• You abused the fact that an undefined character acts like ṛ :) – Erik the Outgolfer Apr 18 '17 at 13:42
• @EriktheOutgolfer More or less. To be perfectly honest, I'm not quite sure what unrecognized tokens do. – Dennis Apr 18 '17 at 13:50
• On second thought, I think that you just chose Actually just because it has ü assigned to the perfect function >_> – Erik the Outgolfer Apr 18 '17 at 13:52
• @programmer5000 It's jelly, actually. – Arjun Apr 20 '17 at 6:20

# Python 2 and 3, 44 41 bytes

lambda l:eval(l.replace(' ','-+'[1/2>0]))


Takes space-separated numbers.

-3 bytes thanks to @JonathanAllan

Try it online: Python 2 (minus) Python 3 (plus)

• Use '-+'[1/2>0] to save three bytes. – Jonathan Allan Apr 18 '17 at 18:44

# CJam/Jelly, 6 bytes

q~:-
S


### CJam

q~    e# Read a list from input
:-    e# Reduce by subtraction
S     e# Push a space
e# Implicit output


Try it online!

### Jelly

(using UTF-8, not the Jelly code page)

q~:- is the helper link. Since it doesn't get called, it really doesn't matter what it does. S computes the sum of an array.

Try it online!

• Jam and Jelly. Makes sense to use two similar things together. – mbomb007 Apr 18 '17 at 14:11
• This is invalid. Jelly has 0x7f for newline, this has 0x0a for newline. In Jelly, this is actually the same as q~:-½S. Unfortunately, the fix (q~:-e#\x7fS) is larger. Try it online! (CJam) and Try it online!, each one with its own encoding to test for yourself. – Erik the Outgolfer Apr 19 '17 at 8:11
• Alternatively, you can use UTF-8 encoding for Jelly, which makes this valid as it currently is, but please specify it. – Erik the Outgolfer Apr 19 '17 at 8:12

# JavaScript / Cubix, 36 bytes

//.!v+u;$I^@O<.Iu a=>eval(a.join-)  # Try it! The JavaScript function can be tested using the snippet below, the Cubix program can be tested here. # How does this work? ## JavaScript The first line is a line comment to JavaScript, since it starts with two slashes, so JavaScript only sees the bottom line (a=>eval(a.join-)), which takes an array as input, joins it with minus signs inbetween, and then runs that as code, resulting in the subtraction of all elements in the array. let f= //.!v+u;$I^@O<.Iu
a=>eval(a.join-)

console.log(f([1,2,3,4,5]))
console.log(f([3,1,4,1,5]))

## Cubix

Cubix sees the following cube (notice that Cubix ignores all whitespace):

      / / .
! v +
u ; $I ^ @ O < . I u a = > e v a l ( a . j o i n  -  ) . . . . . . . . . . . . . . . . . . .  ### The Beginning The IP starts on the third line, pointing east. It hits the 'I' command, which takes a number from the input, and pushes it to the stack. Then, it is redirected by '^' into the sum-loop. ### Sum-loop I removed all characters not part of the sum loop, and replaced them by no-ops ('.'). The IP initally arrives on the second line, pointing east.  . . . ! v + u ;$
. . . . . . I u . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . .
. . .
. . .


First, the '!' command checks the top element. If that is 0 (i.e. we have reached the end of the input), the next instruction ('v') is executed, reflecting the IP out of the loop. If we have not yet reached the end of the input, we add the top two items together ('+', the second item is the sum up to that point, the top item the new input). Then, the IP wraps to another face of the cube, into the 'u' character, which causes the IP to make a u-turn, and execute a 'I' command (read another input integer), while pointing north. The IP wraps back to the top face, skips ('$') the delete instruction (';') and makes another u-turn, back to the point at which we started. ### The End If the IP is reflected out of the loop, the following characters are executed:  . . . . v . . ; . . . @ O < . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  These instructions delete the top element (which is zero), and then output the top element (the sum) as an integer. Then the '@' command is reached, so the program ends. # JavaScript (ES6) / QBasic, 84 83 bytes '';f=x=>eval(x.join+)/* INPUT a FOR i=1 TO 2 i=0 INPUT n a=a-n PRINT a NEXT i '*/  Another solution with the comment hack! JavaScript computes the sum. It takes in an array containing numbers as input. Outputs as function return. You can call the function like alert(f([10,20,30])). QBasic computes the difference. Repeatedly asks for input. As soon as you enter a value, it outputs the difference of all the numbers you have entered 'til the time of hitting Enter and again asks for input. Keeps on doing the same until the end of everything. ## How does it work? In QBasic (a language of structured BASIC family; it does not require line numbers), ' marks the beginning of a comment which goes till the end of the line. Whereas in JavaScript, it marks the start of a string. So, the whole first line is marked as a comment in QBasic but in JavaScript, the line is executed (and this line contains the JavaScript part that adds the numbers as well as a /* at the end which starts a comment in order to hide the rest of the QBasic code from JavaScript interpreter.) The code from second line to second-last line contains the QBasic code to compute the difference of all the input numbers (the code is very self-explanatory). The last line contains '*/. ' causes the QBasic interpreter to interpret the following code as a comment, whereas in JavaScript, it does not have any effect as it is a part of a comment (which was started at the end of the first line). The following code (*/) causes JavaScript to end the comment which was started in the first line, but it is not executed in QBasic because QBasic thinks it's a part of a comment. ## Test Cases JavaScript (adds): '';f=x=>eval(x.join+);/* INPUT a FOR i=1 TO 2 i=0 INPUT n a=a-n PRINT a NEXT i '*/ console.log(f([12,5,7,2])); QBasic (subtracts): Go to this website. Copy paste the following code in their text-editor : 1 '';f=x=>eval(x.join+);/* 2 INPUT a 3 FOR i=1 TO 2 4 i=0 5 INPUT n 6 a=a-n 7 PRINT a 8 NEXT i 9 '*/  The reason why line numbers are required is that the website I mentioned only supports unstructured BASIC languages. And that website is the only decent online BASIC interpreter I could find. However, running the code present in the top of the post (the code without line numbers) should work fine in any good QBasic interpreter that supports structured BASIC and ' as a comment-starter (few do not, most do, though). ## Notes • This is my first post! I hope it's a good one! • The QBasic part of the code does not require END because the interpreter will never reach it! (It will forever be stuck in the infinite loop; always asking for more input.) • @DomHastings I've made a minor edit for you – caird coinheringaahing Aug 30 at 19:37 • Thank you! Apologies again... – Dom Hastings Aug 30 at 19:42 # Python 2 and 3, 33 bytes lambda l,*r:l+sum(r)*(1/2>0 or-1)  Takes input as separate parameters. • lambda l,*r:l+sum(r)*(1/2-.5)*2 for 31 bytes – ovs Apr 21 '17 at 12:10 • @ovs I purposefully avoided returning a float. If you are allowed to, then *(1/2*4-1) is a byte less than yours. – Veedrac Apr 21 '17 at 12:34 • I think the return type does not really matter as long the value is correct – ovs Apr 21 '17 at 12:40 # Brain-Flak / Brain-Flueue, 20 bytes ({}<([({{}})]){}>{})  Try it online! (Brain-Flak) (plus) Try it online! (Brain-Flueue) (minus) ## Explanation The only difference between Brain-Flak and Brain-Flueue is that Brain-Flueue replaces the two stacks (last in first out) used in Brain-Flak with two queues (first in first out). Naturally this program uses this difference. ### Annotated Code ( ) Push the sum of... {} the first input, < > zero, {} the remaining sum (see below) ([ ]) Push the of below line ({{}}) Pop all the input sans first, push the sum {} Pop (in Brain-Flak, this discards the negative sum, in Brain-Flueue, the positive)  # JS (ES6), CGL (CGL Golfing Language), 32 26 bytes  x=>eval(x.join-) //-⨥  JS does the subtraction and CGL does the addition. ## JS: x=>eval(x.join-)  An anonymous function that subtracts each element in the array using Array#reduce. //-⨥  A comment. ## CGL  x=>eval(x.join-)  What looks like a space on the first line is actually a non-breaking space, a comment in CGL. The first line is ignored. //-⨥  The /s do nothing. The - decrements the current stack pointer so it is pointing to input. ⨥ adds the current stack (input) together, pushes that to the next stack, and increments the current stack. It is implicitly outputted. • You can shorten the JS version by using x=>eval(x.join-), saving 5B – Luke Apr 18 '17 at 18:00 • – Adám Apr 20 '17 at 14:22 • @Adám you are right. Fixed. – programmer5000 Apr 20 '17 at 14:50 # Whispers v2/Add++, 72 bytes D,f,@:,v¦Ω_ y:'' Dx,]getchar,y,+x,x$f>y
> Input
>> ∑1
>> Output 2


Try it online! (Whispers v2)

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.

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.

# Befunge-93 / Befunge-98(FBBI), 23 bytes

#<&01v>-.@.+
~^#+&_^#+1


(Note: there must not be trailing whitespace in the input.)

### Explanation (both languages)

The control flow is the same in both languages until we reach EOF.

#<&01v>-.@.+
#<               # (skipped)
&              # Get the first number
0             # Push 0
1            # Push 1
v           # Go down to the next line

~^#+&_^#+1
_           # The top 1 is nonzero, so the we go to the left
(begin loop)
&            Get the next number
+             Add it to the running total (which starts as the 0 we pushed)
^#              (skipped)
~                Get the next separator character
^#         (skipped)
_           The separator plus 1 is nonzero, so we keep going left
(repeat)


By the time we run out of input, the top stack element is the sum of all the numbers but the first, and the first number is underneath it. When we reach EOF, the control flow diverges, because Befunge-93 pushes -1, while Befunge-98 reflects the instruction pointer.

### Explanation continued (Befunge-93)

~^#+&_^#+1
&         # Get the last number
+          # Add it to the running total
^#           # (skipped)
~             # On EOF: push -1
+1    # Add 1 to get 0
^#      # (skipped)
_        # The top of the stack is 0, so we turn to the right
^       # Go up to the first line

#<&01v>-.@.+
>       # Turn to the right
-      # Subtract, from the first number, the sum of all the others
.     # Print the result
@    # End the program


### Explanation continued (Befunge-98)

~^#+&_^#+1
&         # Get the last number
+          # Add it to the running total
^#           # (skipped)
~             # On EOF: reverse direction
^            # Go up to the first line

#<&01v>-.@.+
<            # Turn to the left
#             # No-op
+  # Αdd the first number to the sum of all the others
.   # Output the result
@    # End the program


# Python 3 / Javascript, 55 bytes

1//1;"""
f=(l)=>eval(l.join-)//""";f=lambda l:sum(l)


Javascript function taken from Luke's Javascript / Cubix answer. Python function is trivial. Works by abusing the fact that // is an operator in python and a line commentor in javascript, and then using """ to comment out the javascript section.

## Running the code

In Python:

1//1;"""
f=(l)=>eval(l.join-)//""";f=lambda l:sum(l)

print(f([1, 2, 3]))


In Javascript:

1//1;"""
f=(l)=>eval(l.join-)//""";f=lambda l:sum(l)

console.log(f([1, 2, 3]));