Reputation: 133
I am trying to use every character in the string in a function i have (that uses only one Char) but i am also trying to use that same string as a whole in the same recursive function to compare it to indvidual characters in another string (using elem
). Is there a way i can use that string heads and tails and also the whole string, so that the string will not be cut after every recursion?
Code:
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]
Upvotes: 2
Views: 67
Reputation: 531718
You want an as-pattern, as documented in Section 3.17.1 of the Haskell 2010 report.
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 }
In your function, you'd write
checkTruths alla@(a:as) allb@(b:bs) = checkTrue a b (removeAbsoluteTrue alla allb): checkTruths as bs
Upvotes: 5