Skip to main content
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

GolfScript, 30 chars

{`".~"+"#{$<.read}".@=!{~}*}.~ 

This program reads the contents of a file named on the command line and, if it does not exactly equal the code above, interprets it as GolfScript. If the input is exactly equal to the code above, it will simply be printed unchanged (except for a newline appended to the end).

This is a fairly straightforward adaptation of this self-identifying programthis self-identifying program. Specifically:

  • { } is a code block literal in GolfScript.
  • .~, applied to a code block, duplicates the block and executes the copy.

Inside the code block:

  • ` stringifies the copy of the code block.
  • ".~"+ appends the characters .~ to it, yielding a string containing the source code of the program.
  • "#{$<.read}" is a documented hack that allow the execution of Ruby code within GolfScript. In this case, it executes the Ruby statement $<.read (shamelessly stolen from Lowjacker's Ruby solutionLowjacker's Ruby solution), which reads and returns the contents of any files specified on the command line. This hack is needed because GolfScript itself provides no explicit file I/O capabilities.
  • .@ duplicates and shuffles the elements on top of the stack so that the stack contains two copies of the file contents followed by the source code of this program.
  • =! compares the top two items on the stack (i.e. the file contents and the source), returning 1 if they are different and 0 if they are the same.
  • {~}* evaluates the remaining copy of the file contents as GolfScript code, but only if the result of the comparison is 1. (Technically, it executes the code block {~} as many times as given by the number on the stack, i.e. 0 or 1 times. Inside the block, ~ is the GolfScript eval operator.)

Ps. If reading the code to execute from stdin is allowed, this challenge can be solved in 21 characters without having to shell out to Ruby:

{`".~"+1$=!{""\~}*}.~ 

This program will read an input string from stdin and, if it does not match its own source, executes it (with an empty input). Like the program above, input that does match the source is simply echoed back.

GolfScript, 30 chars

{`".~"+"#{$<.read}".@=!{~}*}.~ 

This program reads the contents of a file named on the command line and, if it does not exactly equal the code above, interprets it as GolfScript. If the input is exactly equal to the code above, it will simply be printed unchanged (except for a newline appended to the end).

This is a fairly straightforward adaptation of this self-identifying program. Specifically:

  • { } is a code block literal in GolfScript.
  • .~, applied to a code block, duplicates the block and executes the copy.

Inside the code block:

  • ` stringifies the copy of the code block.
  • ".~"+ appends the characters .~ to it, yielding a string containing the source code of the program.
  • "#{$<.read}" is a documented hack that allow the execution of Ruby code within GolfScript. In this case, it executes the Ruby statement $<.read (shamelessly stolen from Lowjacker's Ruby solution), which reads and returns the contents of any files specified on the command line. This hack is needed because GolfScript itself provides no explicit file I/O capabilities.
  • .@ duplicates and shuffles the elements on top of the stack so that the stack contains two copies of the file contents followed by the source code of this program.
  • =! compares the top two items on the stack (i.e. the file contents and the source), returning 1 if they are different and 0 if they are the same.
  • {~}* evaluates the remaining copy of the file contents as GolfScript code, but only if the result of the comparison is 1. (Technically, it executes the code block {~} as many times as given by the number on the stack, i.e. 0 or 1 times. Inside the block, ~ is the GolfScript eval operator.)

Ps. If reading the code to execute from stdin is allowed, this challenge can be solved in 21 characters without having to shell out to Ruby:

{`".~"+1$=!{""\~}*}.~ 

This program will read an input string from stdin and, if it does not match its own source, executes it (with an empty input). Like the program above, input that does match the source is simply echoed back.

GolfScript, 30 chars

