56
\$\begingroup\$

yes, from coreutils, is 91 lines long. Many of them are comments, but that is still WAY too long.

Edit from September 2019: the source file grew over the past five years and is now 126 lines long.

Write a program that imitates yes:

  • outputting to stdout an infinite stream of "y\n"'s
  • there must be an option to stop it other than killing the process with SIGKILL: but SIGINT and SIGPIPE are fine
  • you are not allowed to use "y" or "\n" or their ASCII values (121, 0x79, 0171, 10, 0xA or 012)

Shortest answer wins.

Bonus:

  • subtract 10 from your code length, if you can receive a phrase in stdin and print it out instead of "y" (but still including the line-break). The program doesn't have to print y on an empty input.
\$\endgroup\$
28
  • 14
    \$\begingroup\$ "you are not allowed to use "y" or "\n"" -- should I read this as "You may not use y or \n inside of a string literal"? \$\endgroup\$
    – apsillers
    Dec 17, 2014 at 14:28
  • 16
    \$\begingroup\$ On a related note, GNU true.c is 80 lines long. \$\endgroup\$ Dec 17, 2014 at 22:32
  • 10
    \$\begingroup\$ @DennisWilliamson On a similarly related note, false.c is 2 lines long.... ;_; \$\endgroup\$
    – LordAro
    Dec 18, 2014 at 2:30
  • 8
    \$\begingroup\$ the coreutils yes takes an optional argument on the command line, not stdin. \$\endgroup\$ Dec 18, 2014 at 20:05
  • 10
    \$\begingroup\$ @MrLore: to pipe into other programs that might constantly ask for confirmation of the various things they are doing, so you dont have to sit there typing the ys yourself. \$\endgroup\$ Dec 20, 2014 at 16:57

92 Answers 92

2
\$\begingroup\$

W s, 1-10 = -9 bytes

I

Explanation

Flag: s % We have to read from STDIN :(
 P      % Newline-print
a       % the STDIN input
  I     % Forever

W, 7 bytes

i12 1CE

Explanation

i     E % Foreach from 1 to positive infinity:
         % (Implicit print a newline on each iteration)
 12 1    % Space does nothing. Basically just pushes 121.
     C  % Convert to a character, rendering 'y'
\$\endgroup\$
4
  • \$\begingroup\$ Never thought I'd see a negative score. \$\endgroup\$
    – PkmnQ
    Mar 30, 2020 at 7:39
  • \$\begingroup\$ @PkmnQ I'm going to make this score -9. \$\endgroup\$
    – user92069
    Apr 1, 2020 at 9:51
  • \$\begingroup\$ Ties your other answer in Husk. \$\endgroup\$
    – PkmnQ
    Apr 2, 2020 at 2:54
  • \$\begingroup\$ @a'_' You're not allowed to use 12 1: "Not in a literal, not as a constant, not as a variable name. Nothing." \$\endgroup\$
    – S.S. Anne
    Apr 4, 2020 at 2:31
2
\$\begingroup\$

Zsh + POSIX utilities, 22 bytes

f()(tr 01 x-z<<<1;f);f

Thanks to pxeger for the shorter way to print y.

Try it online!

Pure zsh, 24 bytes

f()(<<<$SPROMPT[29];f);f

Try it online!

Bonus: mandatory phrase from stdin — 21 - 10 bytes

read a;f()(<<<$a;f);f

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Shorter way to print y: tr 01 x-z<<<1 \$\endgroup\$
    – pxeger
    Nov 18, 2021 at 8:12
2
\$\begingroup\$

Zsh, 14 bytes

x=Y;<<<$x:l;$0

Try it online!

Set x=Y, then print $x, :lowercased. $0 recurses.

\$\endgroup\$
2
\$\begingroup\$

Python 3, 25 bytes

while 1:print(chr(97|25))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Apparently this isn't totally portable. My sys.version is 2.7.9 (default, Dec 11 2014, 04:42:00) \n[GCC 4.9.2], so if yours is different this might not work I guess.

Python 2 - (76-10)=66

import sys
s=sys.stdin;a=`help`[1]if s.isatty()else s.read()
while 1:print a

Pretty long, but I wanted to go for the bonus (even though it cost more than 10 bytes). Checking if stdin is empty or not without prompting for input is long, apparently.

At first, I misread the bonus as taking an argument instead of stdin. I'm proud of my solution for that, so I'm posting it anyway ;)

