49
\$\begingroup\$

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.

\$\endgroup\$
6
  • 19
    \$\begingroup\$ I'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 \$\endgroup\$
    – Alex A.
    Commented Apr 14, 2015 at 19:40
  • \$\begingroup\$ You just can't golf SPL. At least, to my feeble knowledge of Shakesperian lore... \$\endgroup\$ Commented Apr 14, 2015 at 19:42
  • \$\begingroup\$ @RodolfoDias: Feeble? I think you're the resident Shakespeare scholar on PPCG. \$\endgroup\$
    – Alex A.
    Commented Apr 14, 2015 at 19:44
  • 9
    \$\begingroup\$ @AlexA. Which may not be a good thing: it just means I'm demented enough to try and understand SPL :-) \$\endgroup\$ Commented Apr 14, 2015 at 19:48
  • \$\begingroup\$ @AlexA. Sp can do it... \$\endgroup\$ Commented Jan 16, 2016 at 0:14

19 Answers 19

20
\$\begingroup\$

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

\$\endgroup\$
1
15
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ is D a valid Roman numeral? IIRC, it's 500 (but I'm not sure if it's valid in SPL) \$\endgroup\$
    – Giuseppe
    Commented Sep 7, 2017 at 15:33
  • 2
    \$\begingroup\$ @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. \$\endgroup\$
    – Maya
    Commented Sep 7, 2017 at 15:42
14
\$\begingroup\$

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

\$\endgroup\$
3
  • 11
    \$\begingroup\$ Could 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. ;) \$\endgroup\$
    – DLosc
    Commented Apr 21, 2015 at 2:49
  • \$\begingroup\$ @DLosc, from what I've seen, there's nothing forbidding you to do that. So... you're a better golfer than me ^_^ \$\endgroup\$ Commented Apr 21, 2015 at 9:28
  • 3
    \$\begingroup\$ Ahem. Don't call Robert Fraser demented. codegolf.stackexchange.com/a/69360/43394 \$\endgroup\$
    – wizzwizz4
    Commented Jan 16, 2016 at 11:30
11
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ If you need even more characters, there's a list here \$\endgroup\$
    – Maya
    Commented May 9, 2018 at 15:09
10
\$\begingroup\$

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.
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – Jo King
    Commented May 29, 2018 at 2:17
9
\$\begingroup\$

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!

\$\endgroup\$
7
\$\begingroup\$

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.
\$\endgroup\$
0
6
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ Time to back and edit some of my answers... Very nice find! \$\endgroup\$
    – Jo King
    Commented May 29, 2018 at 2:15
  • \$\begingroup\$ Is that a bug in the tokenizer, or is that behavior defined in the language? \$\endgroup\$
    – Cœur
    Commented Sep 29, 2018 at 4:36
  • 2
    \$\begingroup\$ @Cœur See update. \$\endgroup\$
    – user202729
    Commented Sep 29, 2018 at 7:33
6
\$\begingroup\$

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.
\$\endgroup\$
3
5
\$\begingroup\$

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.
\$\endgroup\$
5
\$\begingroup\$

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.
\$\endgroup\$
1
  • 4
    \$\begingroup\$ In 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! \$\endgroup\$
    – Jo King
    Commented May 8, 2018 at 12:04
4
\$\begingroup\$

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.
\$\endgroup\$
4
\$\begingroup\$

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

The operations the square of, the cube of, the factorial of and the product of exist but are only mentioned in passing (if at all) 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 you can get 24 as \$4!\$ i.e.

the factorial ofa big big cat

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

Finally, note that there is also the square root ofand the quotient between. Both round down, as the only type in SPL is integers: a short way to get 200 is as \$200 = \lfloor \sqrt{8!} \rfloor\$:

the square root ofthe factorial ofa big big big cat
\$\endgroup\$
3
\$\begingroup\$

Use shorter words

Examples:

Thy and thyself are shorter than your and yourself.

Is is shorter than are.

and more...

\$\endgroup\$
3
\$\begingroup\$

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.
\$\endgroup\$
3
\$\begingroup\$

All newlines are ignored

,.Ajax,.Puck,.Page,.Act:.Scene:.[Enter Ajax and Puck]Ajax:You Puck.[Exeunt]
\$\endgroup\$
3
\$\begingroup\$

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)
\$\endgroup\$
1
  • \$\begingroup\$ This doesn't to work in some situations where you've already removed a word, such as Speak thy or Let us Scene I \$\endgroup\$
    – Jo King
    Commented May 29, 2018 at 2:20
3
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.