# Write a Program that Writes a function BUT in a different language!

Write the shortest program that takes one input (n) from STDIN (or equivalent) and outputs a simple incrementing function with one argument (x) that returns x + n but the function must be in a different language. Pretty simple!

This is code-golf, normal rules apply, shortest program wins.

Example: ><> to Python (Ungolfed)

!v"def i(x):"a"    return x+"ir!
>l?!;o


Input:

3


Output:

def i(x):
return x+3


EDIT: Anonymous functions and lambda expressions are allowed!

• How big can the input get? As far as I can tell, your example only works with single digit numbers for the ><> half of it. – Sp3000 Nov 9 '15 at 6:12
• In theory in should be able to use any (reasonable) input, but answers that only use a input that could maintain 1 digit are fully acceptable, I did consider reworking the example to correct this before posting but i figured ill leave it for simplicity stake. – Blake Lockley Nov 9 '15 at 6:32
• I don't see any definition for function BUT that we are supposed to write.. -_- – Optimizer Nov 9 '15 at 13:45
• When a function returns another function, it's called a closure. I don't know if this applies cross-language, though... – ETHproductions Nov 9 '15 at 15:31
• @ETHproductions I guess the goal is not to return a function object, but the source code of a function in that other language. – Paŭlo Ebermann Nov 9 '15 at 20:40

# GS2 → K, 2 bytes

•+


This prints a tacit, monadic function. The source code uses the CP437 encoding. Try it online!

## Test run

$xxd -c 2 -g 1 sum-func.gs2 00000000: 07 2b .+$ printf 42 | gs2 sum-func.gs2
42+
$kona K Console - Enter \ for help (42+) 69 111 f : 42+ 42+ f 69 111  ## How it works ### GS2 • GS2 automatically reads from STDIN and pushes the input on the stack. • • indicates that the next byte is a singleton string literal. • Before exiting, GS2 prints all stack items. ### K Left argument currying is automatic in K. Here, n+ turns the dyadic function + into a monadic function by setting its left argument to n. • Which encoding are you using? – Conor O'Brien Nov 9 '15 at 15:30 • @CᴏɴᴏʀO'Bʀɪᴇɴ "The source code uses the CP437 encoding." – ETHproductions Nov 9 '15 at 16:02 # ShapeScript → J, 4 bytes "&+"  This prints a tacit, monadic verb. Try it online: ShapeScript, J ## Test run $ cat sum-func.shape; echo
"&+"
$printf 42 | shapescript sum-func.shape; echo 42&+$ j64-804/jconsole.sh
42&+ 69
111
f =: 42&+
f 69
111


## How it works

### ShapeScript

• ShapeScript automatically reads from STDIN and pushes the input on the stack.

• "&+" pushes that string on the stack.

• Before exiting, ShapeScript prints all stack items.

### J

& performs argument currying.

Here, n&+ turns the dyadic verb + into a monadic verb by setting its left argument to n.

• I'm pretty sure there's a language where you don't need the close-quote. – lirtosiast Nov 9 '15 at 6:04
• There's a good chance you're right, but I can't recall one with implicit input and implicit output. – Dennis Nov 9 '15 at 6:06

# GolfScript → CJam, 4 bytes

{+}+


This prints a code block (anonymous function). Try it online: GolfScript, CJam

## Test run

