OCaml provides wild card matching pattern when every other case fails:
let imply v = match v with
(true,false) -> false
| _ -> true;;
What's the equivalence in Haskell?
Better separate the function definition, like this
imply :: (Bool, Bool) -> Bool
imply (True, False) = False
imply _ = True
Now, whenever the pattern (True, False) is passed to imply, it will return False, on all other cases it will return True.
Also, what you have actually done is perfectly fine, if you are defining it in interactive shell, better watch out for the indentation errors.
Prelude> :{
Prelude| let imply v = case v of
Prelude| (True, False) -> False
Prelude| _ -> True
Prelude| :}
Prelude> imply (False, True)
True
Prelude> imply (True, False)
False
Prelude> imply (True, True)
True
Prelude> imply (False, False)
True
Related
I'm currently still getting to know more about Haskell, under the topic of Pattern Matching, what does it mean by _ && _ = False in Haskell? I came across this statement in one of the lecture slides, like what does it mean by the underscores? Thanks again!
The underscores are wildcards. That is, they match any value, but do not bind the value to a name. A common example of this usage is:
True && True = True
_ && _ = False
Underscores means a wildcard. It binds with any value. It thus means that for any value for the left and right operand for the given type of the function, it will fire that clause, and thus return False.
One can thus define (&&) :: Bool -> Bool -> Bool with:
(&&) :: Bool -> Bool -> Bool
True && True = True
_ && _ = False
The real implementation of (&&) is however lazy in its second parameter, and is implemented as [src]:
-- | Boolean \"and\"
(&&) :: Bool -> Bool -> Bool
True && x = x
False && _ = False
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
How to check the second argument between an operation of true and false in haskell? E.g, False && True will only check the first argument and then will stop the operation. Is there something like False & True in Haskell to check both arguments?
&& in the prelude is implemented as
(&&) :: Bool -> Bool -> Bool
True && x = x
False && _ = False
Which means in the case that the first argument is False the second
one is never evaluated. You could always implement your own version to have the behaviour that you want e.g.:
(.&&.) :: Bool -> Bool -> Bool
True .&&. x = x
False .&&. False = False
False .&&. True = False
In which the second second argument is evaluated in either case.
There is no good reason why you'd want this, but if you insist...
import Control.Parallel (pseq)
(&&!) :: Bool -> Bool -> Bool
a &&! b = b `pseq` (a&&b)
Usually, seq (which doesn't require any imports) will also suffice instead of pseq, but only the latter actually guarantees that b will be evaluated.
Of course, the semantics of this function will be exactly the same as && alone, it will just be slower in some cases.
I was mislead by the OP's subject "Operator & in haskell?"
For those who are here looking for (&) in Haskell
& is a reverse application operator in Data.Function
(&) :: a -> (a -> b) -> b
I'm a Haskell noob and while reading about Implications in Haskell Road I came to following puzzle.
verdict :: Bool -> Bool -> (Bool, String)
verdict p q | not result = (False, "LIAR")
| result && p = (True, "telling the truth")
| result = (True, "innocent unless proven guilty")
| otherwise = (False, "we shouldn't get here")
where result = (not p) || q
-- map (\x -> verdict (fst x == 1) (snd x == 1)) [(1,1),(1,0),(0,1),(0,0)]
Is there a tool that would warn me about the otherwise or other similar logical errors?
I think I would write this function a different way:
-- not result
verdict True False = (False, "LIAR")
-- result && p
verdict True True = (True , "telling the truth")
-- result
verdict False _ = (True , "innocent unless proven guilty")
verdict _ True = (True , "innocent unless proven guilty")
-- otherwise
verdict _ _ = (False, "we shouldn't get here")
Then not only is it obvious to humans which clauses can be left out (the last two), but also to the machine; ghc says this at its default warning level:
test.hs:2:5: Warning:
Pattern match(es) are overlapped
In an equation for ‘verdict’:
verdict _ True = ...
verdict _ _ = ...
Checking guard overlap in general is of course undecidable; and furthermore I don't know of a tool that will try to give an approximate answer.
This is probably a clearer expression of your intent:
implies :: Bool -> Bool -> Bool
p `implies` q = not p || q -- The backticks allow infix usage.
-- The assumption is that p `implies` q is a known fact.
verdict :: Bool -> Bool -> (Bool, String)
verdict p q = (result, remark)
where
result = p `implies` q
remark
| not result = "LIAR"
| p = "telling the truth"
| otherwise = "innocent until proven guilty"
Guards are syntax sugar for pattern matches on Bool values. For general tips on arranging pattern matches, see Daniel Wagner's answer.
Been trying to write my own Prelude, and to write QuickCheck properties along with it. Writing Prelude is going well, but in doing the QuickCheck I have managed to get blocked early. Here are snippets of the 2 files I have:
-- MyLude.hs
module MyLude where
import Prelude (Bool(..))
(&&) :: Bool -> Bool -> Bool
(&&) True a = a
(&&) _ _ = False
-- MyLudeTest.hs
module MyLudeTest where
import qualified MyLude as P
prop_test_and a b = a && b == a P.&& b
Then in ghci I run:
:load MyLudeTest.hs
:m +Test.QuickCheck
quickCheck prop_test_and
and get the following error:
*** Failed! Falsifiable (after 1 test):
False
False
What confuses me is that I have implemented (||) and implemented a quickcheck property for it that does pretty much the same thing the prop_test_and does, and that seems to have no problems. What am I missing?
This is just operator precedence at work. Your property is parsed as:
prop_test_and a b = a && (b == a) P.&& b
Precedence!
Prelude Test.QuickCheck> (False && False) == myand False False
True
Prelude Test.QuickCheck> False && False == myand False False
False
You're missing parens around your operators. Remember (&&) binds weaker (3) than (==) (4)