Skip to main content
2 of 4
doing nothing might not count as "something different"
Ilmari Karonen
  • 21k
  • 5
  • 55
  • 101

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

Ilmari Karonen
  • 21k
  • 5
  • 55
  • 101