Sign up ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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!

share|improve this question
    
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 yesterday
    
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. – Blakusl yesterday
4  
I don't see any definition for function BUT that we are supposed to write.. -_- – Optimizer yesterday
1  
When a function returns another function, it's called a closure. I don't know if this applies cross-language, though... – ETHproductions yesterday
    
@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 yesterday

30 Answers 30

GS2 → K, 2 bytes

•+

This prints a tacit, monadic function. The source code uses the CP437 encoding.

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.

share|improve this answer
1  
Which encoding are you using? – Cᴏɴᴏʀ O'Bʀɪᴇɴ yesterday
1  
@CᴏɴᴏʀO'Bʀɪᴇɴ "The source code uses the CP437 encoding." – ETHproductions yesterday

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.

share|improve this answer
    
I'm pretty sure there's a language where you don't need the close-quote. – Thomas Kwa yesterday
    
There's a good chance you're right, but I can't recall one with implicit input and implicit output. – Dennis yesterday

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 42 on the stack, then executes +, which pops two integers from the stack and pushes their sum.

share|improve this answer
1  
I was just going to post this! – Loovjo yesterday

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.

share|improve this answer

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
share|improve this answer

O to K, 5 bytes

i'++o

Thanks to @kirbyfan64sos

share|improve this answer
    
K has automatic currying, so you can just do i'++p. – kirbyfan64sos yesterday

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
share|improve this answer
    
Hey, someone actually went through with Seriously! :D – ETHproductions yesterday
1  
Seriously? You finished Seriously before Simplex? D: – Cᴏɴᴏʀ O'Bʀɪᴇɴ yesterday
1  
@CᴏɴᴏʀO'Bʀɪᴇɴ It's not fully finished yet (see the issue tracker), but it works well enough to use in some golfs. – Mego yesterday

><> 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<
share|improve this answer

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.

share|improve this answer

Rotor to K, 2 bytes

'+

Might as well jump in on the K bandwagon.

share|improve this answer

Mathematica to C#, 20 bytes

"x=>x+"<>ToString@#&

Outputs a C# Func<int, int> of form

x=>x+n
share|improve this answer
    
The output is valid Javascript (ES6/7) as well. – Ismael Miguel 53 mins ago

rs -> K, 2 bytes

/+

Live demo.

share|improve this answer

Pyth -> K, 4 bytes

+z\+

K is really easy to abuse here...

Live demo.

share|improve this answer

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!

share|improve this answer

POSIX shell to Haskell, 19 bytes

read n;echo "($n+)"

Anonymous functions being allowed, Haskell is a good output choice with the operator sections.

share|improve this answer

Microscript II to Javascript ES6, 9 bytes

"x=>x+"pF
share|improve this answer

Python to CJam, 18 bytes

lambda n:"{%f+}"%n

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.

share|improve this answer

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);
share|improve this answer
    
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 46 mins ago
    
@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 41 mins ago
    
Well, I think that GET is passed over STDIN. I can test it in a while. – Ismael Miguel 39 mins ago
    
serverfault.com/questions/187025/… <-- Evidence on my claim. Just use the code I provided, slap this link and no one can complain – Ismael Miguel 33 mins ago

Haskell to Mathematica, 14 bytes

(++"+#&").show
share|improve this answer

GNU sed to C, 46 bytes

sed -r 's/^([0-9]+)$/f(int x){return x+\1;}/'
share|improve this answer

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.

share|improve this answer

Bash → C/C++/C#/Java, 33 bytes

and maybe others

echo "int f(int a){return a+$1;}"
share|improve this answer

Chaîne to JavaScript ES6, 7 bytes

Again, assumes numeric input.

x=>x+{i
~~~~~~~
x=>x+   ; write that text
     {  ; command sequence
      i ; read input and push to stack
        ; implicit: take top entry on stack and pushes it to writing string
        ; implicit: close }
        ; implicit: print string
share|improve this answer

JavaScript to Lambda Calculus, 21 bytes

(This uses the linked document as a basis.)

x=>`λa(${x}(add a))`

Say input is 5. Then this becomes:

"λa(5(add a))"
share|improve this answer
1  
Where is an interpreter for Lambda Calculus? – feersum 23 hours ago
    
@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. – Cᴏɴᴏʀ O'Bʀɪᴇɴ 23 hours ago
1  
What do you mean "you were told"? Answers that don't run in any language implementation are invalid. – feersum 23 hours ago
    
1  
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 23 hours ago

Tiny Lisp to Ceylon, 68

(d u(q((n)(c(q(Integer x))(c(q =>)(c(c(q x)(c(q +)(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.

share|improve this answer
    
Nice job working with the very limited output capabilities! – DLosc 52 mins ago
    
Can you do (x+ 7) in the output and save a c call? – DLosc 50 mins ago

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.

share|improve this answer

Powershell to EmacsLisp, 37 bytes

function p($a){"(defun p(x)(+ $a x)"}

EmacsLisp to Powershell, 44 bytes

(defun p(x)(format"function p($a){%s+$a}"x))
share|improve this answer

Javascript ES6 to ಠ_ಠ, 34 bytes

_=>`ಠ${[...'_1+'].join`ಠ
ಠ`}ಠ`

You can try the ಠ_ಠ interpreter here.The ಠ_ಠ program takes input and adds one to it.

share|improve this answer

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+
share|improve this answer

C# to Mathematica, 10 bytes

n=>n+"+#&"

Outputs a Mathematica Function of form

n+#&
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.