我试图在我拥有的函数中使用字符串中的每个字符(仅使用一个字符),但我也尝试在同一个递归函数中使用相同的字符串作为一个整体,以将其与中的单个字符进行比较另一个字符串(使用elem
)。有没有办法可以使用该字符串的头和尾以及整个字符串,以便每次递归后字符串都不会被切断?
代码:
checkTrue :: TrueChar -> Char -> [Char] -> TruthValue
checkTrue a b c
| a == IsTrue b = AbsoluteTrue
| (a == IsFalse b) && (b `elem` c) = PartialTrue
| otherwise = NoneTrue
checkTruths :: [TrueChar] -> [Char] -> [TruthValue]
checkTruths [][] = []
checkTruths (a:as) (b:bs) = checkTrue a b (removeAbsoluteTrue (a:as) (b:bs)): checkTruths as bs
{- This is the line,
i wanted to use b as a string and also as b:bs. is this possible? -}
checkTruths _ _ = [NoneTrue]
最佳答案
您想要一个 as 模式,如 documented在 Haskell 2010 报告的第 3.17.1 节中。
Patterns of the form var@pat are called as-patterns, and allow one to use var as a name for the value being matched by pat. For example,
case e of { xs@(x:rest) -> if x==0 then rest else xs }
is equivalent to:
let { xs = e } in case xs of { (x:rest) -> if x==0 then rest else xs }
在你的函数中,你会写
checkTruths alla@(a:as) allb@(b:bs) = checkTrue a b (removeAbsoluteTrue alla allb): checkTruths as bs
关于haskell - 我可以将字符串切成头尾后用作整个字符串吗(:as) and using recursion on it in Haskell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70998734/