带有绑定变量的字符串截断匹配表达式
以下 shell 会话显示了我想了解的一些行为:
1> A = "Some text".
"Some text"
2> "Some " ++ R = A.
"Some text"
3> R.
"text"
4> B = "Some ".
"Some "
5> B ++ L = A.
* 1: illegal pattern
语句 2 和 5 在语法上肯定是相同的吗?我想使用这个习惯用法从字符串中提取一些文本,其中 B
正在从配置文件中读取。这可能吗?我应该使用什么语法来代替上面 5) 中显示的语法?
谢谢!
The following shell sessions shows some behavior I would like to understand:
1> A = "Some text".
"Some text"
2> "Some " ++ R = A.
"Some text"
3> R.
"text"
4> B = "Some ".
"Some "
5> B ++ L = A.
* 1: illegal pattern
Surely statements 2 and 5 are syntacticially identical? I would like to use this idiom to extract some text from a string, where B
is being read in from a configuration file. Is this possible, and what syntax should I use instead of that shown in 5) above?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
LHS ++ RHS
模式在编译时扩展为[ lhs0, lhs1, lhs2 | RHS]
(其中LHS =:= [lhs0, lhs1, lhs2]
,并且编译器拒绝对除文字字符串/列表之外的任何内容执行此操作。理论上它可以对变量执行此操作,但现在我认为您需要这样做:
The
LHS ++ RHS
pattern is expanded at compiletime to[ lhs0, lhs1, lhs2 | RHS]
(whereLHS =:= [lhs0, lhs1, lhs2]
, and the compiler refuses to do this for anything but literal strings/lists. In theory it could do this for variables, but it simply doesn't right now.I think in your case you need to do: