Choosing Languages for Golfing

28

13

What are some tips for choosing the right language to golf? What factors affect the language to be chosen?

Here are some example problem types:

  • Problems that require I/O solution, either console or file
  • Problems that require parsing
  • Problems that requires you to write your solution as a function definition
  • Math problems
  • Problem dealing with prime numbers
  • Solving number puzzles
  • Performing numerical methods
  • String processing
  • Array processing
  • Tricky 2D array problems
  • Computational geometry
  • Recursion
  • 2D graphics
  • 3D graphics
  • Audio
  • Networking / web
  • Parallel processing

Don't simply say things like "Use GolfScript|J" because you like them.

Ming-Tang

Posted 2011-04-16T20:40:28.133

Reputation: 5 383

Question was closed 2016-03-13T09:32:16.630

Choose whatever language you want. It's also a competition within languages. More unique, interesting solutions in more languages is good. – Mego – 2016-02-24T21:36:37.313

I'm voting to close this question as primarily opinon-based; it's not a good, objective on-topic question within the scope defined in the help center

– cat – 2016-03-02T00:38:44.923

There is Advocate languages to golf in on meta, which has a similar goal.

– trichoplax – 2016-11-26T11:21:30.037

I think the best way to answer is to check the languages common for top solutions on this site. – Samy Bencherif – 2017-08-04T20:17:23.090

For some problems it's the type system. E.g. if you need to deal with integers greater than 64 bits, you want languages which have that built in (e.g. Golfscript, Haskell) rather than languages which make them expensive (e.g. Java). – Peter Taylor – 2011-04-16T21:02:44.800

I've made this a wiki in keeping with our policy on "Tips" type questions. – dmckee --- ex-moderator kitten – 2011-04-16T21:13:50.657

99 bottles is helpful if you need a language beginning with a specific letter... – Jesse Millikan – 2011-04-19T04:24:13.983

Kudos to ace, trimsty, algorithmshark and hosch250 for helping to make this thread a better resource. Please keep the submissions coming! It was a tough bounty call between algorithmshark and hosch250, who both gave a lot of detailed effort, but in the end Herr Shark gets the nod for leading the way. – Jonathan Van Matre – 2014-03-25T15:18:55.173

Answers

-1

Depends on what one needs, C/C++ is fast, but you have to code more of the work yourself. Python and Ruby are slower but are a lot easier to code with built in methods that shortens a lot of work and they automatically handle infinitely large values (if one has the RAM). Using a functional language like Haskell is great for purely mathematical functional use if one can frame the problem that way.

ewanm89

Posted 2011-04-16T20:40:28.133

Reputation: 101

This probably shouldn't be the accepted answer. The description about high vs low level languages doesn't actually have anything to do with character count. – Samy Bencherif – 2017-08-04T20:12:57.837

Yes, it is real fair to hold an 2011 answer to a question edited on the 23 Feb 2017. Maybe we should stop changing the questions after it has been answered? – ewanm89 – 2017-08-06T09:29:51.627

2As a primarily python user, in Py you pay for your ease of use in character count although there are some really hacky tricks which can save characters. Haskel and Lisp, while fun, tend to run even longer than Python. – arrdem – 2011-07-02T04:48:38.053

10

Putting my two cents in on array programming languages, J and APL in particular.

K/Kona, Q, and Nial fall in this category too, but they generally have the same benefits and criticisms. Use discretion. I will use J examples below, mostly because these are ASCII and thus easy to type--remember than APL characters count as single bytes, so don't let that be your problem with the language as choice for golfing.

  • Math problems
  • Solving number puzzles
  • Performing numerical methods
  • Tricky 2D array problems

These two are very good math and data-manipulation languages, because they toss arrays around a high level, and a lot of looping is done implicitly, by saying, e.g. add ten to each of 3, 4, and 5 (10 + 3 4 5) or sum each row of an array (+/"1 arr--the looping is in the "1).

  • Problem dealing with prime numbers

