Tips for golfing in LOLCODE

14

What general tips do you have for golfing in LOLCODE? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to LOLCODE. (e.g. "remove comments" is not an answer). Please post one tip per answer.

Loovjo

Posted 2015-05-09T18:23:47.677

Reputation: 7 357

I had thought about posting this myself. It's actually not impossible to golf in LOLCODE. – Alex A. – 2015-05-09T18:39:41.960

2Somewhat embarassingly, my second and third highest upvoted answers are in LOLCODE. So I will take this opportunity to share everything I know. – Alex A. – 2015-05-09T18:51:34.190

Answers

7

Define variables using the syntax variable R value rather than I HAS A variable ITZ value.

If you want to set a variable n equal to 1,

n R 1

is only 5 bytes, whereas

I HAS A n ITZ 1

is 15 bytes.

Alex A.

Posted 2015-05-09T18:23:47.677

Reputation: 23 761

2But then you lose the whole point of LOLCODE! – Optimizer – 2015-05-09T18:47:48.200

3@Optimizer: To be fair, when is there ever a point to LOLCODE? – Alex A. – 2015-05-09T18:50:37.707

18The point of LOLCODE is for the lols, of course. – user12205 – 2015-05-09T19:01:16.513

1@ace: Okay, you got me there. – Alex A. – 2015-05-09T19:01:29.033

6lol​​​​​​​​​​​​ – bjb568 – 2015-05-10T00:47:39.423

Wait, can't you only do that if the var is already defined? – OldBunny2800 – 2016-07-22T16:14:47.827

@OldBunny2800 I think it depends on which interpreter you're using. – Alex A. – 2016-07-22T16:37:40.993

Using the lolcode.org language spec, it implies that <var> R <value> is only to change the value of a variable, similar to Javascript's var foo=bar vs foo=bar – OldBunny2800 – 2016-07-22T16:43:07.597

@OldBunny2800 On this site we define languages by their implementations rather than their specifications. The R initialization and assignment works in LOLCoffee, the one available on http://repl.it.

– Alex A. – 2016-07-22T16:45:54.740

1AWSUM THX! I didn't know that. – OldBunny2800 – 2016-07-22T16:56:31.803

3

In many LOLCODE implementations, such as the one on repl.it, HAI and KTHXBYE, which begin and end programs respectively, are unnecessary. In implementations in which they are necessary, the version number after HAI isn't necessary (e.g. HAI 1.2).

Similarly, the STDIO library is typically loaded by default, so CAN HAS STDIO? is also unnecessary.

Alex A.

Posted 2015-05-09T18:23:47.677

Reputation: 23 761

3

In many cases it's shorter to read variable values from STDIN rather than defining a function. However, note that GIMMEH, which reads input from STDIN, always reads a YARN (i.e. string). But you can take advantage of LOLCODE's dynamic typing and add 0 to convert to a NUMBR.

For example,

GIMMEH n
n R SUM OF n AN 0    
... (operations on n)

Defining n is 26 bytes, including newlines. Compare this to a user-defined function:

HOW DUZ I f YR n
    ... (operations on n)
IF U SAY SO

This requires 28 bytes.

Note that you could also multiply by 1 to convert to a NUMBR, but that requires 4 more bytes than the sum:

GIMMEH n
n R PRODUKT OF n AN 1

Alex A.

Posted 2015-05-09T18:23:47.677

Reputation: 23 761

1But n IS NOW A NUMBR is one byte shorter than n R SUM OF n AN 0 – Leaky Nun – 2016-07-22T16:53:57.037

@LeakyNun Which implementation has that? I haven't seen it before. – Alex A. – 2016-07-22T23:52:40.763

2

When printing the value of a variable to STDOUT, consider the following:

VISIBLE variable

is much shorter than

VISIBLE ":{variable}"

Also, whenever a trailing newline is acceptable,

VISIBLE variable

which includes a trailing newline by default, is shorter than

VISIBLE variable!

which suppresses the newline.

Alex A.

Posted 2015-05-09T18:23:47.677

Reputation: 23 761