我试图在 Idris 中创建一个函数,如下所示:
strSplit : String -> Maybe (Char, String)
这会将字符串“取消”到它的第一个Char
和字符串的其余部分,Nothing
如果它为空则返回。
所以我写了这个,但失败了:
strSplit x = case strM of
StrNil => Nothing
StrCons c cd => Just (c, cs)
所以我然后尝试了这个,有点像Prelude.Strings
:
strSplit x with (strM x)
strSplit "" | StrNil = Nothing
strSplit (strCons c cs) | (StrCons c cs) = Just (c, cs)
编译并运行没有问题。
我的问题是,为什么我必须使用该with
规则以这种方式拆分字符串,为什么我的原始方法会失败?
注意:抱歉,我目前无法访问解释器,因此我还不能在此处编写错误消息。