With prime number problems in particular, J has fast and short builtin primitives, as do some dialects of APL. (Edit: I'm thinking of Nars2000, which is part dialect and part completely different implementation. APL has no builtin for primes.) N-th prime (p:), no. of primes up to (_1&p:), factoring (q:), GCD and LCM (+. and *.), and so on, there's a lot there. However, in practice, the question will often specify that you have to cook your own prime implementations, so these don't see too much use. There are still neat and fancy ways of getting the prime stuff you need, it just becomes a little less cut-and-paste.

  • String processing
  • Array processing

Array and string processing is a bit of a mixed bag: if it's something that APL/J is good at or has a primitive or common idiom for, it's nearly trivial; if it's something that's very sequential and not very parallelizable, you're going to have a bad time. Anything in between is up in the air, though usually they will respond favourably.

  • Problems that require I/O solution, either console or file
  • Problems that requires you to write your solution as a function definition

IO is weird. APL has a single-character input expression, but with J you have to spend at least 8 to read in a number: ".1!:1]1. Output is a little less verbose, but you're still looking at 6 or 7 characters wasted, in practice. J in particular really likes it a lot if you can take in the input as arguments to a function, instead of having to muck around with IO itself.

In practice, with J and APL, usually the solution is written as a function that you invoke at the console. With APL, you can basically just put in variable names for your arguments and wrap the expression you were working with in curly braces and call it a day.

But with J, there's a bit of an overhead for defining functions explicitly--3 :'...', and you have to escape any strings inside--so what is usually done is something called tacit programming: you program at the function level, combining primitives in a manner not unlike that of Haskell. This can be both a blessing and a curse, because you don't have to spend as many characters referring to your arguments, but it's easy to drown in parentheses and end up losing tens of characters trying to hack your otherwise short and clever solution into something that works.

  • Problems that require parsing
  • Computational geometry

I don't have experience with golfing these particular problems, but I will say this: in the end, array programming languages are very good at piping and transforming lots of data in the same way. If you can turn the problem into an exercise in number shuffling, you can make it an APL/J problem, no sweat.

That said, not everything is an APL/J problem. Unlike Golfscript, APL and J just happened to be good for golfing, alongside their other benefits ;)

algorithmshark

Posted 2011-04-16T20:40:28.133

Reputation: 8 144

Excellent contribution! Thank you for being the first into the pool. – Jonathan Van Matre – 2014-03-18T16:39:57.890

4

Why hasn't Perl been praised yet? It's an excellent golfing language, for nearly every one of these, especially string related stuff (regex).

Burlesque is good for number-related programs, while Ruby is great for simpler text manipulation.

There's actually a list of languages and golfing scores over here.

cjfaure

Posted 2011-04-16T20:40:28.133

Reputation: 4 213

I don't know squat about Perl, but I agree. – ckjbgames – 2017-02-03T00:51:58.057

2

I like using obscure programming languages to (try to) get the job done.

Here are my favorites for the details you listed:

Problems that require I/O solution, either console or file

Languages like TI-Basic work well, but I prefer Ruby because of puts

Problems that require parsing

GolfScript will definitely aid you here

Problems that requires you to write your solution as a function definition

TI-84 Table - allows functions as Y= e.g. Y=|X| returns the absolute value of X

Math problems

TI-Basic - made for a calculator, so it includes math ;)

Problem dealing with prime numbers

Nothing special; Mathematica is probably the right tool for the job

Solving number puzzles

TI-Basic as it automatically loops through arrays

Performing numerical methods

TI-Basic or Mathematica

String processing

Python - has some great string functions.

No matter how good you think TI-Basic is, don't use it for strings...

Array processing

TI-Basic - automatically loops through arrays; e.g. increment all values in the array - L1+1→L1

Ruby - also has very powerful array features, and of course the ! will help compress code also

Tricky 2D array problems