$cat sum-func.gs; echo {+}+$ printf 42 | golfscript sum-func.gs
{42 +}
$cjam > 69 {42 +} ~ 111 > {42 +}:F; 69F 111  ## How it works ### GolfScript • GolfScript automatically reads from STDIN and pushes the input on the stack. • {+} pushes that block on the stack. • + performs concatenation, which happily concatenates a string and a block. • Before exiting, GolfScript prints all stack items. ### CJam {n +} is a code block that, when executed, first pushes n on the stack, then executes +, which pops two integers from the stack and pushes their sum. • I was just going to post this! – Loovjo Nov 9 '15 at 6:36 • Wow that's impressive. {n +} pushes 42, then executes +. (Probably should be {42 +} or "pushes n") – Justin Nov 11 '15 at 7:10 • @Justin Indeed. Thanks! – Dennis Nov 11 '15 at 14:20 # BrainF*** to JavaScript ES6, 57 bytes ----[-->+++<]>--.[-->+<]>+.+.--[->++<]>.[--->+<]>+++.[,.]  (Assumes that the input is composed of numeric characters) Say 1337 is your input. Then, this would compile to: x=>x+1337  # Rotor to K, 2 bytes '+  Might as well jump in on the K bandwagon. # O to K, 5 bytes i'++o Thanks to @kirbyfan64sos Another version using features added after the challenge was created. i'+ • Gets input, pushes to stack • Pushes '+' as a string • Outputs stack contents • K has automatic currying, so you can just do i'++p. – kirbyfan64sos Nov 9 '15 at 15:51 • The distance between K and O is 4. You need to shorten it one byte. – mbomb007 Nov 11 '15 at 22:39 • @mbomb007 wat. The code is 5 bytes long – phase Nov 11 '15 at 22:42 • The letters. In the alphabet. Get it? – mbomb007 Nov 11 '15 at 22:42 • @mbomb007 that was too complicated for me – phase Nov 11 '15 at 23:05 # R to Julia, 19 bytes cat("x->x+",scan())  This reads an integer from STDIN using scan() and writes an unnamed Julia function to STDOUT using cat(). The Julia function is simply x->x+n, where n comes from the R program. # Malbolge to JavaScript ES6, 71 bytes ('&%@9]!~}43Wyxwvutsr)Mon+HGi4~fBBdR->=_]:[875t4rT}0/Pf,d*((II%GEE!Y}Az  It's always fun to generate Malbolge code. # Minecraft 1.8.7 to K, 7 6 + 33 + 27 + 62 = 129 128 Bytes This is using this version of byte counting. Command blocks (going from left to right): scoreboard objectives add K dummy scoreboard players set J K <input> tellraw @a {score:{name:"J",objective:"K"},extra:[{text:"+"}]} This could probably be golfed a little more, but it's fairly simple: generate a variable J with the objective K and set its score for that objective to the input (there is no STDIN - I figured this was close enough). Then, after a tick, output the score of the variable J for the objective K followed by a +. Easy peasy. • Note that the .dat bytecounting is not applicable for this system, as the Minecraft version listed does not support structure files. – Addison Crump Feb 21 '17 at 9:02 • For the second one, couldn't you remove the extra, place the score JSON in an array, and place a string after? tellraw @a [{score:{name:"J",objective:"K"}},"+"] – Redwolf Programs Nov 9 at 16:04 • @RedwolfPrograms Not at the time of submission, four years ago. – Addison Crump Nov 10 at 4:44 ## Seriously to Python, 15 bytes ,"lambda n:n+"+ Expects input to be in string form, i.e. "3" Explanation: ,: read value from input "lambda n:n+": push this literal string +: concatenate top two values on stack  Try it online (you will have to manually enter the input because the permalinks don't like quotes) • Hey, someone actually went through with Seriously! :D – ETHproductions Nov 9 '15 at 15:26 • Seriously? You finished Seriously before Simplex? D: – Conor O'Brien Nov 9 '15 at 15:28 • @CᴏɴᴏʀO'Bʀɪᴇɴ It's not fully finished yet (see the issue tracker), but it works well enough to use in some golfs. – Mego Nov 9 '15 at 20:15 # Mathematica to C#, 22 bytes "x=>x+"<>InputString[]  Outputs a C# Func<int, int> of form x=>x+n  • The output is valid Javascript (ES6/7) as well. – Ismael Miguel Nov 10 '15 at 22:27 # rs -> K, 2 bytes /+  Live demo. # Pyth to APL, 7 5 bytes +z"--  The Pyth code simply concatenates the input (z) with the string "--". This creates an unnamed monadic train in APL with the form n--, where n comes from Pyth. When calling it in APL, (n--)x for some argument x computes n--x = n-(-x) = n+x. Try: Pyth, APL Saved 2 bytes thanks to Dennis! ## ><> to Python, 25 + 3 = 28 bytes "v+x:x adbmal o/?(3l ;>~n  Takes input via the -v flag, e.g. py -3 fish.py add.fish -v 27  and outputs a Python lambda, e.g. lambda x:x+27. For a bonus, here's an STDIN input version for 30 bytes: i:0(?v x+"r~/"lambda x: o;!?l<  # Mouse to Ruby, 19 bytes ?N:"->x{x+"N.!"}"$


