Tips for golfing in The Shakespeare Programming Language

44

5

What general tips do you have for golfing in The Shakespeare Programming Language? I'm looking for ideas which can be applied to code-golf problems and which are also at least somewhat specific to SPL (e.g. "remove comments" is not an answer).

Please post one tip per answer.

Loovjo

Posted 2015-04-14T17:17:29.190

Reputation: 7 357

17I'm not certain that you can golf SPL. SPL is so verbose that I almost feel like this question needs the code trolling tag. :P – Alex A. – 2015-04-14T19:40:07.890

You just can't golf SPL. At least, to my feeble knowledge of Shakesperian lore... – Rodolfo Dias – 2015-04-14T19:42:17.103

@RodolfoDias: Feeble? I think you're the resident Shakespeare scholar on PPCG. – Alex A. – 2015-04-14T19:44:02.093

8@AlexA. Which may not be a good thing: it just means I'm demented enough to try and understand SPL :-) – Rodolfo Dias – 2015-04-14T19:48:36.513

@AlexA. Sp can do it... – Conor O'Brien – 2016-01-16T00:14:53.380

This is not a golfing tip, but a note. On Listen tothy! command (aka int_input) it's undefined behavior if there are nothing left in the input, so you can't reliably determine EOF with integer input. – user202729 – 2018-05-30T13:34:07.820

Answers

18

(Well... since this is my adopted language 1, I feel like I'm obliged to add something here)

Use short variable names

Yeah, yeah, yeah, that's pretty standard to code-golf, you just use one-letter-length variable names, no news here. Or is there? Because the chaps that created SPL were wacky enough not to allow any name for their language's variables: they must come from Shakesperian plays which, in terms of character length, is a pain in the royal tush - but, if you're coding in SPL, you already know that. So, my advice would be to use characters with short names, like Ajax, Ford, Page or Puck, instead of The Archbishop of Canterbury, per instance.

That's all I got, I'm afraid.


1- since my Java skills are equivalent of those of a newborn.

Rodolfo Dias

Posted 2015-04-14T17:17:29.190

Reputation: 3 940

2Variable name list. – user202729 – 2018-05-28T15:55:08.207

13

(after doing the previous one, I just remembered a few things)

Choose your code carefully

An example, to try and explain what this mean.

Ajax:
 You are as warm as a big red rich fair cute bold cat!

and

Ajax:
 Thou art as blossoming as an embroidered trustworthy bottomless peaceful charming handsome chihuahua!

produce the exact same result, which is make the character being spoken to assume the value 64 (since, assuming you read my long battle with SPL, you know that both lines are equivalent to 2*2*2*2*2*2*1); however, the first one has 59 bytes, while the second one has a bytecount of 107.

Still, no one is going to be demented enough to enter a challenge with SPL, so...

Rodolfo Dias

Posted 2015-04-14T17:17:29.190

Reputation: 3 940

10Could you use the same adjective multiple times in a description? E.g. a big big big big big big cat, saving 4 more bytes? I know it ruins the artistry... but this is code golf after all. ;) – DLosc – 2015-04-21T02:49:13.217

@DLosc, from what I've seen, there's nothing forbidding you to do that. So... you're a better golfer than me ^_^ – Rodolfo Dias – 2015-04-21T09:28:52.593

1

Ahem. Don't call Robert Fraser demented. http://codegolf.stackexchange.com/a/69360/43394

– wizzwizz4 – 2016-01-16T11:30:32.473

12

Any Roman numeral is a valid scene number

That means, that instead of:

Scene I
Scene II
Scene III
Scene IV
Scene V

You can do:

Scene L
Scene I
Scene C
Scene X
Scene V

NieDzejkob

Posted 2015-04-14T17:17:29.190

Reputation: 4 630

is D a valid Roman numeral? IIRC, it's 500 (but I'm not sure if it's valid in SPL) – Giuseppe – 2017-09-07T15:33:52.897

@Giuseppe I don't understand why you don't just check that, but the official SPL interpreter supports M, C, D, X, L, I and V. – NieDzejkob – 2017-09-07T15:42:45.167

11

Remove unneeded words

Look at this (inserting newlines and whitespace for readability):

Puck:You is a big big cat!
     Open your heart!
Ajax:Be you worse than I?
     If so, let us return to Scene V!
     If not, you is a cat.
     You is the sum of I and the sum of a cat and you
     Speak thy mind!
     [Exeunt]

Looks pretty golfed eh? Here's an even more stripped down unintelligible form:

Puck:You big big cat!
     Open heart!