{`".~"+"#{$<.read}".@=!{~}*}.~ 

This program reads the contents of a file named on the command line and, if it does not exactly equal the code above, interprets it as GolfScript. If the input is exactly equal to the code above, it will simply be printed unchanged (except for a newline appended to the end).

This is a fairly straightforward adaptation of this self-identifying program. Specifically:

  • { } is a code block literal in GolfScript.
  • .~, applied to a code block, duplicates the block and executes the copy.

Inside the code block:

  • ` stringifies the copy of the code block.
  • ".~"+ appends the characters .~ to it, yielding a string containing the source code of the program.
  • "#{$<.read}" is a documented hack that allow the execution of Ruby code within GolfScript. In this case, it executes the Ruby statement $<.read (shamelessly stolen from Lowjacker's Ruby solution), which reads and returns the contents of any files specified on the command line. This hack is needed because GolfScript itself provides no explicit file I/O capabilities.
  • .@ duplicates and shuffles the elements on top of the stack so that the stack contains two copies of the file contents followed by the source code of this program.
  • =! compares the top two items on the stack (i.e. the file contents and the source), returning 1 if they are different and 0 if they are the same.
  • {~}* evaluates the remaining copy of the file contents as GolfScript code, but only if the result of the comparison is 1. (Technically, it executes the code block {~} as many times as given by the number on the stack, i.e. 0 or 1 times. Inside the block, ~ is the GolfScript eval operator.)

Ps. If reading the code to execute from stdin is allowed, this challenge can be solved in 21 characters without having to shell out to Ruby:

{`".~"+1$=!{""\~}*}.~ 

This program will read an input string from stdin and, if it does not match its own source, executes it (with an empty input). Like the program above, input that does match the source is simply echoed back.

shell out to ruby for file io
Source Link
Ilmari Karonen
  • 21k
  • 5
  • 55
  • 101

GolfScript, 2130 chars

{`".~"+1$=~"+"#{$<.read}".@=!{""\~~}*}.~ 

This program will interpret its inputreads the contents of a file named on the command line and, if it does not exactly equal the code above, interprets it as GolfScript, unless. If the input is exactly equal to the code above, in which case it just prints the inputwill simply be printed unchanged (except for a newline appended to the end).

This is a fairly straightforward adaptation of this self-identifying program. Specifically:

  • { } is a code block literal in GolfScript.
  • .~, applied to a code block, duplicates the block and executes the copy.

Inside the code block:

  • ` stringifies the copy of the code block.
  • ".~"+ appends the characters .~ to it, yielding a string containing the source code of the program.
  • 1$="#{$<.read}" comparesis a documented hack that allow the resulting string withexecution of Ruby code within GolfScript. In this case, it executes the inputRuby statement $<.read (while leaving a copyshamelessly stolen from Lowjacker's Ruby solution), which reads and returns the contents of any files specified on the inputcommand line. This hack is needed because GolfScript itself provides no explicit file I/O capabilities.
  • .@ duplicates and shuffles the elements on top of the stack) so that the stack contains two copies of the file contents followed by the source code of this program.
  • =! negatescompares the result oftop two items on the comparisonstack (i.e. the file contents and the source), yieldingreturning 1 if the strings don't match,they are different and 0 if they doare the same.
  • { ~}* executesevaluates the code insideremaining copy of the innerfile contents as GolfScript code block 0 or 1 times, depending onbut only if the result of the negationcomparison is 1.

Inside the inner code block:

  • (Technically, it executes the code block ""\{~} pushes an empty string belowas many times as given by the input stringnumber on the stack, to act as dummy input for the code (if any) to be executed byi.e. 0 or 1 times. Inside the followingblock, ~.
  • ~ executesis the input string as GolfScript codeeval operator.)

(The ""\ is needed to properly mimicPs. If reading the normal GolfScript execution environmentcode to execute from stdin is allowed, which always starts with the stack containing onethis challenge can be solved in 21 characters without having to shell out to Ruby:

{`".~"+1$=!{""\~}*}.~ 

This program will read an input string valuefrom stdin and, even if there is no actual input. Alternativelyit does not match its own source, replacingexecutes it with just . would execute the(with an empty input code with itself as). Like the program above, input that does match the source is simply echoed back.)

GolfScript, 21 chars

{`".~"+1$=!{""\~}*}.~ 

This program will interpret its input as GolfScript, unless the input is exactly equal to the code above, in which case it just prints the input unchanged (except for a newline appended to the end).

This is a straightforward adaptation of this self-identifying program. Specifically:

  • { } is a code block literal in GolfScript.
  • .~, applied to a code block, duplicates the block and executes the copy.

Inside the code block:

  • ` stringifies the copy of the code block.
  • ".~"+ appends the characters .~ to it.
  • 1$= compares the resulting string with the input (while leaving a copy of the input on the stack).
  • ! negates the result of the comparison, yielding 1 if the strings don't match, and 0 if they do.
  • { }* executes the code inside the inner code block 0 or 1 times, depending on the result of the negation.

Inside the inner code block:

  • ""\ pushes an empty string below the input string on the stack, to act as dummy input for the code (if any) to be executed by the following ~.
  • ~ executes the input string as GolfScript code.

(The ""\ is needed to properly mimic the normal GolfScript execution environment, which always starts with the stack containing one string value, even if there is no actual input. Alternatively, replacing it with just . would execute the input code with itself as input.)

GolfScript, 30 chars

{`".~"+"#{$<.read}".@=!{~}*}.~ 

This program reads the contents of a file named on the command line and, if it does not exactly equal the code above, interprets it as GolfScript. If the input is exactly equal to the code above, it will simply be printed unchanged (except for a newline appended to the end).

This is a fairly straightforward adaptation of this self-identifying program. Specifically:

  • { } is a code block literal in GolfScript.
  • .~, applied to a code block, duplicates the block and executes the copy.

Inside the code block:

  • ` stringifies the copy of the code block.
  • ".~"+ appends the characters .~ to it, yielding a string containing the source code of the program.
  • "#{$<.read}" is a documented hack that allow the execution of Ruby code within GolfScript. In this case, it executes the Ruby statement $<.read (shamelessly stolen from Lowjacker's Ruby solution), which reads and returns the contents of any files specified on the command line. This hack is needed because GolfScript itself provides no explicit file I/O capabilities.
  • .@ duplicates and shuffles the elements on top of the stack so that the stack contains two copies of the file contents followed by the source code of this program.
  • =! compares the top two items on the stack (i.e. the file contents and the source), returning 1 if they are different and 0 if they are the same.
  • {~}* evaluates the remaining copy of the file contents as GolfScript code, but only if the result of the comparison is 1. (Technically, it executes the code block {~} as many times as given by the number on the stack, i.e. 0 or 1 times. Inside the block, ~ is the GolfScript eval operator.)

Ps. If reading the code to execute from stdin is allowed, this challenge can be solved in 21 characters without having to shell out to Ruby:

{`".~"+1$=!{""\~}*}.~ 

This program will read an input string from stdin and, if it does not match its own source, executes it (with an empty input). Like the program above, input that does match the source is simply echoed back.

doing nothing might not count as "something different"
Source Link
Ilmari Karonen
  • 21k
  • 5
  • 55
  • 101

GolfScript, 1921 chars

{`".~"+1$=!*""\~{""\~}*}.~ 

This program will interpret its input as GolfScript, unless the input is exactly equal to the code above, in which case it does nothingjust prints the input unchanged (except printfor a newline appended to the end).

This is a straightforward adaptation of this self-identifying program. Specifically:

  • { } is a code block literal in GolfScript.
  • .~, applied to a code block, duplicates the block and executes the copy.

Inside the code block:

  • ` stringifies the copy of the code block.
  • ".~"+ appends the characters .~ to it.
  • 1$= compares the resulting string with the input (while leaving a copy of the input on the stack).
  • ! negates the result of the comparison, yielding 1 if the strings don't match, and 0 if they do.
  • { }* repeatsexecutes the input stringcode inside the inner code block 0 or 1 times, depending on the result of the negation.

Inside the inner code block:

  • ""\ pushes an empty string tobelow the bottom ofinput string on the stack, to act as dummy input for the code (if any) to be executed by the following ~.
  • ~ evals the result of the *, which equals executes the input if it doesn't match this program, or an empty string otherwiseas GolfScript code.

(The ""\ is needed to properly mimic the normal GolfScript execution environment, which always initializesstarts with the stack to containcontaining one string value, even if there is no actual input. Alternatively, replacing it with just . would execute the input code with itself as input.)

GolfScript, 19 chars

{`".~"+1$=!*""\~}.~ 

This program will interpret its input as GolfScript, unless the input is exactly equal to the code above, in which case it does nothing (except print a newline).

This is a straightforward adaptation of this self-identifying program. Specifically:

  • { } is a code block literal in GolfScript.
  • .~, applied to a code block, duplicates the block and executes the copy.

Inside the code block:

  • ` stringifies the copy of the code block.
  • ".~"+ appends the characters .~ to it.
  • 1$= compares the resulting string with the input (while leaving a copy of the input on the stack).
  • ! negates the result of the comparison, yielding 1 if the strings don't match, and 0 if they do.
  • * repeats the input string 0 or 1 times, depending on the result of the negation.
  • ""\ pushes an empty string to the bottom of the stack, to act as dummy input for the code (if any) to be executed by the following ~.
  • ~ evals the result of the *, which equals the input if it doesn't match this program, or an empty string otherwise.

(The ""\ is needed to properly mimic the normal GolfScript execution environment, which always initializes the stack to contain one string value, even if there is no actual input. Alternatively, replacing it with just . would execute the input code with itself as input.)

GolfScript, 21 chars

{`".~"+1$=!{""\~}*}.~ 

This program will interpret its input as GolfScript, unless the input is exactly equal to the code above, in which case it just prints the input unchanged (except for a newline appended to the end).

This is a straightforward adaptation of this self-identifying program. Specifically:

  • { } is a code block literal in GolfScript.
  • .~, applied to a code block, duplicates the block and executes the copy.

Inside the code block:

  • ` stringifies the copy of the code block.
  • ".~"+ appends the characters .~ to it.
  • 1$= compares the resulting string with the input (while leaving a copy of the input on the stack).
  • ! negates the result of the comparison, yielding 1 if the strings don't match, and 0 if they do.
  • { }* executes the code inside the inner code block 0 or 1 times, depending on the result of the negation.

Inside the inner code block:

  • ""\ pushes an empty string below the input string on the stack, to act as dummy input for the code (if any) to be executed by the following ~.
  • ~ executes the input string as GolfScript code.

(The ""\ is needed to properly mimic the normal GolfScript execution environment, which always starts with the stack containing one string value, even if there is no actual input. Alternatively, replacing it with just . would execute the input code with itself as input.)

Source Link
Ilmari Karonen
  • 21k
  • 5
  • 55
  • 101
Loading