我正在嘗試在我擁有的函式中使用字串中的每個字符(僅使用一個字符),但我也嘗試在同一個遞回函式中將相同的字串作為一個整體使用,以將其與另一個字串中的單個字符進行比較(使用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]
uj5u.com熱心網友回復:
您需要一個 as-pattern,如Haskell 2010 報告的第 3.17.1 節中所述。
var@pat形式的模式稱為as-patterns,并允許使用var作為pat匹配的值的名稱。例如,
case e of { xs@(x:rest) -> if x==0 then rest else xs }相當于:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/427286.html
標籤:哈斯克尔
上一篇:具有型別級別Bool的GADT-使(&&)型別運算子暗示對其引數的約束
下一篇:Haskell中評估的順序
