4
\$\begingroup\$

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

Please post one tip per answer.

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

Define λx.f x instead of copying f

In Grass you could usually copy something to the top of the stack to reduce the length of code referring to them.

But instead of defining the identity function and applying it:

wvWwwwww

You could define a new function that calls the original function instead:

wWWWWWw

Depending on the surrounding code and the number of items to be copied, it could be sometimes longer than the copying code. But it would likely still save you characters because it has one less item in the stack, and makes all references across this point shorter.

It doesn't work for characters that you would apply Out or Succ on them. It also cannot be used in a function. But it's likely you'll find other workarounds and make it still shorter.

\$\endgroup\$
0
\$\begingroup\$

Hide intermediate values in a function

For the intermediate values in a chain of statements that only one final result is useful afterwards, you could hide all the intermediate values in a function, to not use spaces in the main stack.

If the function is pure (doesn't involve I/O), and the argument isn't used, instead of defining the function and immediately calling it, you could save one item in the stack by defining the function to apply the argument as a function to the supposed return value and delaying the call to where it is used.

That is, for such a function, its application and further uses of its return value:

w WWwwww WWWw WWWWw v
Ww
WWWw WWWWww

Apply the argument to the supposed return value in the end of the function, remove the application, and swap the function and the argument when you need to use the supposed return value later:

w WWwwww WWWw WWWWw WWWWw v

Www WWwww

If you try them as full programs, they give different results, because the function calls Out which isn't pure, and they ran twice for the two calls.

\$\endgroup\$
0
\$\begingroup\$

Fluent interface for printing

It's often more useful to make the output function return itself, instead of the outputted character, when you are writing programs to print a block of hardcoded text. You could do this by quining:

ww WWWw WWWwww v     a = λf.λx.(Out(x);f(f))
Ww                   Out2 = a(a)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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