# 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. Nov 9, 2015 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. Nov 9, 2015 at 6:32
• I don't see any definition for function BUT that we are supposed to write.. -_- Nov 9, 2015 at 13:45
• When a function returns another function, it's called a closure. I don't know if this applies cross-language, though... Nov 9, 2015 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. Nov 9, 2015 at 20:40

# Fith to Forth, 28 bytes (non-competitive)

Fith was created after the challenge was posted.

": p " line " + ;" + + .


This pushes the first part, then reads the number, then pushes the last part. The two + words then concatenate the three strings, and . prints.

The output looks something like this:

: p 7 + ;


This defines a function named p, and as far as I know, it is the shortest method in Forth to define the necessary function.

# Python 3 to 05AB1E, 14 bytes

lambda n:n+'+'


Try it online! Takes a string as input.

The Python function simply returns 'n+', where n is the input. In 05AB1E, pushes implicit input, push n, add them, and implicitly output.

# Knight → Squire, 13 bytes

O+"\(x)=>x+"P


Attempt This Online!

Knighting this code will inquire thee, in which thou shalt enscribe numerals on thine keyboard. Whilst one can be a jest and use Arabic numerals, true royalties use Roman numerals, such as $$\\text{CCCXIV}\$$ (that's 314 for you jests). (It doesn't matter as it is string concatenation - Squire parses both).

Upon striking the return key, the following journey shall be proclaimed:

\(x)=>x+CCCXIV


Some foreigners might entitle this as an 'anonymous lambda' but true Squire users lack this brash peasant vocabulary.

Squire program

f=\(x)=>x+CCCXIV
# embark and proclaim
proclaim(f(X)) # CCCXXIV (314 + 10 = 414)


# 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


## 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))


# Ruby to Thue, 139 bytes

Not as short as i thought it should be, but I will post anyway. Thue may not be the best language for golfing in.

10.times{|n|puts "#{n}+::=#{n==9?'+':''}#{(n+1)%10}\n#{n}|::=|*#{n}\n>#{n}::=~#{n}"};puts "_::=:::\n||::=<\n<*::=<>\n::=\n|_#{'+'*($*[0]).to_i}|"  Takes a command-line augment in ruby, and regular input in Thue. # 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

alert(Function('$x',return$x+${prompt()};));  Old invalid answer: y=>''+Function('$x',return $x+${y};)

• @LegionMammal978 Done. Fixed it. Nov 12, 2015 at 1:13

# Python to C++, 49 bytes 39 bytes

print"[](int x){return x+%d;}"%input()


Thanks @Mego !

Basically, this prints out lambda with the argument x and adds it to the input of the Python program.

# Python to Lisp - 34 36 Bytes

print"(lambda (x) (+ x %s))"%input()


# Python3 → Go, 89 bytes

print('package main;import"fmt";func main(){a()};func a(x int)int{fmt.Println(x+'+input()+')}')


also a full program which compiles/runs with any of go build, go install or go run.

if we allow just a function (which is an error/will not compile) then 47 bytes:

print('func a(x int)int{return x+'+input()+'}')


# Go → Python (any version), 109 bytes

package main;import"fmt";func main(){var a string;_,_=fmt.Scanln(&a);fmt.Println("def a(x):\n return x+"+a)}


## Seriously to K, 4 bytes

'+,+


Everyone else is posting "to K" answers, I might as well too. Takes input in the form of a string ("3.14").

Try it online

# Go → LOLCODE, 192 bytes

package main;import."fmt";func main(){a:="";Scanln(&a);Println("HAI1.3\nHOW IZ I ADDIN YR X\nI HAS A VAR\nVAR R SUM OF X AN "+a+"\nVISIBLE VAR\nIF U SAY SO\nKTHXBYE")}


the resulting output compiles with lci.

# Java (OpenJDK 8) -> Python 3, 155 153 bytes

import java.util.Scanner;class Main{public static void main(String[] args){Scanner s=new Scanner(System.in);System.out.println("lambda x:x+"+s.next());}}


Try it online!

Not exactly a winning combination but it gave me a chance to practice using java.

Saved 2+ bytes thanks to @WheatWizard

• You could probably save bytes by writing out to a python lambda function. "lambda x:x+". Feb 21, 2017 at 0:21
• @WheatWizard thanks Feb 21, 2017 at 14:46
• You don't need spaces on the sides of your equals signs add well. Feb 21, 2017 at 14:55
• Why did you remove the target language from the title? Nov 8, 2019 at 12:31
• @PaŭloEbermann I think this was my first or second answer on this site, so I didn’t really know what I was doing. It’s fixed now. Nov 9, 2019 at 15:04

# 05AB1E to J, 4 bytes

„&+«


Try it online!