Python 2 - (52-10+∞)=∞ (invalid!)

import sys
while 1:print(sys.argv+[0])[1]or`help`[1]

sys.argv is a list where the zeroth element is the filename and every element afterwords is an argument given to the program. I add a falsey value to the end of the list; if there are no arguments, the first element is that falsey value, otherwise it's the first argument. a or b in Python returns the first value that confirms what the outcome will be: If a is truthy, we already know that the whole thing will be true, so it just gets returned. If it's false, b is returned (since False or b == b).

\$\endgroup\$
5
  • \$\begingroup\$ @fry I could, but it would prompt the user. I wanted it to start spitting out y\n immediately if stdin was empty. \$\endgroup\$ Dec 17, 2014 at 14:53
  • \$\begingroup\$ @fry That's weird, it works for me. It might be dependant on cPython? I don't have any other ideas. \$\endgroup\$ Dec 17, 2014 at 15:10
  • \$\begingroup\$ good idea, on it \$\endgroup\$ Dec 17, 2014 at 15:17
  • \$\begingroup\$ r=raw_input();p=r if r else`help`[1]\nwhile 1:print p 52 characters \$\endgroup\$
    – globby
    Dec 18, 2014 at 6:07
  • 1
    \$\begingroup\$ Hmm, already the first line seems to have an y in it, the second one too. \$\endgroup\$ Dec 18, 2014 at 12:51
1
\$\begingroup\$

Plan 9 rc: 20-10=10 (but not quite there)