Ajax:Be you worse I?
     If solet usScene V!
     If notyou cat.
     You is the sum ofI the sum ofa cat you.
     Speak thy!

Try it online!

For reference, here's a list of easily removable words from code:

  • Speak thy mind -> Speak thy
  • Open your heart -> Open heart *
  • Listen to thy heart -> Listen tothy
  • Open your mind -> Open mind *
  • You is a cat -> You cat *
  • Only applies to constants such as big cat, otherwise you still need the 'is'
  • Let us return to -> Let us
  • Be X [op] than Y? -> Be X [op] Y?
  • You is [op] of X -> You is [op] ofX
  • You is [op] of X and Y -> You is [op] ofX Y
  • [Exeunt] -> (removed entirely)

Legend:

  • X and Y are constants like cat, I or you
  • [op] are operations like nicer, the sum of
  • * means you can remove the punctuation if it's the last sentence of the play

If I come across any others I'll add it to the list.

For extra reference, here's the shortest header to start off a program:

,.Ajax,.Puck,.Act I:.Scene I:.[Enter Ajax and Puck]

Try it online!

Add Page and Ford as extra characters as necessary.

Jo King

Posted 2015-04-14T17:17:29.190

Reputation: 38 234

1

If you need even more characters, there's a list here

– NieDzejkob – 2018-05-09T15:09:10.637

:| striped down – ASCII-only – 2018-05-29T05:59:10.333

8

Use short words

So here they are:

  • Use a instead of an or the, except the expressions the sum of and similar, since they won't work with a.
  • Use am, be or is instead of are or art.
  • Use I instead of me or myself.
  • Use you instead of thee, thou, thyself or yourself.
  • Use big for adjectives.
  • Use cat for positive numbers.
  • Use pig for negative numbers.
  • Use zero for zero.
  • Use worse for A < B (instead of smaller or punier).
  • Use nicer for A > B (instead of better, bigger, fresher, friendlier or jollier).
  • Character names:
    • 4 letters: Ajax, Ford, Page, Puck
    • 5 letters: Egeus, Helen, Julia, Lucio, Mopsa, Paris, Pinch, Priam, Regan, Robin, Romeo, Timon, Titus, Venus, Viola
    • 6 letters: Adonis, Aegeon, Alonso, Angelo, Arthur, Banquo, Bianca, Brutus, Cicero, Dorcas, Duncan, Emilia, Fenton, Hamlet, Hecate, Hector, Helena, Hermia, Imogen, Juliet, Lennox, Oberon, Olivia, Orsino, Portia, Thaisa, Thurio, Tybalt
    • 7 letters: Adriana, Aemilia, Agrippa, Antonio, Capulet, Cassius, Claudio, Dionyza, Escalus, Goneril, Horatio, Leonato, Luciana, Macbeth, Macduff, Malcolm, Mariana, Miranda, Octavia, Ophelia, Orlando, Othello, Pantino, Proteus, Publius, Shallow, Shylock, Slender, Solinus, Theseus, Titania, Troilus, Ulysses
    • 8 letters: Achilles, Beatrice, Benedick, Benvolio, Claudius, Cordelia, Cressida, Dogberry, Don John, Falstaff, Gertrude, Hermonie, Isabella, Lysander, Mercutio, Montague, Pericles, Polonius, Pompeius, Prospero, Rosalind, Stephano
    • 9 letters: Agamemnon, Antiochus, Autolycus, Balthazar, Brabantio, Cassandra, Cleopatra, Cornelius, Demetrius, Desdemona, Donalbain, Don Pedro, Ferdinand, Francisca, Hippolyta, King John, King Lear, Lychorida, Sebastian, The Ghost, Valentine, Vincentio
    • 10 letters: Andromache, Cymberline, Fortinbras, Friar John, King Henry
    • 11 letters: Mark Antony
    • 12 letters: Doctor Caius, King Richard, Lady Capulet, Lady Macbeth, Lady Macduff, Prince Henry, Queen Elinor
    • 13 letters: John of Gaunt, Julius Caesar, Lady Montague, Mistress Ford, Mistress Page
    • 14 letters: Friar Laurence, The Apothecary
    • 15 letters: Christopher Sly, Octavius Caesar
    • 17 letters: John of Lancaster, Mistress Overdone, The Duke of Milan
    • 18 letters: The Duke of Venice
    • 24 letters: The Abbot of Westminster
    • 28 letters: The Archbishop of Canterbury
    • If you really need more, you definitely should consider using the stack of each character.

NieDzejkob

Posted 2015-04-14T17:17:29.190

Reputation: 4 630

