| Bytes | Lang | Time | Link |
|---|---|---|---|
| 469 | Brainfuck | 250511T172128Z | JOrE |
| 186 | Pascal | 250511T171358Z | JOrE |
| 016 | Vyxal | 220202T150715Z | Jakque |
| 089 | Python 3.8 | 210507T103912Z | Jakque |
| 096 | Ruby | 200816T135626Z | Dingus |
| 156 | AWK | 210611T212616Z | Pedro Ma |
| 043 | Japt | 210507T114015Z | Etheryte |
| nan | Pxem esolangbox notation | 210505T061343Z | user1004 |
| 086 | Python 2 | 210409T064125Z | dingledo |
| nan | Wolfram Language Mathematica | 200803T213534Z | att |
| 028 | Klein 001 | 201112T194702Z | Wheat Wi |
| 140 | Haskell | 200805T212319Z | Silvio M |
| 127 | Haskell | 201108T163113Z | Wheat Wi |
| 026 | Keg | 200804T005725Z | lyxal |
| nan | Java 10+ | 200816T232449Z | user |
| nan | R | 200803T172156Z | Dominic |
| 121 | Python 3 | 200814T003936Z | Quelklef |
| 125 | R | 200807T085840Z | Dominic |
| 025 | Quine + Cat = Hello World | 200803T165429Z | fireflam |
| 017 | Jelly | 200805T214437Z | Jonathan |
| 117 | PHP | 200806T130130Z | RFSnake |
| 032 | V vim | 200805T184654Z | Beefster |
| 189 | Python 2 | 200803T200634Z | Beefster |
| 048 | ><> | 200804T215427Z | SE - sto |
| 023 | 05AB1E | 200804T100829Z | Kevin Cr |
| nan | Hello World + Quine = Cat | 200804T203000Z | rtpax |
| 051 | Alice | 200804T193144Z | Oyarsa |
| nan | Rust | 200803T194547Z | TehPers |
| nan | Aceto | 200804T121909Z | L3viatha |
| 129 | Python 3 | 200803T201447Z | water_gh |
| 110 | Bash | 200803T215549Z | Beefster |
Brainfuck, Hello World + Quine = Cat, 469 bytes
Hello World (Requires dynamic memory)
+[-->-[>>+>-----<<]<--<---]>-.>>>+.>>..+++[.>]<<<<.+++.------.<<-.>>>>+.
Quine:
->++>+++>+>+>+++>>>>>>>>>>>>>>>>>>>>+>+>++>+++>++>>+++>+>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+>+>>+++>>+++>>>>>+++>+>>>>>>>>>++>+++>+++>+>>+++>>>+++>+>++>+++>>>+>+>++>+++>+>+>>+++>>>>>>>+>+>>>+>+>++>+++>+++>+>>+++>>>+++>+>++>+++>++>>+>+>++>+++>+>+>>+++>>>>>+++>+>>>>>++>+++>+++>+>>+++>>>+++>+>+++>+>>+++>>+++>>++[[>>+[>]++>++[<]<-]>+[>]<+<+++[<]<+]>+[>]++++>++[[<++++++++++++++++>-]<+++++++++.<]
Cat Program:
+[,.]
Note: It needs a null at end of input, or the last character will repeated infinite times.
Pascal, Cat + Quine = Hello World, 186 Bytes
Cat Program:
var s:string;begin read(s);write(s)end.
Quine:
CONST T=';BEGIN WRITE(#67#79#78#83#84#32#84#61#39,T,#39,T)END.';BEGIN WRITE(#67#79#78#83#84#32#84#61#39,T,#39,T)END.
Hello World:
begin write("Hello World!")end.
Vyxal, Cat + Quine = Hello, 16 bytes
Hello world, 13 bytes
kh\!+□⁋`I^`I^
Cat, 7 bytes
kh\!+□⁋
Try it Online!qwhYOKWoeKBiyIsIiIsImFcbmJcbmNcbmRcbiJd)
Quine, 6 bytes
`I^`I^
Explanation :
## Cat (10)
kh # push 'Hello World' onto the stack
\!+ # add '!' to the string
□ # push the list of the inputs onto the stack
⁋ # join the element on top of the stack, with '\n' as separator
# implicit output
## Quine (6)
`I^` # push 'I^' on the stack
I # string -> `string`string
^ # reverse the stack
# implicit output
## Hello (16)
kh\!+ # push `Hello World!` onto the stack
□ # | do
⁋ # | many
`I^` # | useless
I # | things
^ # reverse the stack
# implicit output
Python 3.8, Cat + Quine = Hello, 101 91 89 bytes
Cat (32 bytes)
a=id=""
while 1:a=input(a)+"\n";
Quine (57 bytes)
exec(s:='+input(id and"exec(s:=%r)"%s or"Hello World!")')
Hello World (89 bytes)
a=id=""
while 1:a=input(a)+"\n";exec(s:='+input(id and"exec(s:=%r)"%s or"Hello World!")')
How it works:
For the cat program:
a=id=0we assignaandidto the empty string.idis now evaluated asFalse(remember it for later)- At the first loop of
while 1:a=input(a)+"\n"will displayawithout a new line (so the empty string) and store inathe first line of the input plus a newline - Then we loop again, displaying the input of the previous iteration until the program has written all the lines of the input and crash (
EOFError)
For the quine program:
- The main structure of the quine is
exec(s:='print("exec(s:=%r)"%s)'), a well known quine in python. - We uses
input()instead ofprint()to avoid the trailing new line - the
+at the start is here to make the program crash as soon as it finished printing to make sure the program ends. idis a python built-in. Because of that it is evaluated asTruesoid and <code 1> or <code2>will be equal to<code 1>(which contains the code for our quine to work).
For the hello world program:
- remember the cat program where we assigned
idto the empty string? id and <code 1> or <code2>will now execute<code2>sinceidis now evaluated asFalse, resulting the printing ofHello World!- The
+crashes the program avoiding looping in the cat code again
Old solution: cat (37) + quine (64) = hello (101 bytes)
Here is my old solution using triple quoted string and comments to "squeeze" the quine part and directly printing "Hello World!". It uses some cool tricks with comments:
while p:=print:id or p(input());id="" # cat (37 bytes)
"";exec(s:='input(\'"";exec(s:=%r)\'%s)#"""-p("Hello World!")#') # quine (64 bytes)
while p:=print:id or p(input());id="""";exec(s:='input(\'"";exec(s:=%r)\'%s)#"""-p("Hello World!")#') # hello (101 bytes)
Who said that comments were not usefull in codegolfing?
Ruby, Cat + Quine = Hello World!, 100 97 96 bytes
Reading the source code is prohibited for quines but there is no such rule for Hello World! programs. We exploit this fact using Ruby's DATA/__END__ mechanism. If __END__ appears alone on any line in the code, execution terminates there. However, any further code is accessible via the constant DATA, which is initialised to a File object containing all of this non-executable 'data'.
Cat
$><<(DATA||=$<).read;a
The idiomatic ||= operator sets the value of the variable DATA only if it is not already defined. In this case, DATA is not defined because the program does not contain __END__. In effect, the first part of the code therefore reduces to $><<$<.read, where $< and $> point to STDIN and STDOUT, respectively. For later use, the final a (which is an undefined variable) throws an error, which is inconsequential here.
Quine
eval$s=%q($><<"eval$s=%q(#$s)
__END__
Hello World!")
__END__
Hello World!
Try it online! or verify quine.
All of the real work is done in the first line, which is a basic Ruby quine template. With __END__ now making an appearance, it shouldn't be too hard to see where this is going.
Hello World!
$><<(DATA||=$<).read;a
eval$s=%q($><<"eval$s=%q(#$s)
__END__
Hello World!")
__END__
Hello World!
Finally we have DATA and __END__ together. Unlike in the cat program, DATA is defined this time: it's a File object containing Hello World!. Once this has been printed, there is no further output because of the error thrown by the final a (undefined) in the first line.
AWK, Cat + HW = Quine, 156 bytes
BEGIN{k=1}1;BEGIN{if(k){printf a="BEGIN{k=1}1;BEGIN{if(k){printf a=%c%s%c,34,a,34,34,34;exit}print%cHello World!%c}",34,a,34,34,34;exit}print"Hello World!"}
This is the overall algorithm:
BEGIN{k=1}1;
BEGIN{if(k){
print"Quine";exit
}
print"Hello, World!"
}
Cat
BEGIN{k=1}1;
Hello World
BEGIN{if(k){printf a="BEGIN{k=1}1;BEGIN{if(k){printf a=%c%s%c,34,a,34,34,34;exit}print%cHello World!%c}",34,a,34,34,34;exit}print"Hello World!"}
Japt, Quine + Cat = Hello World, 43 bytes
All of the complexity is jammed into making a quine that includes a variable that holds Hello World!. Cat simply outputs the default input and the Hello World merger reuses the variable from the quine.
Quine, 41 bytes
"Hello World!" // U = "Hello World!"
"iQ ²iRiQiUiQ"iQ ²iRiQiUiQ // A bunch of messy string manipulations to make this a quine
Cat, 2 bytes
U // Output the default first input
Hello World, 43 bytes
"Hello World!" // U = "Hello World!"
"iQ ²iRiQiUiQ"iQ ²iRiQiUiQ // Throwaway line
U // Output U
Pxem (esolang-box notation), Hello World: 48 bytes + Quine: 110 bytes = Cat: 158 bytes.
Unprintables are escaped.
Hello World
.f!.zHello World.p.d.a.w.c.z.w.o.i.c\001.+.a.d.a.a
Quine
!Be!?BzBc!!B+BzXXFBaBcFBzBsXXFBBaBsBtBvBmBvBcAcB-BaBsBp".e!?.z.c!!.+.zXXF.a.cF.z.sXXF..a.s.t.v.m.v.cAc.-.a.s.p
Cat
.f!.zHello World.p.d.a.w.c.z.w.o.i.c\001.+.a.d.a.a
!Be!?BzBc!!B+BzXXFBaBcFBzBsXXFBBaBsBtBvBmBvBcAcB-BaBsBp".e!?.z.c!!.+.zXXF.a.cF.z.sXXF..a.s.t.v.m.v.cAc.-.a.s.p
Try it online!
Use variable mode to switch programs.
How it works
The Hello World program consists of several parts, as in:
# branch if content begins with '!' or not
.f!.z
# Hello World program
# Note that '!' is recycled here
Hello World.p.d
.a
.w.c.z
# Cat program
# Note that content of stack is cleared before entrying here
.w.o.i.c\001.+.a.d
.a.a\n
Quine is recycled from here.
Cat program uses this specification:
The first line is the file name of the pxem code.
The rest is the content of the pxem code.
Python 2, Quine + Cat = Hello World, 86 bytes
Quine (35 bytes)
id='id=%r;print id%%id';print id%id
Cat (51 bytes)
+0*''and'Hello World!'
while''>id:print raw_input()
Hello World (86 bytes)
id='id=%r;print id%%id';print id%id+0*''and'Hello World!'
while''>id:print raw_input()
Wolfram Language (Mathematica), Quine + Cat = Hello World, 15+28=43 15 + 22 = 37 bytes
3 functions.
Quine
ToString[#0] &
Cat
#/.#_:>"Hello World!"&
Hello World
ToString[#0] & #/.#_:>"Hello World!"&
HW+Q=C,46+58=104 Q+C=HW,43+48=91 Cat + Quine = Hello World, 49 + 37 = 86 bytes
Cat
Print[#/._@_->"Hello World!"]&@$ScriptInputString
Quine
Print[ToString[#0, InputForm][]] & []
Hello World
Print[#/._@_->"Hello World!"]&@$ScriptInputStringPrint[ToString[#0, InputForm][]] & []
Klein 001, Quine + Cat = Hello World, 28 bytes
Quine, 11 bytes
This is Martin Ender's quine from here
:?\:2+@> "
Cat, 17 bytes
@ >"Hello world!"
Hello World, 28 bytes
:?\:2+@> "
@ >"Hello world!"
Haskell, Cat + Quine = Hello World, 140 bytes
Cat
b=interact id
main=b where c=1;
Quine
main=putStr a>>print a;b=putStrLn "Hello world!";a="main=putStrLn a>>print a;b=putStrLn \"Hello world!\";a="
Hello World!
b=interact id
main=b where c=1;main=putStr a>>print a;b=putStrLn "Hello world!";a="main=putStrLn a>>print a;b=putStrLn \"Hello world!\";a="
We exploit the rules of variable shadowing. The cat program simply calls the global b, defined as interact id (a standard cat in Haskell). We declare a variable c that's never used, simply so we can concatenate later. The quine is pretty standard; we define a variable b that we never use, but otherwise it simply prints its payload and exits.
Here's a version of "Hello world" with better spacing.
b = interact id
main = b
where c=1
main=putStr a>>print a
b=putStrLn "Hello world!"
a="main=putStrLn a>>print a;b=putStrLn \"Hello world!\";a="
main simply calls b, but this time is calls the locally-declared b, which prints "Hello world!". All of the other variables are unused.
Haskell, Cat + Quine = Hello World, 127 bytes
Cat, 27 bytes
o=interact id
main=o where
Quine, 100 bytes
o=putStr"Hello, world!";main=putStr$(++)<*>show$"o=putStr\"Hello, world!\";main=putStr$(++)<*>show$"
Hello World
o=interact id
main=o where o=putStr"Hello, world!";main=putStr$(++)<*>show$"o=putStr\"Hello, world!\";main=putStr$(++)<*>show$"
Explanation
The main trick in this answer is scoping. In the cat we have a simple program. We have a main which is just defined to be o, which is a cat program. At the end of the program there is a where which opens a new scope. This will make our entire quine program attached to the scope of the main program. Two things are solved by doing this.
Since the quine program's
mainis not defined in the global scope we avoid the duplicate main declaration error.We can declare a version of
othat overrides the one in the global scope. Our quine program declaresoto be a hello world program.
The overall technique is very similar to Silvio Mayolo's answer, however improves upon it in two ways.
Silvio Mayolo's answer declares a variable
c=1, which is never used (I am not sure why this is done. Removing it doesn't break anything). This does not have this.I used a shorter quine technique. I took a glance at Silvio Mayolo's quine and I don't know how it works, but it is longer than mine.
Keg, Hello World + Quine = Cat, 26 bytes
«H%c¡|,!«``:[④|᠀,]`:[④|᠀,]
How it Works
Hello World
«H%c¡|,!«`
This is my answer to the HW challenge with some additional string closing syntax. Why? Because a) the main string needs closing to be concatenated and b) the end ``` is needed to "ignore" the quine part
Quine (non-trivial)
`:[④|᠀,]`:[④|᠀,]
`:[④|᠀,]`
Push the string :[④|᠀,] to the stack
:[④|᠀,]
Duplicate the string and start an if-block. The if block uses the truthiness of the t.o.s to determine which branch is to be executed. In this case, the string is truthy, so the ④ is executed (printing the string raw). Implicit output then prints the string nicely.
Concatenation
«H%c¡|,!«``
Push the string Hello, World! followed by an empty string onto the stack.
:[④|᠀,]
Duplicate the top of the stack (an empty string) and start the if block. Empty strings are considered falsey, so the ᠀, branch is executed. This takes input and prints it.
`:[④|᠀,]
Push the string :[④|᠀,] and don't do anything with it.
Java 10+, Cat + Quine = Hello World, 384 (135 + 249) bytes
Cat, 135 bytes
interface C{static void main(String[]a){System.out.println(Boolean.TRUE?new java.util.Scanner(System.in).nextLine():"Hello World");}}//
Quine, 249 bytes
(note: TIO doesn't let me run the code unless I rename my interface from Q to Main, but just know that it's supposed to be named the former)
interface Q{static void main(String[]a){var s="interface Q{static void main(String[]a){var s=%c%s%c;System.out.printf(s,34,s,34,10);}}%cinterface Boolean{boolean TRUE=false;}";System.out.printf(s,34,s,34,10);}}
interface Boolean{boolean TRUE=false;}
Hello World, 384 bytes
interface C{static void main(String[]a){System.out.println(Boolean.TRUE?new java.util.Scanner(System.in).nextLine():"Hello World");}}//interface Q{static void main(String[]a){var s="interface Q{static void main(String[]a){var s=%c%s%c;System.out.printf(s,34,s,34,10);}}%cinterface Boolean{boolean TRUE=false;}";System.out.printf(s,34,s,34,10);}}
interface Boolean{boolean TRUE=false;}
Again, the TIO link contains an interface called Main, but it's actually C, the cat program.
It redefines Boolean.TRUE to be false when the quine is concatenated to the cat.
R, Quine + hello, world = cat; 48 + 49 ... 43 + 44 = 87 bytes
Or 75 bytes as internal code of functions and not including function(){} wrappers.
Edit: -14 bytes thanks to Robin Ryder!
Nontrivial quine:
'->T;cat(sQuote(T),T)' ->T;cat(sQuote(T),T)
Hello,world:
~F->y;cat(`if`(T>0,"Hello world!",scan(,T)))
Cat:
'->T;cat(sQuote(T),T)' ->T;cat(sQuote(T),T)~F->y;cat(`if`(T>0,"Hello world!",scan(,T)))
A 'trivial quine' version could be Quine = ~1, and Hello, world = +F->y;cat(`if`(y<0,scan(,''),'Hello world!')), for 2+45=47 bytes.
How? (nontrivial & trivial versions)
The default behaviour of R is to output any unassigned values (such as variables or expressions). So, to print a quine, we simply need to generate an expression containing the program code, and it is output by default (this applies both to the nontrivial quine, which is contructed using cat to join the various text elements together, as well as the trivial quine ~1 consisting simply of a formula which is outputted)
If a value is assigned to a variable, it is not output. So to stop the quines from printing, we incorporate them into an expression and assign this to the variable y.
To do this, we need to use a binary operator, but since this operator will also appear at the start of the 'Hello, world' program, it must also function as a unary operator. Both the ~ (formula) and + (positive/sum) operators have this property.
Conveniently, R also includes a (little used outside coding challenges) left-to-right assignment operator, ->, which - together with a unary/binary operator - lets us package the quine into the variable y & forget about it. Then all we need to do is to determine whether this has happened or not, and use this to switch between 'Hello, world' and 'cat' behaviour.
Python 3, Cat + Quine = Hello World, 121 bytes
- -2 bytes thanks to @Jo King
Cat:
Actual cat part is taken from the top comment of this SO answer.
If the file is long enough, switch to a Hello World program.
len(open(__file__).read())<99or~print('Hello World!')
import sys
print(sys.stdin.read())
The ~print exits the program after printing: print returns None and ~None throws. (Crashing to exit was allowed by OP in a comment.)
Quine:
Pretty standard. Originally wanted to use Python 3.8 := to do print((s:='print((s:=%r)%%s)')%s), but that was longer. Stole the use of ; instead of \n from one of the other Python answers.
s='s=%r;print(s%%s)';print(s%s)
len(open(__file__).read())<99or~print('Hello World!')
import sys
print(sys.stdin.read())
s='s=%r;print(s%%s)';print(s%s)
R, Quine (74 51 bytes) + Cat (77 74 bytes) = Hello world 151 125 bytes
R, Hello world (173 174 bytes) + Cat (77 74 bytes) = Quine 250 248 bytes
R, Quine (74 51 bytes) + Hello world (173 174 bytes) = Cat 247 225 bytes
A set of Quine, Cat & Hello world from which any 2 can be combined to form the third.
Not the shortest answer, but pleasingly symmetric.
Quine (74 51 bytes)
'->F;T=0;cat(sQuote(F),F)' ->F;T=0;cat(sQuote(F),F)
Cat (77 74 bytes)
~1->y;cat(`if`(T>1,sprintf(T,sQuote(T)),`if`(T,scan(,""),"Hello world!")))
Hello world (173 174 bytes)
~1->y;T='~1->y;T=%s;cat(`if`(F<0,scan(,""),"Hello world!"))~1->y;cat(`if`(T>1,sprintf(T,sQuote(T)),`if`(T,scan(,""),"Hello world!")))';cat(`if`(F<0,scan(,""),"Hello world!"))
Quine + Cat = Hello World, Jelly, 25 bytes
-2 bytes thanks to @Jonathan Allan
Quine (12 bytes)
“Ṿṭ⁷;⁾v`”v`
(starts with a newline)
Cat (13 bytes)
Ṇ
“,ḷṅḳȦ»³ÑƑ?
Try it online! (argument quoted to avoid casting to a Python object from string as per @Jonathan Allan's suggestion)
Hello World (25 bytes)
“Ṿṭ⁷;⁾v`”v`Ṇ
“,ḷṅḳȦ»³ÑƑ?
(starts with a newline)
How it Works
In Jelly, the last link (last line) is always executed as the main link. The Cat and Hello World have the same last link, so they are differentiated by the value of the first link (blank (identity) or Ṇ (logical not)).
“,ḷṅḳȦ»³ÑƑ?
? # If
ÑƑ # The first link is the identity
“,ḷṅḳȦ» # Return "Hello World!" (String Compressor: https://codegolf.stackexchange.com/a/151721/68261)
# Else
³ # Return the input
The quine is slightly difficult because it needs to prepend a blank line.
“Ṿṭ⁷;⁾v`”v`
“Ṿṭ⁷;⁾v`” # Set the string "Ṿṭ⁷;⁾v`"
v` # Eval it on itself:
Ṿṭ⁷;⁾v`
Ṿ # Uneval: "“Ṿṭ⁷;⁾v`”"
ṭ⁷ # Prepend a newline "¶“Ṿṭ⁷;⁾v`”"
;⁾v` # Concatenate "v`" to get "¶“Ṿṭ⁷;⁾v`”v`"
Jelly, Quine + Cat = Hello World! 17 bytes
Note that using a formatted input has been deemed valid and this entry takes input as a command-line argument formatted as a Python string. To have a pure-Cat program we'd need to use STDIN in Jelly, since it first attempts to evaluate any command-line argument as Python. This is achievable in 21 bytes with ”ṘṘ + ”1$0¡ƈȮ¤L¿“,ḷṅḳȦ»Ṇ? TIO.
”ṘṘ
1$0¡³“,ḷṅḳȦ»⁼?
”ṘṘ1$0¡³“,ḷṅḳȦ»⁼?0
How?
The shortest proper quine in Jelly is:
”ṘṘ - Main Link: any arguments
”Ṙ - an 'Ṙ' character
Ṙ - print Jelly representation of x (i.e. ”Ṙ) and yield x ('Ṙ')
- implicit print (i.e. Ṙ)
To use it we need to not let the Ṙ execute in the largest program.
One way to not execute a link is to follow it with 0¡ - repeat zero times, but ¡ needs a link to repeat, like X0¡, so we make X equal 1$.
$ composes the preceding two links into a single monadic link and (slightly surprisingly) 1$ can start a full program, as a monad which yields \$1\$ but when repeated zero times it just yields whatever its left argument is.
As such starting a program which has one command-line argument with 1$0¡ applies 1$ zero times to that argument, i.e. is a no-op, giving the rest of the program that same left argument.
But when 1$0¡ is prefixed with ”ṘṘ we have the X (described earlier) equal to Ṙ1 which when applied zero times to ”Ṙ yields the character 'Ṙ'.
Since the character, 'Ṙ', is not equal to the right argument of the Main Link (which, when given a single command-line argument is that argument) since that is a list of characters, we can use equality, ⁼, to test, ?, (effectively) whether the prefix ”ṘṘ is present and either...
...Cat* (if not):
³ - yield the programs 1st command-line argument
...or Hello World!:
“,ḷṅḳȦ» - compressed string = "Hello World!"
* The Cat code for the 21 byte STDIN version is:
ƈȮ¤L¿ - niladic link (no arguments)
¿ - do...
¤ - ...instruction: nilad followed by link(s) as a nilad
ƈ - read a character from STDIN
Ȯ - print & yield
L - ...while?: length (0 at EOF)
and we use the monad logical-NOT, Ṇ, as our test since we get an implicit left argument of 0 with no command-line arguments and by this point Ṇ gets an argument of 0 (0Ṇ = \$1\$ -> Cat) or 'Ṙ' (”ṘṆ = \$0\$ -> Hello World!).
PHP, Hello World + Quine = Cat, 117 bytes
Because of the input method this only works using the command line.
Hello world
the double die is because the php code has to get interupted earlier in order to prevent errors being printed (missing function a)
<?php if(!function_exists('a')){die('Hello world!');}die(a($argv));
Quine
without opening tag, php just outputs whatever it contains
function a($b){unset($b[0]);echo implode(' ',$b);}
Cat
Because function declations are passed first, the die() isn't called yet and therefor a() exists, and is called in order to print its arguments. The unset avoids the scriptname from being printed (which is not an input)
<?php if(!function_exists('a')){die('Hello world!');}die(a($argv));function a($b){unset($b[0]);echo implode(' ',$b);}
If only the first argument has to be printed, a shortcut can be used (101 bytes):
<?php if(!function_exists('a')){die('Hello world!');}die(a($argv));function a($b){unset($b[0]);echo implode(' ',$b);}
This however is not the full input and i consider this invalid
V (vim), Quine + Hello World = Cat, 32 bytes
Quine
dG2idG2i
Explanation:
dG: Delete the buffer (saving it to the unnamed register)2idG2i: InsertdG2itwice.
Hello World
"_dG4iHello World!
␛pH3D
With trailing newline. TryItOnline also shows a trailing space after that, but this appears to be an artifact of it's V runner.
Explanation:
"_dG: Delete the buffer (without saving it to a register)4iHello World!␊␛: Write "Hello World!" 4 timesp: Paste from the (empty) unnamed registerH3D: Delete the first 3 lines of the buffer
Cat
dG2idG2i"_dG4iHello World!
␛pH3D
Since all no-ops in V are automatically cat programs, the trick here is to make the combined program cancel out itself.
Explanation:
dG: Delete the buffer (saving it to the unnamed register)2idG2i"_dG4iHello World!␊␛: WritedG2i"_dG4iHello World!twice (trailing newline)p: Paste from the unnamed register.- Since the motion used to delete was
G, this pastes it onto the following line. - Therefore, the first two lines of the buffer are that crazy string, the third is an empty line, and the rest is the original buffer
- Since the motion used to delete was
H3D: Delete the first 3 lines of the buffer
Python 2, Hello World + Cat = Quine, 200 198 189 bytes
Hello World
id=0;a="Hello World!";a0='id=0;a="%s";a0=%r;print a0%%((a,a0)if id<1else 1)\nimport sys\nif id:print sys.stdin.read()';print a
Cat
0%((a,a0)if id<1else 1)
import sys
if id:print sys.stdin.read()
My previous answer was actually wrong. raw_input only reads one line. This reads the entire input.
Quine
id=0;a="Hello World!";a0='id=0;a="%s";a0=%r;print a0%%((a,a0)if id<1else 1)\nimport sys\nif id:print sys.stdin.read()';print a0%((a,a0)if id<1else 1)
import sys
if id:print sys.stdin.read()
2020-08-05: -42 bytes thanks to Jonathan Allen, +33 to fix a bug
><>, Quine + Cat = Hello World!, 48 bytes
"r00gol?!;40.
The classic ><> quine
"]v"i:0(?;o
>l?!;o
^"Hello World!"<
A simple cat program, loaded with some other code that isn't being run.
"r00gol?!;40."]v"i:0(?;o
>l?!;o
^"Hello World!"<
The quine part makes the instruction pointer stop interpreting "]v" as text, instead clearing the stack and moving down to the "Hello World!" printer.
Equivalently, the program can be written as
'rd3*ol?!;40.']v'i:0(?;o
>l?!;o
^"Hello World!"<
Which, as Jo King points out, avoids using the g code reflection instruction, arguably making the quine more genuine.
05AB1E, Quine + Hello World = Cat, 23 bytes
Quine:
2096239D20BJ
Try it online (with input) or try it online (without input).
Hello World:
I.gi”Ÿ™‚ï!
Try it online (with input) or try it online (without input).
Cat:
2096239D20BJI.gi”Ÿ™‚ï!
Try it online (with input) or try it online (without input).
(All three output with trailing newline.)
Explanation:
2096239 # Push integer 2096239
D # Duplicate it
20B # Convert it to base-20 as list: "D20BJ"
J # Join stack together: "2096239D20BJ"
# (after which it is output implicitly as result)
I # Push the input (or an empty string if none is given)
.g # Get the amount of items on the stack (which will be 1)
i # If this amount is 1 (which it always is):
”Ÿ™‚ï! # Push dictionary string "Hello World!"
# (after which it is output implicitly as result)
2096239D20BJ # Same as above
I # Push the input (or an empty string if none is given)
.g # Get the amount of items on the stack: 2
i # If this amount is 1 (which it isn't):
”Ÿ™‚ï! # Push dictionary string "Hello World!"
# (implicit else:)
# (implicitly output the input we pushed earlier as result)
See this 05AB1E tip of mine (section How to use the dictionary?) to understand why ”Ÿ™‚ï! is "Hello World!".
Credit of the quine goes to @Grimmy's answer here.
Hello World + Quine = Cat, C (GCC), 149 (81 + 68)
Hello World
a;main(s){a?read(0,&s,1)&&main(putchar(s)):puts("Hello World!");}
#define main m
Quine
a=1;main(s){printf(s="a=1;main(s){printf(s=%c%s%1$c,34,s);}",34,s);}
Cat (Hello World + Quine)
a;main(s){a?read(0,&s,1)&&main(putchar(s)):puts("Hello World!");}
#define main m
a=1;main(s){printf(s="a=1;main(s){printf(s=%c%s%1$c,34,s);}",34,s);}
Alice, Cat + Quine = Hello World, 51 bytes
Cat: (With trailing newline)
\ > "!dlroW olleH"d&O@
^/ v
# < i
Uses # to skip the redirect west and instead hit the redirect south into the i, which pushes the input as a string to the top of the stack. The instruction pointer then reflects off the top and bottom boundaries of the grid, hitting the o and @ from the Hello World program, causing it to output the top of the stack as a string and then terminate. The code requires a trailing newline, which I couldn't get to display here in the code block.
Quine:
"!<@O&9h.
Just a standard Alice quine.
Hello World:
\ > "!dlroW olleH"d&O@
^/ v
# < i
"!<@O&9h.
The # is now used to skip the @ from the quine program, causing the instruction pointer to instead hit the redirect west, which passes through a mirror and hits two more redirects to hit a standard Alice Hello World program.
Rust, Quine + Cat = Hello, world! (106 + 2 = 108 bytes)
Quine (108 106 bytes):
-2 bytes: removed ',' from "Hello world!"
let s=format!("Hello world!");format!("{},{0:?})","let s=format!(\"Hello world!\");format!(\"{},{0:?})\"")
.into() instead of format! saves a few bytes but is context-dependent.
Cat (2 bytes):
;s
Quine + Cat = Hello, world! (110 108 bytes):
let s=format!("Hello world!");format!("{},{0:?})","let s=format!(\"Hello world!\");format!(\"{},{0:?})\"");s
Updated to not use include_str!. Hopefully this doesn't break any rules anymore.
This relies on it being in a closure/function that implements Fn(String) -> String with argument s.
Old answer, uses include_str!:
Quine (67 bytes):
match include_str!("f"){p@_ if p.len()==67=>p,_=>"Hello, world!"}//
(Not very creative, unfortunately)
Cat (1 byte):
s
Quine + Cat = Hello, world! (68 bytes):
match include_str!("f"){p@_ if p.len()==67=>p,_=>"Hello, world!"}//s
Try it! (Repl.it link due to multiple files)
This depends on the code being in its own file named "f" and being include!'d into main.rs before being executed. The Repl.it link has the programs in separate files with different names, which means that the quine and hello world programs are different by one character so that they include the correct string.
This challenge was especially difficult in Rust (without using a comment at the end of one of the programs) because of the syntax of the language. Functions and multi-statement closures have braces surrounding them, so you can't just concat two closures to get a third, unfortunately.
Aceto, quine (67) + cat (33) = Hello World (100 bytes*)
(*I counted one file including a final newline so that catting them together works as expected)
quine (made it for this challenge):
£"24«cs%55«3+cp24«2+cdpsdpsppn"24«cs%55«3+cp24«2+cdpsdpsppn
cat:
X
n
p el
r"HlX
^^ oldnp
^Wor!"
The quine itself was the hardest part, due to the nature of having code on a Hilbert curve (The "Hello World", and cat programs are trivial compared to it). The solution of having the concatenated program do something else than the parts is simple in Aceto: Because the (longer-line) quine enforces a square size of an even power of two (64 = 2^6), and the cat program has, on its own, a square of size 8x8 (8 = 2^3, an odd power of two), the instruction pointer starts moving in a different direction.
Python 3, Hello World + Quine = Cat, 129 bytes
Hello World:
import os,atexit as a
p=print
a.register(p,"Hello World!")
def print(_):
p(input())
os._exit(0)
Quine:
s='s=%r;print(s%%s)';print(s%s)
Cat:
import os,atexit as a
p=print
a.register(p,"Hello World!")
def print(_):
p(input())
os._exit(0)
s='s=%r;print(s%%s)';print(s%s)
atexit lets you define cleanup steps that will run when your program exits "normally". In this case, I register the print function (renamed p) with the argument "Hello World!", so it will print that string when the program ends.
I then redefine print to become a cat function. Normally, this would cause the program to print its input and "Hello World!", but os._exit() is an "abnormal" exit that bypasses the cleanup steps.
Now that print has been redefined, the Quine simply calls this cat function and the program abruptly exits. If the Quine doesn't exist, then the program exits normally, printing "Hello World!" in the process.
The final program doesn't work on TIO, but it works for me running Python 3.7.3 on MacOS.
Bash, Quine + Cat = Hello World, 110 bytes
Quine
q='eval printf "$s" "$s"';s='q=\47eval printf "$s" "$s"\47;s=\47%s\47;$q';$q
Cat
true false&&cat||echo Hello World!
Hello World
q='eval printf "$s" "$s"';s='q=\47eval printf "$s" "$s"\47;s=\47%s\47;$q';$qtrue false&&cat||echo Hello World!
This takes advantage of the fact that undefined variables expand to the empty string and that true is a command that can take arguments.
You can trivially swap the cat and the hello world by swapping true and false