Ungolfed:

? N:       ~ Read an integer from STDIN, store in N
"->x{x+"   ~ Write that string to STOUT
N. !       ~ Write N
"}"$~ Close bracket, end of program  This creates an unnamed Ruby function of the form ->x{x+n} where n comes from Mouse. • Good job, I like this post. – phase Nov 11 '15 at 23:58 # Haskell to Mathematica, 14 bytes (++"+#&").show  # Brainfuck to Java, 273 +[----->+++++.+++++.++++++.[---->++++.+[->++++.-[->+++-.-----[->+++.+++++.++++++.[---->++++.-[--->++-.[----->++-.[->+++.---------.-------------.[--->+---.+.---.----.-[->+++++-.-[--->++-.[----->+++.,[.,]+[--------->+++.-[--->+++. Outputs a method like int d(int i){return i+42;} (which doesn't look like a Java method, but... Java!) • Y'know, because Java. – Conor O'Brien Nov 11 '15 at 14:23 • What means, it doesn't look like a Java method? How else should that kind of method look? – Paŭlo Ebermann Nov 11 '15 at 17:46 • @PaŭloEbermann public static int method(int argument){return argument+42;} – phase Nov 11 '15 at 17:48 • Hmm, okay. (Though if you are programming only in public static, you are not really doing Java. argument would also better be named summand ;-). ) – Paŭlo Ebermann Nov 11 '15 at 17:50 • @PaŭloEbermann I was just modelling public static void main(String[] arguments){} – phase Nov 11 '15 at 17:51 # PHP → JavaScript (ES6), 20 24 bytes Reading from STDIN is always expensive in PHP. It looks a bit strange: x=>x+<?fgets(STDIN);  It prints x=>x+ and waits for user input to complete the string, terminates with the complete anonymous JavaScript function, e.g. x=>x+2. First version (24 bytes) <?='x=>x+'.fgets(STDIN);  • Why not just x=>x+<?=$x;? It's valid PHP4.1 and you can pass the values over POST, GET, SESSION, COOKIE, ... and it will work flawlessly. Or on PHP5.3 with register_globals=on (on your php.ini file). – Ismael Miguel Nov 10 '15 at 22:34
• @IsmaelMiguel The requirements of the challenge say that it takes a number from STDIN. Every time I ignored that it was criticized. So I take requirements seriously now. :) – insertusernamehere Nov 10 '15 at 22:39
• Well, I think that GET is passed over STDIN. I can test it in a while. – Ismael Miguel Nov 10 '15 at 22:41
• serverfault.com/questions/187025/… <-- Evidence on my claim. Just use the code I provided, slap this link and no one can complain – Ismael Miguel Nov 10 '15 at 22:47
• Ho, understood! That's clever, indeed! – Blackhole Nov 11 '15 at 14:20

# Pyth -> K, 4 bytes

+z\+


K is really easy to abuse here...

Live demo.

# Python 2 to CJam, 18 20 bytes

Thanks to LegionMammal978 for correcting the functionality.

print"{%f+}"%input()


The Python does a basic string format. %f is the code for a float, and since I wouldn't lose any bytes for handling floats, I went ahead and did so.

The CJam is much the same as the Golfscript->CJam answer. It looks something like this:

{7.4+}


or:

{23+}


It's a block that takes the top value off the stack, pushes the special number, then adds them.

• @LegionMammal978 At the end of the question it says that anonymous functions and lambda expressions are allowed. Still, I'll edit my answer with an alternative full program. – bkul Nov 13 '15 at 3:06
• The OP means that the output can be a function. – LegionMammal978 Nov 13 '15 at 12:19