Note that multi-word characters can omit any spaces after their name (from user202729's tip), so they are preferable to characters with only one word

– Jo King – 2018-05-29T02:17:47.813

7

You don't have to switch people in conditionals.

For example,

Ajax:Is you better than the sum of a fat fat fat fat cat and a fat cat?
Puck:If so,let us return to Scene I.

is longer than

Ajax:Is you better than the sum of a fat fat fat fat cat and a fat cat?If so,let us return to Scene I.

Oliver Ni

Posted 2015-04-14T17:17:29.190

Reputation: 9 650

2WHOOps. fixing. – Oliver Ni – 2018-09-30T03:53:32.370

6

Use only one scene

Normally, you can't reuse the first scene of an act, since it always has [Enter Ajax and Puck] first thing. However, you can preface this with [Exeunt] to be able to reuse the first scene of a play. [Exeunt] is one byte shorter than declaring a new scene, and jumping to an Act is two bytes less than jumping to a Scene.

E.g: A simple cat program

,.Ajax,.Page,.Act I:.Scene I:.[Enter Page and Ajax]Scene V:.Ajax:Open mind!Speak thy!Let usScene V!

Try it online!

To:

,.Ajax,.Page,.Act I:.Scene I:.[Exeunt][Enter Page and Ajax]Ajax:Open mind!Speak thy!Let usAct I!

Try it online!

Jo King

Posted 2015-04-14T17:17:29.190

Reputation: 38 234

5

Jumps can be shorter

You can use return to even if the scene is ahead, and vice versa. In fact, the interpreter doesn't even care if you omit it entirely. This means these four statements are 100% equivalent:

Let us proceed to Scene X.
Let us return to Scene X.
Let us Scene X.
Let usScene X.

NieDzejkob

Posted 2015-04-14T17:17:29.190

Reputation: 4 630

Jo King (https://codegolf.stackexchange.com/a/164394/76162) has found that "Let us Scene X" is enough.

– Ed Wynn – 2018-05-09T07:09:49.940

@EdWynn good to know. I rewrote the answer to include this. – NieDzejkob – 2018-05-09T15:07:37.153

8Let usscene X is enough. – user202729 – 2018-05-28T15:50:41.450

5

Remove redundant spaces

Apart from the trivial ones (two adjacent spaces, adjacent to a punctuation, adjacent to a newline, at the end) (with some exceptions), the space after the following tokens can be removed:

  • the sum of
  • let us
  • the ghost (like this) (but who would use this character anyway)
  • if so and if not (remove the , after this, and don't need to add any space. Example: if solet usact I)
  • listen to (so listen tothy works, like this)
  • and basically any multi-word tokens.

Note: This does not work with the Perl interpreter. It's probably just an unintended effect caused by Flex's parsing behavior. The "language standard" is not clear about that.

user202729

Posted 2015-04-14T17:17:29.190

Reputation: 14 620

Time to back and edit some of my answers... Very nice find! – Jo King – 2018-05-29T02:15:30.373

Is that a bug in the tokenizer, or is that behavior defined in the language? – Cœur – 2018-09-29T04:36:24.327

2@Cœur See update. – user202729 – 2018-09-29T07:33:08.450

5

The "let us return to scene X" can be used even if the character being spoken to is off stage. So instead of:

Puck:Am I as fat as the sum of a big big big big big big cat and a cat?
Ajax:If not,let us return to scene III.
[Exit Puck]
[Enter Page]
Ajax:You is a big big big big big big cat.

You can do:

Puck:Am I as fat as the sum of a big big big big big big cat and a cat?
[Exit Puck]
[Enter Page]
Ajax:If not,let us return to scene III.You is a big big big big big big cat.

Robert Fraser

Posted 2015-04-14T17:17:29.190

Reputation: 912

4

Instead of substracting, add constants

You is the difference between I and a big cat.

is longer than

You is the sum of I and a big pig.

NieDzejkob

Posted 2015-04-14T17:17:29.190

Reputation: 4 630

3

If (so|not) does not have to immediately follow the comparison.

Am I as big as you?You big big big big big cat.If so, you is the sum of you and a big big cat.

NieDzejkob

Posted 2015-04-14T17:17:29.190

Reputation: 4 630

4In fact, the results of a comparison are stored as a flag and can be used repeatedly across scene transitions or across several If sos. This can be helpful in avoiding repeating comparisons! – Jo King – 2018-05-08T12:04:22.020

3

Use shorter words

Examples:

Thy and thyself are shorter than your and yourself.

Is is shorter than are.

and more...

Oliver Ni

Posted 2015-04-14T17:17:29.190

Reputation: 9 650

3

Use smart conditionals

For example, don't use

Ajax:Are you as good as or better than me?If so,let us return to Scene I.

But rather,

Ajax:Are you worse than me?If not,let us return to Scene I.

Oliver Ni

Posted 2015-04-14T17:17:29.190

Reputation: 9 650

2

Gotos aren't the only commands usable with If (so|not)

Am I as big as you?If so,you is the sum of you and a big big cat.If not,you is the sum of you and twice I.

NieDzejkob

Posted 2015-04-14T17:17:29.190

Reputation: 4 630

2

All newlines are ignored

,.Ajax,.Puck,.Page,.Act:.Scene:.[Enter Ajax and Puck]Ajax:You Puck.[Exeunt]

NieDzejkob

Posted 2015-04-14T17:17:29.190

Reputation: 4 630

2

Use < or > instead of ==

Not applicable to every situation, but it's quite a bit shorter:

Puck == Ajax

Puck:Is I as bad as you?

Puck < Ajax or Puck > Ajax

Puck:Is I worse you?
Puck:Is I nicer you?

4 bytes shorter for both!

Jo King

Posted 2015-04-14T17:17:29.190

Reputation: 38 234

2

You don't need the ending . or !.

Try it online!

This can also be used before a [. (thanks jimmy23013)

,.Ajax,.Page,.Act I:.Scene I:.[Enter Ajax and Page]Ajax:You cat[Exeunt]

Examples:

  • ,.Ajax,.Page,.Act I:.Scene I:.[Enter Ajax and Page]Ajax:You cat
  • ,.Ajax,.Page,.Act I:.Scene I:.[Enter Ajax and Page]Ajax:Open mind[Exeunt]

Doesn't work in:

  • ,.Ajax,.Page,.Act I:.Scene I:[Enter Ajax] (right after Scene {number}:) (however this works, I don't know why)
  • ,.Ajax,.Page,.Act I:.Scene I:.[Enter Ajax and Page]Ajax:Listen to your heart (and shorter variants)
  • ,.Ajax,.Page,.Act I:.Scene I:.[Enter Ajax and Page]Ajax:Let us return to scene I (and shorter variants)
  • ,.Ajax,.Page,.Act I:.Scene I:.[Enter Ajax and Page]Ajax:Recall (only after empty Recall, Recall x or Recall@ etc. works)
  • ,.Ajax,.Page,.Act I:.Scene I:.[Enter Ajax and Page]Ajax:Speak thy (without mind)

user202729

Posted 2015-04-14T17:17:29.190

Reputation: 14 620

This doesn't to work in some situations where you've already removed a word, such as Speak thy or Let us Scene I – Jo King – 2018-05-29T02:20:21.503

2

Use ‘twice’ and the distributive property

Instead of:

You is the sum ofa big big big big cat a big big big cat.

Use:

You is twice twice twice the sum ofa big cat a cat.

This saves 2 bytes per twice since 2 uses of big is 8 bytes (counting spaces) and twice is only 6 bytes.

Hello Goodbye

Posted 2015-04-14T17:17:29.190

Reputation: 442

2

Use 'square', 'cube' and 'product'

The three operations the square of, the cube of and the product of exist but are only mentioned in passing in the documentation. They can save you bytes to define (large-ish) constants. For instance, to get the number 64,

a big big big big big big cat

is 5 bytes longer than

the cube ofa big big cat

Similarly, the shortest way of getting 49 is with \$49=7^2\$ i.e.

the square ofthe sum ofa big big big cat a pig

and there can be situations where viewing a number as the product of two smaller existing constants can make the product of useful.

Robin Ryder

Posted 2015-04-14T17:17:29.190

Reputation: 6 625

0

Use constants to store large numbers

For example, if you have

Ajax:You is a fat fat fat fat fat fat fat fat fat fat cat!
Puck:You is the sum of thyself and a fat fat fat fat fat fat fat fat fat fat cat
Ajax:You is the sum of the difference of me and thyself and a fat fat fat fat fat fat fat fat fat fat cat!

You can just declare another person at the top

Ford,a fat fat fat fat fat fat fat fat fat fat cat.

and change your code to

Ajax:You is Ford.
Puck:You is the sum of thyself and Ford.
Ajax:You is the sum of the difference of me and thyself and Ford.

Oliver Ni

Posted 2015-04-14T17:17:29.190

Reputation: 9 650

8The official interpreter does not support declaring constants like that. – NieDzejkob – 2017-09-07T16:17:55.403

That, and also it's "the difference between", not "the difference of". Not related to your tip, though. – Hello Goodbye – 2019-12-14T21:14:32.113