import Data.List elemIndex1 xss@(x:xs) = if (x == ' ') then (elemIndex x xss) else (elemIndex1 xs)
所以我希望这个函数给出:
elemIndex1 "qwe asd zxc" Just 3
elemIndex1 "qwe asd zxc" Just 0
据我所知,在else子句中xss实际上变成了xs。
你好像在等你 xss@(x:xs) 如下所示:
xss@(x:xs)
xss
x
xs
十
xss = "qwe asd zxc" x = ' ' xs = "asd zxe"
这不是模式匹配的工作方式。 实际上等于 x:xs ,所以在这个例子中 " asd zxc" .
x:xs
" asd zxc"
weirdElemIndex str = weirdElemIndex' str where weirdElemIndex' "" = Nothing weirdElemIndex' (x:xs) = if x == ' ' then elemIndex ' ' str else weirdElemIndex' xs
注意 str helper函数体中的I引用在其调用中将是一个常量。
str
不管它的价值,你人为的例子似乎相当于 elemIndex ' ' Nothing .
elemIndex ' '
Nothing