【发布时间】:2019-04-22 15:14:43
【问题描述】:
我试图在包含某个子字符串的列表的上下文中查找字符串的索引,但无济于事。我不确定如何将整个未触及的数组作为参数传递给递归函数中的另一个函数。这是我的方法:
find' :: [String] -> Maybe Int
find' [] = error "List is empty"
find' [x] = return maybe 0
find' (x:xs)
| "HU" `isInfixOf` x = return elemIndex x (x:xs)
| otherwise = checkTail
where checkTail = find' xs
错误是:
* Couldn't match type `[[String]] -> Maybe Int' with `Maybe Int'
Expected type: String -> [String] -> Maybe Int
Actual type: String -> [String] -> [[String]] -> Maybe Int
* The function `return' is applied to three arguments,
but its type `([String] -> [[String]] -> Maybe Int)
-> String -> [String] -> [[String]] -> Maybe Int'
has only four
In the expression: return elemIndex x (x : xs)
In an equation for find':
find' (x : xs)
| "HU" `isInfixOf` x = return elemIndex x (x : xs)
| otherwise = checkTail
where
checkTail = find' xs
| "HU" `isInfixOf` x = return elemIndex x (x:xs)
^^^^^^^^^^^^^^^^^^^^^^^^^
我真的不明白错误以及为什么它不匹配,因为在这两种情况下我都返回一个可能的 int。但是,正如我所提到的,我不确定 (x:xs) 是否实际上意味着参数的整个列表。 澄清一下,我试图从字符串列表中找到字符串的索引。
【问题讨论】:
-
我发现很难理解你想用这个做什么 - 特别是你所说的
return和maybe是什么意思。它们都是 Haskell 中的标准函数,但我强烈怀疑两者都不是你认为的那样。 (特别是return maybe 0我认为你的意思是Just 0)