Try switching the order of expressions
When you have multiple atoms (names/integers) next to each other, they have to be separated by whitespace. In some cases, you can rearrange the expression to eliminate the whitespace. A common example is with addition:
(a 1(subexpression))
(a(subexpression)1)
A less obvious case is when you have a function with multiple arguments. Consider a function F
with arguments (A B)
. If you're calling the function like this:
(F A(s B 1))
then you may be able to save a byte by taking the arguments as (B A)
instead:
(F(s B 1)A)
(These particular examples only work if they're not at the end of a line; in that case, parenthesis autocompletion would make both options the same length. But there are instances where switching the order helps even at the end of a line.)
Reordering can be especially helpful with conditionals. Frequently, you will have a recursive function with a body structured like this:
(i(condition)(recursive call)(base case))
In some instances, you can save bytes by inverting the condition and reversing the order of the truthy and falsey branches:
(i(inv-condition)(base case)(recursive call))
For example, here's a function F
that returns the length of a number's Collatz orbit:
(load library
(d F(q((N)(i(l 1 N)(a(F(i(odd? N)(a(* 3 N)1)(/ N 2)))1)0
If we change the condition (l 1 N)
(truthy if N is greater than 1, falsey if N is 1 or less) to (l N 2)
(truthy if N is less than 2, falsey if N is 2 or greater), we can switch the truthy and falsey branches and save a byte thanks to parenthesis autocompletion:
(d F(q((N)(i(l N 2)0(a(F(i(odd? N)(a(* 3 N)1)(/ N 2)))1
In this particular case, we can then save more bytes by rewriting (a(...)1
to (a 1(...
:
(d F(q((N)(i(l N 2)0(a 1(F(i(odd? N)(a(* 3 N)1)(/ N 2