I am trying to create function setEqual.. This is what I have so far:
setEqual :: Eq a => [a] -> [a] -> Bool
setEqual [] _ = True
setEqual _ [] = False
setEqual a#(x:a') (y:b) | x == y = setEqual a' b
| otherwise = setEqual a b
setEqualTest = [ not ("ABCD" `setEqual` "ABC"),
not ("ABC" `setEqual` "ABCD"),
[0,2,1,3] `setEqual` [0,1,2,3] ]
When I run the test I get [True, False, False]. Am I doing this correctly?
I think you just need to get two things right for writing your recursive function (to check set equality here): the base case and the recursive case.
You base case is incorrect. You don't know that an empty set [] is equal to an arbitrary set _ unless it's also empty. So it should be:
setEqual [] [] = True
plus that empty set is not equal to anything else:
setEqual [] _ = False
setEqual _ [] = False
(Note that the setEqual [] [] ... line takes precedence as it is written before the others.
Your recursive case has a typo, and the last line should be:
| otherwise = setEqual a' b
Related
I have a data object for trees like this:
data Tree = Empty | Node Int [Tree] deriving (Show, Eq)
Here is my searching function:
searchValueTree :: Tree -> Int -> Bool
searchValueTree Empty _ = False
searchValueTree (Node a list) valueSearch
| a == valueSearch = True
| otherwise = helperTree list valueSearch
--help function
helperTree :: [Tree] -> Int -> Bool
helperTree [] _ = False
helperTree (x:xs) value = searchValueTree x value || helperTree xs value
test::Bool
test = searchValueTree (Node 5 [Node 4 [Node 3 [Empty]], Node 7 [Empty], Leer]) 3
The question is, when I'm in the helper function and I call searchValueTree x value and I haven't found my value, which is called first: helperTree list valueSearch in searchValueTree, or helperTree xs value in helperTree?
I can't figure out the order of execution.
It goes something like this:
searchValueTree x value || helperTree xs value
-> {- definition of || -}
case searchValueTree x value of
True -> True
False -> helperTree xs value
-> {- pattern match forces evaluation of the call to searchValueTree -}
case (case x of
Empty -> False
Node a list | a == value -> True
| otherwise -> helperTree list value
) of
True -> True
False -> helperTree xs value
-> {- assuming x matches Node a list and a /= value -}
case let Node _ list = x in helperTree list value of
True -> True
False -> helperTree xs value
I believe in your parlance this means that helperTree list valueSearch is called in searchValueTree before helperTree xs value is called in helperTree. From here, because the first argument to (||) has not yet reached a form that lets the pattern match choose between its branches, evaluation continues in the scrutinee of the case, namely in let Node _ list = x in helperTree list value.
I'm attempting to create a replace function and the code below creates the error described in the post title in the first line of non-commented code following it. I have no idea as to why this is happening, so any help would be greatly appreciated.
replace [] t r n = []
replace [] _ _ _ = []
replace xs _ _ 0 = xs
replace (x:xs) t r n
| x == t = r:(replace [xs] t r (n-1))
| otherwise x (replace [xs] t r n)
There are some problems here: all the parts of the definition should start at the same column, so you should unindent the lines after the first one. Furthermore you need to write a = after the otherwise. xs is a list, so you call replace with replace xs, not replace [xs]. For the last guard you are constructing a list, so x : (…), not x (…). The second clause also does not make much sense, since that is equivalent to the first one.
You thus can implement this as:
replace :: (Integral n, Eq a) => [a] -> a -> a -> n -> [a]
replace [] _ _ _ = []
replace xs _ _ 0 = xs
replace (x:xs) t r n
| x == t = r : replace xs t r (n-1)
| otherwise = x : replace xs t r n
I'm trying to learn haskell for a class, an I have a homework that requires me to compare a synonym type: Critere = Acteur -> Bool with the custom one Acteur.
I need to compare every acteur on a list to be sure they match every Critere on an other list... that's my code so far
data Acteur = Acteur Nom Sexe RevenuMinimum Date' Restriction ListeFilms
type Critere = Acteur -> Bool
instance Ord Acteur where
compare x y
| nomActeur x == nomActeur y = EQ
| nomActeur x < nomActeur y = LT
| otherwise = GT
eqActeur (Acteur nom1 _ _ _ _ _) (Acteur nom2 _ _ _ _ _ ) = nom1 == nom2
instance Eq Acteur where
(==) = eqActeur
This is what the teacher gave me realate to the type Acteur... that's my answer so far:
selectionActeursCriteres :: [Critere] -> [Acteur] -> [Acteur]
selectionActeursCriteres [] (x:xs) = (x:xs)
selectionActeursCriteres (x:xs) [] = []
selectionActeursCriteres (x:xs) (y:ys) = t where
t = selectionActeursCriteres tail (x:xs) [z | z <- ys, x == y]
I've tried to join the teacher like two days ago, and I'm still waiting...
Thanks you for your help in advance!
I know how to check if a string contains a certain character like this:
charFound :: Char -> String -> Bool
charFound c s = c `elem` s
Now, how could I have the same approach using recursion?
Also, using Pattern Matching to check if one of the parameters is empty, I'm getting
Parse Error In pattern: ' ' s
charFound:: Char->String->Bool
charFound '' s = error "Something is empty." -- ERROR
charFound c "" = error "Something is empty." -- OK
Am I allowed to use the _ to ignore a parameter with something that's not a list ?
Update Currently code
charFound :: Char->String->Bool
charFound c (x:xs)
| c == x = True
| xs == "" = False
| otherwise = contido c xs
Also
charFound :: Char->String->Bool
charFound _ "" = False -- returns false if I type > charFound 'a' "Leh"
charFound c (x:xs)
| c == x = True
| otherwise = contido c xs
We can keep the same signature, because the function does the same thing.
charFound :: Char -> String -> Bool
When you are performing recursion you always want to make sure you consider your base case, in this case it would be when you are looking for a character in an empty String, which should obviously always return false.
charFound c "" = False
Now you have to consider the other case, where the String is not empty. If the string is not empty, then it is of the form (x:xs), and if x equals our character then we return true, otherwise we check whether c is in xs.
charFound c (x:xs)
| c == x = True
| otherwise = charFound c xs
Alternatively,
charFound c (x:xs) = c == x || charFound c xs
Edit:
To answer your additional question about pattern matching, you are getting that warning because '' is not a character! A character can never be empty, so you don't need to consider that possibility.
Also, you definitely can use _ to match on something that is not a list. You can use _ to match any parameter you like. For example, the base case could have been written as
charFound _ "" = False
Because we don't actually need to know what the value of the character is when the String is empty, so we don't need to give it a name.
The following questions appear in a mock exam and i cannot understand how the answers came about.
Anyone care to derive a method of calculating the answer as a similar set of questions will appear in an exam tomorrow.
Match each function definition with the logical operation it implements
Question Correct Match Selected Match
funA x True = x
funA _ _ = False
-- Correct A. Logical AND
funB x False = x
funB x True = not x
-- Correct B. Exclusive OR
funC False _ = True
funC True x = x
-- Correct C. Logical IMPLICATION
funD x False = x
funD _ True = True
-- Correct D. Logical (Inclusive) OR
funE x True = x
funE x False = not x
-- Correct E. Logical EQUIVALENCE
The question is to match logical operations to functions implementing them.
One method is simply comparing the truth table for the logical connectives to
that of the functions.
For instance, AND:
x y x AND y
-----------------
F F F
F T F
T F F
T T T
matches
funA x True = x
funA _ _ = False
as
x y funA x y
-------------------------
F F F
F T x == False
T F F
T T x == True
and similarly for the rest.
Another option is to observe that x AND y is true iff x is true and y is true, and see which function satisfies that.