read n;echo "($n+)"  Anonymous functions being allowed, Haskell is a good output choice with the operator sections. # Retina to Pip, 4 bytes Uses one file for each of these lines + 1 penalty byte; or, put both lines in a single file and use the -s flag. $
+_


Matches the end of the input with $ and puts +_ there. This results in something of the form 3+_, which is an anonymous function in Pip. # Bash → C/C++/C#/Java, 33 bytes and maybe others echo "int f(int a){return a+$1;}"


# Vitsy to K, 5 Bytes

\o/ K will be being used very soon if it can do this.

N'+'Z

or maybe...

N'+'O

If the input is taken as a string (only for 0-9 input)...

i'+'Z

All of these, for input 2, will output:

2+

## Tiny Lisp to Ceylon, 68 61

(d u(q((n)(c(q(Integer x))(c(q =>)(c(c(q x+)(c n()))()))))))


Tiny Lisp doesn't have real input and output – it just has expression evaluation. This code above creates a function and binds it to u. You can then call u with the argument n like this: (u 7), which will evaluate to this Tiny Lisp value:

((Integer x) => (x+ 7))


This is a valid Ceylon expression, for an anonymous function which adds 7 to an arbitrary integer.

Thanks to DLosc for an improvement of 7 bytes.

• Nice job working with the very limited output capabilities! – DLosc Nov 10 '15 at 22:28

# JavaScript to Lambda Calculus, 39 bytes

(This uses the linked document as a basis.)

alert((x=>λa(${x}(add a)))(prompt()))  Say input is 5. Then this becomes: "λa(5(add a))"  • Where is an interpreter for Lambda Calculus? – feersum Nov 10 '15 at 0:08 • @feersum Check the link. I'm not sure if an actual interpreter exists, but I was told I was able to submit in this language. – Conor O'Brien Nov 10 '15 at 0:10 • What do you mean "you were told"? Answers that don't run in any language implementation are invalid. – feersum Nov 10 '15 at 0:12 • – Conor O'Brien Nov 10 '15 at 0:14 • Lambda calculus being fairly well known, I assume there must be a valid interpreter somewhere. The point is, you need to identify such an interpreter and write the code in the format accepted by that interpreter. – feersum Nov 10 '15 at 0:17 # Microscript II to Javascript ES6, 9 bytes "x=>x+"pF  # GNU sed to C, 46 bytes sed -r 's/^([0-9]+)$/f(int x){return x+\1;}/'


## Ceylon to Tiny lisp, 76

shared void run(){print("(q((x)(s process.readLine()else""(s 0 x))))");}


This produces (after reading a line of input) output like (q((x)(s 5(s 0 x)))), which evaluates in Tiny Lisp to ((x) (s 5 (s 0 x))), a function which takes an argument x, subtracts it from 0, and subtracts the result from 5. (Yeah, this is how one adds in Tiny Lisp, there is only a subtraction function build in. Of course, one could define an addition function first, but this would be longer.)

You can use it like this as an anonymous function:

((q((x)(s 5(s 0 x)))) 7)


(This will evaluate to 12.)

Or you can give it a name:

(d p5 (q((x)(s 5(s 0 x)))))
(p5 7)


Corrections and Golfing Hints from DLosc, the author of Tiny Lisp.

# Javascript (ES6) > PHP/Javascript, 3848 47 bytes

This code is an anonymous function that creates a VALID PHP and Javascript function called anonymous.

P=prompt;P(Function('$x',return$x+${P()};));  As of the writting of this code, only Firefox has implemented ES6 nativelly (without flags and suchs). This code uses the prompt() function to request input and for output. Firefox supports copying the output of prompt(), making this a legitime output method for Javascript. Considerations: • The ; at the end is required for PHP • The space in return$x is required by Javascript
• Ignoring it will say that return$x is undefined • The function name is automatic: I have no choice over it • Picking only PHP or only Javascript will only reduce the code in 1 byte, and would reduce the fun-factor by half • May work with other languages Old answer (48 bytes): alert(Function('$x',return $x+${prompt()};));


y=>''+Function('$x',return$x+\${y};)