'Long code line continuation method in GAP language

Take the following code snippet as an example:

f:= FreeGroup("P","Q","R","S");
AssignGeneratorVariables(f);
g:= f/ParseRelators(f, "P^12=Q^4=R^4=S^8=1, Q^2=R^2, S^2 = P^6*Q^2*R,
Q*P=P^7*Q^2*R, Q*P^3=P^3*Q, R*P=P^10*Q*R, R*Q=P^6*Q^3*R, S*P=P^2*R*S,
S*Q=P^3*Q^3*R*S, S*R=R*S" );

I'm not sure if there is a long code line continuation method implemented in GAP language.

Edit: Based on the nice tricks and tips given by Olexandr and Horn, I would like to add some corresponding supplementary information as follows:

  1. "Triple Quoted Strings" is described here.

  2. The handling mechanism of line continuation, i.e., backslash followed by new line, is described here.

  3. In addition, the gap source code also includes the following description:

$ ugrep -i 'line continuation.*backslash'
bin/x86_64-pc-linux-gnu-default64-kv8/src/io.c:    // handle line continuation, i.e., backslash followed by new line; and
src/io.c:    // handle line continuation, i.e., backslash followed by new line; and

Regards, HZ



Solution 1:[1]

The problem here is not with the command occupying several lines, but with the string including a line break, as the error message says:

gap> g:= f/ParseRelators(f, "P^12=Q^4=R^4=S^8=1, Q^2=R^2, S^2 = P^6*Q^2*R,
Syntax error: String must not include <newline>
g:= f/ParseRelators(f, "P^12=Q^4=R^4=S^8=1, Q^2=R^2, S^2 = P^6*Q^2*R,
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You have to use backslash \ to continue the string from a new line. This input works:

f:= FreeGroup("P","Q","R","S");
AssignGeneratorVariables(f);
g:= f/ParseRelators(f, "P^12=Q^4=R^4=S^8=1, Q^2=R^2, S^2 = P^6*Q^2*R,\
Q*P=P^7*Q^2*R, Q*P^3=P^3*Q, R*P=P^10*Q*R, R*Q=P^6*Q^3*R, S*P=P^2*R*S,\
S*Q=P^3*Q^3*R*S, S*R=R*S" );

You can also use spaces as you like to indent and format it, e.g.

f:= FreeGroup("P","Q","R","S");
AssignGeneratorVariables(f);
g:= f/ParseRelators(f, 
"P^12  = Q^4 = R^4 = S^8 = 1,  \
 Q^2   = R^2, S^2 = P^6*Q^2*R, \
 Q*P   = P^7*Q^2*R,            \
 Q*P^3 = P^3*Q,                \
 R*P   = P^10*Q*R,             \
 R*Q   = P^6*Q^3*R,            \
 S*P   = P^2*R*S,              \
 S*Q   = P^3*Q^3*R*S,          \
 S*R   = R*S" );

or so.

Solution 2:[2]

As an alternative to the nice answer by Olexandr, let me mention that you can also use triple quoted strings to avoid the need for line continuations. E.g. like this:

f:= FreeGroup("P","Q","R","S");
AssignGeneratorVariables(f);
g:= f/ParseRelators(f, """
 P^12  = Q^4 = R^4 = S^8 = 1,
 Q^2   = R^2, S^2 = P^6*Q^2*R,
 Q*P   = P^7*Q^2*R,
 Q*P^3 = P^3*Q,
 R*P   = P^10*Q*R,
 R*Q   = P^6*Q^3*R,
 S*P   = P^2*R*S,
 S*Q   = P^3*Q^3*R*S,
 S*R   = R*S
""" );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Max Horn