Jump to content

GolfScript: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Hello world: fix source for code; explanation
m Language: Citations; +clause
Line 8: Line 8:
== Language ==
== Language ==


Programs in GolfScript consist of lists of items, each of which is pushed onto the [[Stack (abstract data type)|stack]] as it is encountered with the exception of variables which have code blocks as their value, in which case the code is executed. When a variable assignment (carried out through the placement of a colon before the name of the variable) is encountered, the top item on the stack is popped and assigned to the variable.<ref name="esolang">{{cite web|title=GolfScript - Esolang|url=http://esolangs.org/enwiki/w/index.php?title=GolfScript&oldid=35741|accessdate=20 June 2014}}</ref>
Programs in GolfScript consist of lists of items, each of which is pushed onto the [[Stack (abstract data type)|stack]] as it is encountered with the exception of variables which have code blocks as their value, in which case the code is executed. When a variable assignment (carried out through the placement of a colon before the name of the variable) is encountered, the top item on the stack is popped and assigned to the variable.<ref name=":0" />


GolfScript attempts to reach its goal of solving problems in as few keystrokes as possible through the use of single symbols to represent high-level operations.<ref name="official website" /> A single symbol can stand for multiple operations depending on the type of the arguments passed to it.<ref name="esolang" />
GolfScript attempts to reach its goal of solving problems in as few keystrokes as possible through the use of single symbols to represent high-level operations.<ref name="official website" /> A single symbol can stand for multiple operations depending on the type of the arguments passed to it,<ref>{{Cite web|url = http://www.golfscript.com/golfscript/quickref.html|title = GolfScript Quick Reference|date = 18 May 2012|accessdate = 25 June 2014|website = GolfScript}}</ref> and can even be reassigned and used like a variable.<ref name=":0" />


=== Input and output ===
=== Input and output ===
If input is supplied to a GolfScript program, it is pushed onto the stack as a string before the program is executed. Output is provided via the stack's contents being printed when execution of the program finishes.<ref name="esolang" />
If input is supplied to a GolfScript program, it is pushed onto the stack as a string before the program is executed. Output is provided via the stack's contents being printed when execution of the program finishes.<ref name=":0" />


== Examples ==
== Examples ==
Line 19: Line 19:
=== Hello world ===
=== Hello world ===


The traditional [[Hello world program]] can be written in GolfScript as:<ref>{{Cite web|url = http://www.golfscript.com/golfscript/tutorial.html|title = GolfScript Tutorial|accessdate = 25 June 2014|website = GolfScript|date = 18 May 2012}}</ref>
The traditional [[Hello world program]] can be written in GolfScript as:<ref name=":0">{{Cite web|url = http://www.golfscript.com/golfscript/tutorial.html|title = GolfScript Tutorial|accessdate = 25 June 2014|website = GolfScript|date = 18 May 2012}}</ref>


"Hello World!"
"Hello World!"

Revision as of 15:02, 25 June 2014

GolfScript is an esoteric programming language that is stack-based and designed to allow programs to be written in as few keystrokes as possible. It aims to be simple and easy to write.[1] The design of the language makes it easy to use in code golfing contests.

History

GolfScript was released on 13 December 2007 and added to the golf server run by Shinichiro Hamaji the next day.[1] The GolfScript specification was originally implemented in Ruby by Darren Smith.[2] A Perl implementation was released by Marty O' Brien on 11 July 2011.[3]

Language

Programs in GolfScript consist of lists of items, each of which is pushed onto the stack as it is encountered with the exception of variables which have code blocks as their value, in which case the code is executed. When a variable assignment (carried out through the placement of a colon before the name of the variable) is encountered, the top item on the stack is popped and assigned to the variable.[4]

GolfScript attempts to reach its goal of solving problems in as few keystrokes as possible through the use of single symbols to represent high-level operations.[1] A single symbol can stand for multiple operations depending on the type of the arguments passed to it,[5] and can even be reassigned and used like a variable.[4]

Input and output

If input is supplied to a GolfScript program, it is pushed onto the stack as a string before the program is executed. Output is provided via the stack's contents being printed when execution of the program finishes.[4]

Examples

Hello world

The traditional Hello world program can be written in GolfScript as:[4]

"Hello World!"

This program has only one item, the string "Hello World!", which is pushed onto the stack and then printed at the end of the program.

Other examples

A program to get two numbers from standard input, add them together, and print the result can be written in GolfScript as:[6]

n%'+'*'"#{
}"'n/\*~

References

  1. ^ a b c "GolfScript". Retrieved 20 June 2014.
  2. ^ "Language::GolfScript - search.cpan.org". Retrieved 20 June 2014.
  3. ^ "Marty O'Brien / Language-GolfScript-0.04 - search.cpan.org". Retrieved 20 June 2014.
  4. ^ a b c d "GolfScript Tutorial". GolfScript. 18 May 2012. Retrieved 25 June 2014.
  5. ^ "GolfScript Quick Reference". GolfScript. 18 May 2012. Retrieved 25 June 2014.
  6. ^ "popularity contest - Anti-golfscript anti-golf: create a task where GolfScript or J is outgolfed by your (conventional) lang - Programming Puzzles & Code Golf Stack Exchange". Retrieved 20 June 2014.