Haskell模式匹配字符串中的char [英] Haskell pattern matching char in a string

查看:69
本文介绍了Haskell模式匹配字符串中的char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对模式匹配有疑问:

是否可以以某种方式匹配(字符串++ [char] ++另一个字符串)?

Is it possible to somehow match a (string ++ [char] ++ anotherstring)?

我尝试过类似的事情:

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

但这会导致解析错误.

推荐答案

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

No, it's not possible. Pattern matching deconstructs values according to the constructors they were built with, so you can only use constructor applications in pattern matching to describe which values match the pattern and which don't.

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

For something like your example, a case works well,

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

这篇关于Haskell模式匹配字符串中的char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