gpt4 book ai didi

variables - 如何通过文字进行模式匹配并同时为其分配变量?

转载 作者:行者123 更新时间:2023-12-05 08:31:00 25 4
gpt4 key购买 nike

我如何将这两者结合起来,以便能够进行模式匹配,同时将匹配的文字放入变量中?

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 = ...

参见示例 https://www.haskell.org/tutorial/patterns.html

关于variables - 如何通过文字进行模式匹配并同时为其分配变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60526257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com