gpt4 book ai didi

Haskell 模式匹配字符串中的字符

转载 作者:行者123 更新时间:2023-12-04 22:06:17 24 4
gpt4 key购买 nike

我有一个关于模式匹配的问题:

是否有可能以某种方式匹配 (string++ [char]++ anotherstring)?

我尝试过类似的事情:

f (s++";"++r) = s++r (the rhs is trivial, but its just for testing ;))

但这会导致解析错误。

最佳答案

不,这不可能。模式匹配根据构建它们的构造函数解构值,因此您只能在模式匹配中使用构造函数应用程序来描述哪些值与模式匹配,哪些不匹配。

对于类似于您的示例, case效果很好,

f str = case break (== ';') str of
(s, _:r) -> s ++ r
_ -> error "No semicolon found"

关于Haskell 模式匹配字符串中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13055770/

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