我可以在将字符串切割成头尾 (a:as) 并在 Haskell 中对其使用递归后将其用作整个字符串吗?
can i use a string as a whole string after cutting it to head and tails (a:as) and using recursion on it in Haskell?
我正在尝试在我拥有的函数(仅使用一个字符)中使用字符串中的每个字符,但我也试图在同一个递归函数中使用相同的字符串作为一个整体来将它与单独的进行比较另一个字符串中的字符(使用 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-pattern,如 Haskell 2010 年报告第 3.17.1 节中的 documented。
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
我正在尝试在我拥有的函数(仅使用一个字符)中使用字符串中的每个字符,但我也试图在同一个递归函数中使用相同的字符串作为一个整体来将它与单独的进行比较另一个字符串中的字符(使用 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-pattern,如 Haskell 2010 年报告第 3.17.1 节中的 documented。
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