我如何将这两者结合起来,以便能够进行模式匹配,同时将匹配的文字放入变量中?
fun1 :: Int -> String
fun1 1 = -- ..... how to bind 1 to a variable in the function declaration?
fun1 55 = -- ..... how to bind 55 to a variable in the function declaration?
fun1 123 = -- ..... how to bind 123 to a variable in the function declaration?
fun1 a = -- ........... all is OK
最佳答案
首先绑定(bind)a
,然后对其进行模式匹配,使其在所有分支的范围内。
fun a = case a of
1 -> ...
55 -> ...
123 -> ...
_ -> ...
或者使用 as-patterns。
fun a@1 = ...
fun a@55 = ...
fun a@123 = ...
fun a = ...
关于variables - 如何通过文字进行模式匹配并同时为其分配变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60526257/