GAP语言中的长代码行续行方法

Long code line continuation method in GAP language

以下面的代码片段为例:

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" );

不知道有没有长代码行续法 用 GAP 语言实现。

编辑:根据Olexandr和Horn给出的nice tricks和tips,我想补充一些相应的补充信息如下:

  1. 描述了“三引号字符串”here

  2. 续行的处理机制,即反斜杠后跟换行,详见here.

  3. 另外,gap源码还包括以下说明:

$ 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

此致, 赫兹

这里的问题不是命令占用多行,而是字符串包含换行符,如错误消息所述:

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,
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

您必须使用反斜杠 \ 从新的一行继续字符串。此输入有效:

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" );

您也可以根据需要使用空格来缩进和格式化,例如

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" );

左右。

作为 Olexandr 的精彩回答的替代方案,让我提一下,您还可以使用三重引号引起来的字符串来避免需要续行。例如。像这样:

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
""" );