Ruby or Python works best here, as TI-Basic doesn't support 2D arrays

Computational geometry

TI-Basic has geometric features and can be used for most math up to Calculus and Linear Algebra


BONUS

Looping

Either Arduino or Quomplex. Arduino has a built-in void loop(){} and Quomplex has the infinite loops contained by brackets ([])

Drawing/GUI

Game Maker Language has very powerful drawing features, and TI-Basic is also a generally useful tool because of the support for drawing on the graph.

Quines

Either HQ9+ or Quomplex because HQ9+ has the Q to output the program's source code and Quomplex will automatically print out its source code unless * (output) is specified or it produces no output, defined with #

Timtech

Posted 2011-04-16T20:40:28.133

Reputation: 12 038

2

If you're solving a math problem and you don't have Mathematica, try Sage. It is based on Python, so if you already know Python you don't need much time to learn its syntax.

Examples:

It is also useful for graph plotting and equation solving (e.g. you can use the solve() function in Sage, or if that is forbidden by the rules, it allows an easy implementation of the Newton Raphson process because it has the diff() function that can perform symbolic differentiation).

Besides, if you are a Python2 programmer, using Sage may allow you to cheat by skipping the long import statements. For example, math and sys are already imported by default. (Note that this may not work if your Python2 program depends on integer division.)

user12205

Posted 2011-04-16T20:40:28.133

Reputation: 8 752

Good info! Thanks for contributing. – Jonathan Van Matre – 2014-03-19T15:46:28.283

1

I know three languages - Java, C++, and Python 3. I know none of these at a high level, but this is my experience with them.

Java:

I would never use Java for golfing again. It takes over 80 characters just to write Hello World!. However, it does have its strengths:

Input requires creation of a Scanner object. It is difficult to input a single character. It requires specification of what types you are inputting.
Parsing is simple enough because of the for loop. The enhanced for loop is excellent for this.
Java supports methods, but the method declaration is quite long.
Java is excellent at math, as well as all other high-level languages.
Java is difficult to use when the problem involves modifying strings. You cannot make modifications to an existing string.
Java's arrays are simple to use.
Java is good at recursion.
Java includes built-in graphics. They are very easy to use.

C++

C++ is a very strong language, but it is somewhat lengthily when trying to golf at 56 characters for Hello world!.

Input and output are easy. You need not specify what types you are inputting - that is done automatically. However, you must include the iostream library.
Parsing is very easy.
Function declaration is simple, but eats a lot of important characters. C++ excels at math, but it does not include PI or E, as Java does.
C++'s strings are easy to use and change as needed.
I use vectors where possible instead of arrays, but both are easy to use.
C++ is good at recursion.
C++ does not include built-in graphics.

Python 3

Python 3 is similar to C++ and Java. It is much shorter as it is not strongly-typed - in other words, it just guesses at what the variables are.

Input is easy, but everything is input as strings. You must manually convert all input to whatever values you want.
Parsing and looping is very simple.
Python function declarations are quite simple and short.
Python is good at math.
Python's strings are simple to use.
Arrays are simple to use.
Python is good at recursion.
Python does not include built-in graphics.

user10766

Posted 2011-04-16T20:40:28.133

Reputation:

Java now has a 61-byte solution, but your point still stands. – MilkyWay90 – 2019-04-01T23:11:10.233

I guess by enhanced for-loop, you're talking about a range-for loop. C++ supports this since C++11 (http://en.cppreference.com/w/cpp/language/range-for). It's syntactically similar to the Java for-range loop and requires the iteratable object's class to implement begin() and end() or begin(T) and end(T) to be overloaded for the given object. It's also implemented for C-style strings and built-in arrays and works for all standard library containers.

– foobar – 2014-03-20T17:18:37.677

@foobar Yes, I was talking about that. Thanks for the info, I've wished for that ever since I took Java. – None – 2014-03-20T17:36:24.573