在此操作中,我在 RGL 中遇到了别名符号:
mkRoot3 : Str -> Root3 = \fcl -> case fcl of {
f@? + c@? + l => {f = f ; c = c ; l = l} ;
_ => error ("mkRoot3: too short root" ++ fcl)
} ;
它是什么意思,有什么用?
最佳答案
字符@用于模式匹配。表达式 foo@bar
表示您使用正则表达式 bar
匹配某些内容,并将结果绑定(bind)到变量 foo
。
让我们首先回顾一下您可以在没有@的情况下对字符串进行模式匹配的一些方法:
example1 : Str -> Str = \s -> case s of {
"x" => "the string is exactly x" ;
"x" + _ => "the string starts with x" ;
? => "the string is one character long" ;
? + ? => "the string is two characters long" ;
("a"|"i"|"u") => "the string is a vowel" ;
_ => "whatever, I'm bored"
} ;
在所有这些中,我们都没有重复使用右侧的字符串。如果你想这样做,你可以将它绑定(bind)到一个变量中,就像这样——还没有使用 @ ,因为我们只是匹配字符串的开头和结尾:
example2 : Str -> Str = \s -> case s of {
"x" + rest => "the string starts with x and ends with" ++ rest ;
start + "x" => "the string starts with" ++ start ++ "and ends with x" ;
_ => "..." } ;
最后,如果您想将某些内容与正则表达式匹配并使用与 RHS 上的正则表达式匹配的任何内容,现在您需要使用 @:
example3 : Str -> Str = \s -> case s of {
v@("a"|"i"|"u") => "the string is the vowel" ++ v ;
a@? => "the string is one character long:" ++ a ;
a@? + b@? => "the string is two characters long:" ++ a ++ "followed by" ++ b ;
_ => "..." } ;
如果您只是尝试匹配 a + b => …
,或任何其他仅包含变量的模式,它不会匹配恰好 2 个字符长的单词。相反,它只会将空字符串与其中一个匹配,将完整字符串与另一个匹配。
所以匹配正则表达式 ?
,它只匹配一个字符,然后将结果绑定(bind)到一个变量,这是唯一可以匹配精确长度的东西,然后重用匹配的字符的方法/右侧的字符串。
您可以在 http://www.grammaticalframework.org/doc/gf-refman.html#pattern-matching 阅读更多关于模式匹配的内容。 .
关于gf - GF中别名 "@"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70736782/