I have a list of Strings and I am trying to add a String at the end of the list in the following code but I am getting type matching errors:
eliminateImpl :: [String] -> String -> [String]
eliminateImpl [] _ = []
eliminateImpl (p:ps) r = if (contains (p:ps) "Impl")
then if (p == "Impl" )
then "Not " ++r++" Or "++ps -- TRYING TO CONCATENATE HERE
else let r = r++p
in eliminateImpl ps r
else (p:ps)
contains :: [String] -> String -> Bool
contains [_] [] = True
contains [] _ = False
contains (p:ps) c = if p == c
then True
else contains ps c
What the code actually does is that the function eleminateImpl takes a first order logic expression, for example: "eliminateImpl ["Q(y)","Impl","P(x)"] []" and it should remove the implication and modify the expression so that the output is: "eliminateImpl ["Not", "Q(y)"," Or ","P(x)"]
I tried r++p and r:p but both are not working. This is the error:
Couldn't match type ‘Char’ with ‘[Char]’
Expected type: [String] Actual type: [Char] In the first argument of ‘(++)’, namely ‘"Not "’ In the expression: "Not " ++ r ++ " Or " ++ ps In the expression: if (p == "Impl") then "Not " ++ r ++ " Or " ++ ps else let r = r ++ p in eliminateImpl ps r
Is there another way to do it?
If I understood you correctly, this seems to be close to what you want:
If I have misunderstood, please comment and I will change my answer.
To replace only one implication:
These functions should go through the list of Strings until they find the first
"impl". Nothing before the"impl"will be changed. If you want to change this the modifications should be trivial.