Cat program

From Esolang
Jump to navigation Jump to search
Not to be confused with Cat Program (language).

The cat program is a program that copies its standard input to its standard output. It is named after the Unix command cat, although this command is actually more powerful. The cat program is a popular problem.

cat has also been described humorously as a programming language in which every program is a quine. The programming language is obviously of very limited computational usefulness. See HQ9+ and Text.

Hello world and quine program

#!/usr/bin/cat
Hello, world!

You must chmod +x the program file to make it executable:

$ chmod +x file

To execute it:

$ ./file

(assuming file is the filename of the program)

Reverse cat

Other shell commands could be used in similar way, so there is reverse variant of cat language too. Unix command line utilities in this space include both rev (reversing characters within each line) and tac (reversing the order of lines in the input; cat backwards).

 $ echo '!dlrow ,olleH' | rev
 Hello, world!
 $ (echo 'world!'; echo 'Hello,') | tac
 Hello,
 world!
 $ (echo '!dlrow'; echo ',olleH') | tac | rev    # or ... | rev | tac
 Hello,
 world!

A simple "reverse cat" program could simply output the bytes of the input backwards, which for a conventional newline-delimited text file has roughly the effect of rev and tac combined (except for the treatment of the file's trailing newline).

While a regular cat program requires only O(1) storage space, a reverse cat program requires O(n) storage space, so as a popular problem it is a slightly more ambitious achievement.

See also