# Ruby to Myby, 14 bytes

$><<"+&"+$**""
C:\Myby>ruby -e "$><<'+&'+$**''" 3 > out  && myby -l out 3
Nibbles: 4A 03
4 nibbles, 2 bytes
6


Myby is exactly J in this case, where one can bond an argument to a dyadic verb to create a monadic verb.

"+"$ Outputs the Husk function +2 for input 2. Try the Husk function online # Husk to Nibbles, 4 bytes :'+s  Try it online! Outputs the Nibbles function +2 for input 2. Here's a Nibbles program that uses this function to add 2 to every elemnt of the input: .$ +2
.$# map over every element of the input +2 # using the function '+2'  # Charcoal to Vyxal, 2 bytes θ+  Try it online! Takes input (for example 5) on stdin. Outputs 5+, which adds 5 to the input when run as vyxal. # C (gcc) to Python, 85 57 50 bytes Borrowing @Oskar Skog's suggestions main(n){scanf("%d",&n);printf("lambda m:m+%d",n);}  # Python 3 to C (gcc), 43 bytes print('i(p){printf("%d",p+'+input()+');}')  ## Joke answers (I couldn't hold myself back) ### 1. C (gcc) to C (gcc), 66 bytes main(n){scanf("%d",&n);printf("i(p){printf('%s',p+%d);}","%d",n);} ### 2. Python 3 to Python 3, 30 bytes print(f"lambda n:n+{input()}") • This can be shortened to main(n){scanf("%d",&n);printf("def i(x):return x+%d",n);}. This is K&R C, the default data type is int and you don't need to declare int-returning functions. Aug 23, 2022 at 13:58 • You can also use lambda which makes the output shorter as well. i=lambda x:x+y is a drop-in replacement, but I think the question allows removing i= as well. Aug 23, 2022 at 14:00 # Pyth to C++, 32 bytes %"[](int x){return x+%d;}"Q  Just a Pyth version of my Python one. # Python to Pyth, 20 bytes print"L+%db"%input()  Basically, it prints out a lambda function (L) that takes in the argument (b, implicit variable) and adds the input from the Python program. • @BadCatEye fixed it.. Nov 12, 2015 at 21:43 # C# to Mathematica, 85 81 bytes using C=System.Console;class C{static void Main(){C.Write(C.ReadLine()+"+#&");}}  Outputs a Mathematica Function of form n+#&  • In the first programming language (C# in your case) you should write a full program (which reads input and produces output), not just a single function. Nov 11, 2015 at 19:21 • @PaŭloEbermann "EDIT: Anonymous functions and lambda expressions are allowed!" Nov 11, 2015 at 21:57 • This is for the created function, not the creating one ... see my comment at the question about this and the OP's answer to it (which were both later than this edit). Nov 11, 2015 at 22:00 • Save 4 bytes by replacing WriteLine with Write Feb 20, 2017 at 9:47 # Batch → K, 8 bytes echo;%1+  Fun fact: you don't have to use a space to separate echo from the text to be echo-ed, but you can pick from a whole series of different characters. For example: ., ,, :, ; and some others. # Java -> C (GCC) 144 134 bytes class m{public static void main(String[]a){System.out.print(String.format("a(b){return %s+b;}",a[0]));}}  Takes the number(string, it is supposed to be a number, no error checking) as the first argument and prints the function to stdout. # Clojure -> JavaScript, 14 bytes #(str"a=>a+"%)  An anonymous function in Clojure that returns an anonymous function in JavaScript (and potentially a few other languages). % is the implicit n. (#(str"a=>a+"%) 3) "a=>a+3"  # Haskell -> Clojure, 25 bytes \n->"#(+ % "++show n++")"  Anonymous Haskell function which takes n, and returns an anonymous Clojure function. let f = \n->"#(+ % "++show n++")" f 3 #(+ % 3)  # Keg to Vyxal, 3 bytes ᠀\+  Try it online! This takes the input at a string, pushes a plus sign, and outputs everything on the stack. It prints <input>+, which adds the input to a number in Vyxal. • Is the output a function in Vyxal, though? Aug 15, 2022 at 15:44 # dc to Haskell, 12 bytes ?[+)][(]nrnn  Try it online! Could be -1 byte if input can be taken on the stack, instead of prompting the user. ## QBIC to Jelly, 6 bytes ;?A+@+  Given n=3, this will output the Jelly code 3+. When another number is passed to this link, it will add 3 to it. Try that online! The QBIC code: ; Read cmd line param as A$
?A  PRINT A\$
+@+ plus the string literal '+'
The string literal is auto-closed by QBIC at EOF.


# sB~ -> Befunge, 7 bytes

iN?N;+


Explained:

i N 'input N
?N;+ 'print N and "+"