Sta*_*ckd 4 haskell pattern-matching
我有关于模式匹配的问题:
是否有可能以某种方式匹配(字符串++ [char] ++ anotherstring)?
我尝试过类似的东西:
f (s++";"++r) = s++r (the rhs is trivial, but its just for testing ;))
Run Code Online (Sandbox Code Playgroud)
但这会导致解析错误.
不,这是不可能的.模式匹配根据构建它们的构造函数来解构值,因此您只能在模式匹配中使用构造函数应用程序来描述哪些值与模式匹配,哪些不匹配.
对于像你的例子,一个case很好的,
f str = case break (== ';') str of
(s, _:r) -> s ++ r
_ -> error "No semicolon found"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1735 次 |
| 最近记录: |