f=`{cat};while()echo $f

The problem with this almost-solution is that it must have something on stdin. I haven't yet been able to find a way to get it to print y if stdin isn't there.

\$\endgroup\$
1
\$\begingroup\$

Windows Batch: 24 bytes

A yes.bat file with this content will work on most Windows XP and later systems with default settings:

@echo %COMSPEC:~12,1%&%0

Else this slightly larger yes.bat file (34 bytes) will work in all cases:

@set "T=%~n0"
@echo %T:~0,1%
@%0

Can be made down to 13 bytes if the file is called y.bat instead of yes.bat, but I feel that's cheating:

@echo %~n0&%0
\$\endgroup\$
1
\$\begingroup\$

C89, 31 37 chars

main(){for(;;)printf("%c\n",'z'-1);}

Built on the idea of @LordAro, without filename requirements.

\$\endgroup\$
0
1
\$\begingroup\$

linux (Ba)sh, 13 to 26 (23 to 36 chars) (repeats 'y' or the input, like yes)

create an executable (+x) file, named as you like (ex: my_yes), containing one of those possible contents:

echo ${*:-y};exec $0 $*  #23 chars, score 13, but loses spaces, and will choke on many inputs

or

echo "${@:-y}";exec $0 "$@"  #27 chars, score 17, but will choke on some inputs

or

printf "%s\n" "${@:-y}";exec $0 "$@"  #36 chars, score 26, a tiny more robust, but "longer"

and then execute it with the arguments you want, it will repeat them ad-lib. (With no arguments, it repeats "y", as the yes command does too)

$ chmod +x my_yes
$ ./my_yes
y
y
y
[ctrl-C]

$ ./my_yes '  It looks ok!'
  It looks ok!
  It looks ok!
  It looks ok!
  It looks ok!
[ctrl-C]
\$\endgroup\$
2
  • \$\begingroup\$ some inputs that can make it choke on the first 2 : first argument starting with a "-", for example. (and many others) \$\endgroup\$ Dec 18, 2014 at 13:18
  • \$\begingroup\$ I use "exec" to ensure I don't bring the computer down very quickly with recursive bash (which 'yes' doesn't do, usually ^^) \$\endgroup\$ Dec 18, 2014 at 13:20
1
\$\begingroup\$

CFML, 247 bytes

This is more a "just for the heck of it" answer, as it's obviously hideously long (CFML earns no points for terseness); and I just realized it can't actually be killed without restarting the service, although it will indeed stop printing to the browser on an ESC character.

<html><body><cfflush><cfif isDefined("URL.i")><cfscript>a=URL.i;if(a is ""){a=chr(asc("x")+1);}while(1){writeOutput("#a#<br>");getPageContext().getOut().flush();}</cfscript><cfelse><form action="a.cfm"><input name="i"/></form></cfif></body></html>
\$\endgroup\$
1
\$\begingroup\$

C, 35 bytes

main(a,b){b='x'+1;for(;;)puts(&b);}

I don't know if this will compile correctly with all compilers on all platforms or not; if it doesn't work for you, let me know. I used several things here that I have never thought of using before. :)

\$\endgroup\$
1
\$\begingroup\$

Node.js, 53 chars (63 if you count node -e "")

update:

  • Reduced another 5 chars.
  • Silly extra char removed.
  • Reduced 2 chars, from 61 to 59.

The real yes does not uses stdin, but an argument, like:

yes n

Well, this answer works like that, it will print y\n over and over:

node -e "for(;;)console.log(process.argv[1]||34 .toString(36))"

And if you append an n after the line, it will print n\n instead:

node -e "for(;;)console.log(process.argv[1]||34 .toString(36))" n

No bonus, since I'm not using stdin.

\$\endgroup\$
1
\$\begingroup\$

CAPL1.5+ ; 6 without input; 10 - 10 = 0 with input

Sidenote
I have read somewhere [link?] that custom languages aren't allowed in golfing questions, as they could make built-in functions that do exactly what the question is asking, however I made CAPL to make golfing easier in general. If you think this is not allowed here, let me know!

I got some ideas from ><> and Befunge (You can move between lines and use hexadecimal characters to push numbers), some from Ruby and some from my own to make golfing easier.
CAPL reads from left to right, and goes one line down at the end of the line. If it is as the last line, the program will quit.

As no-one knows this language yet, I'll try to explain as much as possible.

Outputting y. 6 bytes

bb*.n<

bb* b is hexadecimal for 11, so bb* is 11*11=121, which is the UTF-8 equivalent of y. This value is pushed to the stack.
. Pops the top value from the stack, and outputs as UTF-8. As 121 is on top of the stack, the index is ignored here.
n Outputs a newline
< Sends the pointer back to the beginning of the line, thus repeating that line. As we don't expect input, we can do this safely without re-asking for the input.

Outputting from input. 10 bytes, 0 after bonus

i~a&{X:.)}

i Takes input from the user, pushes as UTF-8 on the top of the stack, and pushes the length after that. I.e. [72,101,108,108,111,5]
~ Pops a number from the stack, then reverses that amount of bytes. I.e. [111,108,108,101,72]
a Hexadecimal for 10, the newline character
&{...} Makes an infinite loop. We have input, so we can't send the pointer back to the line. I could place the function on the line below, which would safe me a byte, but newlines aren't allowed in this challenge.
X Removes the top value from the stack (The index from the loop)
:. Duplicates the top value, then outputs as UTF-8
) Turns stack right. ([1,2,3,4,5] -> [5,1,2,3,4])

Howver, this means we start with a newline, then start outputting the input, then a newline, then the input, etc. If we're not allowed to start with a newline, use the following code with 12 bytes, or 2 after subtracting the bonus.

iXa#~&{X:.)}

The only new command here is #, which pushes the amount of items on the stack to the stack.
I removed the length from i, because adding 1, then swapping with the newline is longer than removing and getting the length again.

Just for fun, here is a "Hello World" program

"Hello World"#~
#?!;.<

The ?! operation is the same as ><>'s

\$\endgroup\$
3
  • \$\begingroup\$ Actually the restriction applies to custom languages/libraries/features published after the question was posted. \$\endgroup\$
    – manatwork
    Aug 4, 2015 at 18:28
  • \$\begingroup\$ @manatwork That would mean my answer is invalid. Thanks for the clarification. I made this language, and wanted to do some golfing challenges to test what I could improve for future versions, if that makes anything better. \$\endgroup\$
    – Charlie
    Aug 4, 2015 at 19:28
  • \$\begingroup\$ In my opinion this is not invalid, just not eligible to win. The restriction's goal was to prevent cheating, but as you included the statement about the language's freshness, this could hardly be considered a cheating attempt. \$\endgroup\$
    – manatwork
    Aug 5, 2015 at 6:26
1
\$\begingroup\$

SmileBASIC, 19 bytes - 10 = 9

INPUT C$@L?C$GOTO@L
\$\endgroup\$
1
\$\begingroup\$

QC 26 (26 - 10 = 16) bytes

(00600F014"0178)0101A01$14

The program requires an input on stdin because QC doesn't have a way to get arguments. Empty input just prints y.

Explanation

(00 read string into memory at address 00, address 00 is length, next bytes are the string itself
600F014 jump if values at 00 and F0, if not equal jump to address 14
"0178 Write 78(lowercase x) to address 01
)0101 increment value at address 01 by 01 which gives y
A01 print string and newline from address 01 until 00 is reached
$14 jump to address 14(print the next line)
\$\endgroup\$
1
  • \$\begingroup\$ Wait, It's decremented by 10, so it becomes 16 bytes, making this the ultimate answer! \$\endgroup\$ Feb 12, 2017 at 6:58
1
\$\begingroup\$

Vitsy, 15 - 10 = 5 bytes

Because Vitsy waits for STDIN to be entered, I have made the bonus input come in as command line arguments.

zl)[bD*][:ZaOu]

Explanation:

z               Grab the entirety of the input stack and patch it on the current.
 l)[   ]        If the stack has no members...
    bD*         Push 11^2 to the stack (this is the ASCII value for 'y').
        [     ] Loop infinitely.
         :      Duplicate the current stack.
          Z     Push the entire contents of the stack to output, top first.
           aO   Push out a newline to the out.
             u  Flatten the top two stacks.
\$\endgroup\$
1
\$\begingroup\$

C(GCC) - 161 bytes

main(int argc,char**argv){if(argc>1){while(1){for(int i=0;i<argc-1;i++){printf("%s ",argv[i+1]);}puts("");}}else{char*c=malloc(1);c[0]='x'+1;while(1){puts(c);}}}

Uses malloc to allocate 1 byte and set its value 'x' + 1 If any input is provided as an argument(mulitple words work too) it will print it instead of y.

Ungolfed version:

main (int argc, char**argv) {
    if (argc > 1) {
        while (1) {
            for (int i=0; i < argc - 1; i++) {
                printf("%s ",argv[i+1]);
            }
            puts("");
        }
    } else {
        char* c = malloc(1);
        c[0] = 'x' + 1;
        while(1) {
            puts(c);
        }
    }
}
\$\endgroup\$
1
\$\begingroup\$

Ohm, 17 - 10 = 7 bytes

IDö?.x1+;Ω
D,∞

Explanation:

IDö?.x1+;Ω  ■Main link
I           ■Input
 Dö?    ;   ■If(!input){
    .x1+    ■  Push 'x' and increment
            ■}
         Ω  ■Call link below

D,∞         ■Helper link
  ∞         ■while (true){
D,          ■  duplicate and print
            ■}
\$\endgroup\$
1
\$\begingroup\$

R, 51 bytes

while(T){cat(paste0(letters[25],'\\',letters[14]))}

Literally outputs "an infinite stream of "y\n"'s", rather than "y[newline]".

In RStudio the output is truncated but it is otherwise infinite.

\$\endgroup\$
1
\$\begingroup\$

R, 27-10 = 17 bytes

x=scan(,'');repeat print(x)

Repeats stdin over and over and over. No y or \n in the code.

\$\endgroup\$
0
1
\$\begingroup\$

><>, 6 bytes

b:*oao

By not including a ; at the end, the ><> will keep on swimming until he's released by a SIGINT.

Explanation

b:*oao
b         Push 11
 :        Duplicate
  *       Multiply top elements to get 121
   o      Print as character
    a     Push 10
     o    Print as character (yielding '\n')
          No program terminator, so our ><> will 
          keep on swimming this path forever.
^----' 


><>, 17 - 10 = 7 bytes

The previous is quite a boring solution, so here's one that takes input from stdin. This abuses the fact that the default way of supplying input to a ><> program is echo 'input' | fish.py yes.fish, where echo provides the \n character.

 i:0)?!v
0r}o:r~<

Explanation

 i:0)?!v     Load STDIN into the stack (reversed)

             NOP           <----------,
 i           Push a byte from STDIN   |
  :          Duplicate top element    |
   0         Push 0                   |
    )?       If (i > 0):              |
      !        Wrap around  ----------'
             Else:
       v       Move to the second part

0r}o:r~<     Print STDIN until halted

       <     Go right         <---------------,
      ~      Remove top element (duplicated   |
                -1 from EOF)                  |
     r       Reverse stack                    |
    :        Duplicate top element            |
   o         Output as character              |
  }          Rotate stack to right (go to     |
                next character)               |
 r           Reverse the stack                |
0            Push 0                           |
             Wrap around       ---------------'

The 0r at the end allow sfor the loop to happen by wrapping around, where we still assume the stack to be reversed with a -1 on top.

\$\endgroup\$
1
\$\begingroup\$

Perl 5 - (10-10) = 0 bytes

print;redo

Requires the -ne flags. Takes in a character(s) from STDIN, repeats the character(s) infinitely.

Example execution

perl -ne 'print;redo'

Explanation

This takes advantage of the -n flag in Perl, where it explicitly wraps while (<>) { ... } around the source code. The use of redo without an explicit label is also used. According to the documentation: If the LABEL is omitted, the command refers to the innermost enclosing loop.

\$\endgroup\$
1
\$\begingroup\$

Mumps (M), 12 bytes - 10 = 2 bytes

Quite simple in Mumps (M), as the language allows single character commands and a newline is denoted by '!'...

R I F  W I,!

Breakdown:

Read STDIN into I, For (with two spaces) = infinite loop, Write contents of I, then newline (!)

Works on both InterSystems & YottaDB (GT.M).

\$\endgroup\$
1
\$\begingroup\$

Husk, 1 - 10 = -9 bytes

The wonderful thing about Husk's laziness is that computations involving infinite lists can be managed and used to produce useful output, like in this case :)

Try it online!

Explanation

   Implicit argument
∞  Build an infinite list repeating forever
   Implicit join by newlines
\$\endgroup\$
1
\$\begingroup\$

Raku (raku -n) 9-10 = -1 bytes

.say;redo

Idea for redo from @booshlinux (I would have done: loop{.say})

\$\endgroup\$
1
\$\begingroup\$

05AB1E, -8 bytes

[,

Try it online!

[,  # full program
[   # forever...
 ,  # output...
    # implicit input
\$\endgroup\$
1
\$\begingroup\$

BRASCA, -3 bytes (7-10)

l,[:mo]

Try it online!

Explanation

<implicit input>
l           - Push 10 (\n)
 ,          - Reverse stack
  [   ]     - While non-zero:
   :mo      - Duplicate, move the duplicate to bottom, output top of stack
\$\endgroup\$
1
\$\begingroup\$

brainfuck, 60-10=50 bytes

+>>,[[>,]>+<]++++++++++>-[<[<+<++++++++++++>>-]]<[[<]>[.>]<]

Try it online!

Explanation:

+>> add one (120 to be adden on later yeilding 'y')
,[[>,]>+<]  if there is input, read it and set a flag to skip the next loop
++++++++++>-  '\n' 
[
  <[<+<++++++++++++>>-] add 10*120 to first cell, shift \n back one cell
]
<[[<]>[.>]<] print the input or 'y\n'
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to the site, and nice first answer! \$\endgroup\$ Mar 9, 2021 at 17:48
1
\$\begingroup\$

JavaScript (Node.js), 86 80 bytes

with strict adherence to the you are not allowed to use "y" or "\n" or their ASCII values (121, 0x79, 0171, 10, 0xA or 012) rule

for(;;)console.log(eval('T\\u'+(+[])+(+[])+(38+38+3)+'peError').toString()[6+4])

Try it online!

-6 bytes thanks to me

\$\endgroup\$
1
\$\begingroup\$

Vyxal, -8 bytes (2-10 bytes)

Not the most original method, but it's short.

{,

Explanation

{         - While non-zero
 ,        - Print implicit input

Try it Online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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