Haskell Pattern matching in a Function
The two-argument function check returns True exactly when both
Boolean operands have the same value. Otherwise it returns False.
I should implement it using :
check :: Bool -> Bool -> Bool
1)- Conditional expressions(checkA)
checkA x y = if x < y then False else True
-
2)-Guarded equations (checkB).
checkB x y | x < y = False | otherwise =True
3)-Pattern matching (checkc).
,but here I get an error
checkC x y False False = True
True True = True
False True = False
True False = False
How can I use Pattern matching to say (if this and that are equivalent then ...) when x and y are numbers(int)?
haskell
|
show 10 more comments
The two-argument function check returns True exactly when both
Boolean operands have the same value. Otherwise it returns False.
I should implement it using :
check :: Bool -> Bool -> Bool
1)- Conditional expressions(checkA)
checkA x y = if x < y then False else True
-
2)-Guarded equations (checkB).
checkB x y | x < y = False | otherwise =True
3)-Pattern matching (checkc).
,but here I get an error
checkC x y False False = True
True True = True
False True = False
True False = False
How can I use Pattern matching to say (if this and that are equivalent then ...) when x and y are numbers(int)?
haskell
Herex
andy
seem to beBool
s, and your condition does not look correct.
– Willem Van Onsem
Nov 11 at 17:42
As forInt
s, one can writeInt
literals, but since that domain is huge, it would really be a (very) bad idea.
– Willem Van Onsem
Nov 11 at 17:43
check :: Bool -> Bool -> Bool do you mean this ??
– Amerov
Nov 11 at 17:44
it's a homework and I have to do it today -_-
– Amerov
Nov 11 at 17:47
1
Ifx
andy
are booleans, you can simply enumerate all possible choices and you're done. With ints, you can't do this (because there are infinitely many of them), so pattern-matching is not an option.
– ForceBru
Nov 11 at 17:49
|
show 10 more comments
The two-argument function check returns True exactly when both
Boolean operands have the same value. Otherwise it returns False.
I should implement it using :
check :: Bool -> Bool -> Bool
1)- Conditional expressions(checkA)
checkA x y = if x < y then False else True
-
2)-Guarded equations (checkB).
checkB x y | x < y = False | otherwise =True
3)-Pattern matching (checkc).
,but here I get an error
checkC x y False False = True
True True = True
False True = False
True False = False
How can I use Pattern matching to say (if this and that are equivalent then ...) when x and y are numbers(int)?
haskell
The two-argument function check returns True exactly when both
Boolean operands have the same value. Otherwise it returns False.
I should implement it using :
check :: Bool -> Bool -> Bool
1)- Conditional expressions(checkA)
checkA x y = if x < y then False else True
-
2)-Guarded equations (checkB).
checkB x y | x < y = False | otherwise =True
3)-Pattern matching (checkc).
,but here I get an error
checkC x y False False = True
True True = True
False True = False
True False = False
How can I use Pattern matching to say (if this and that are equivalent then ...) when x and y are numbers(int)?
haskell
haskell
edited Nov 12 at 0:39
amalloy
58.8k5101153
58.8k5101153
asked Nov 11 at 17:40
Amerov
247
247
Herex
andy
seem to beBool
s, and your condition does not look correct.
– Willem Van Onsem
Nov 11 at 17:42
As forInt
s, one can writeInt
literals, but since that domain is huge, it would really be a (very) bad idea.
– Willem Van Onsem
Nov 11 at 17:43
check :: Bool -> Bool -> Bool do you mean this ??
– Amerov
Nov 11 at 17:44
it's a homework and I have to do it today -_-
– Amerov
Nov 11 at 17:47
1
Ifx
andy
are booleans, you can simply enumerate all possible choices and you're done. With ints, you can't do this (because there are infinitely many of them), so pattern-matching is not an option.
– ForceBru
Nov 11 at 17:49
|
show 10 more comments
Herex
andy
seem to beBool
s, and your condition does not look correct.
– Willem Van Onsem
Nov 11 at 17:42
As forInt
s, one can writeInt
literals, but since that domain is huge, it would really be a (very) bad idea.
– Willem Van Onsem
Nov 11 at 17:43
check :: Bool -> Bool -> Bool do you mean this ??
– Amerov
Nov 11 at 17:44
it's a homework and I have to do it today -_-
– Amerov
Nov 11 at 17:47
1
Ifx
andy
are booleans, you can simply enumerate all possible choices and you're done. With ints, you can't do this (because there are infinitely many of them), so pattern-matching is not an option.
– ForceBru
Nov 11 at 17:49
Here
x
and y
seem to be Bool
s, and your condition does not look correct.– Willem Van Onsem
Nov 11 at 17:42
Here
x
and y
seem to be Bool
s, and your condition does not look correct.– Willem Van Onsem
Nov 11 at 17:42
As for
Int
s, one can write Int
literals, but since that domain is huge, it would really be a (very) bad idea.– Willem Van Onsem
Nov 11 at 17:43
As for
Int
s, one can write Int
literals, but since that domain is huge, it would really be a (very) bad idea.– Willem Van Onsem
Nov 11 at 17:43
check :: Bool -> Bool -> Bool do you mean this ??
– Amerov
Nov 11 at 17:44
check :: Bool -> Bool -> Bool do you mean this ??
– Amerov
Nov 11 at 17:44
it's a homework and I have to do it today -_-
– Amerov
Nov 11 at 17:47
it's a homework and I have to do it today -_-
– Amerov
Nov 11 at 17:47
1
1
If
x
and y
are booleans, you can simply enumerate all possible choices and you're done. With ints, you can't do this (because there are infinitely many of them), so pattern-matching is not an option.– ForceBru
Nov 11 at 17:49
If
x
and y
are booleans, you can simply enumerate all possible choices and you're done. With ints, you can't do this (because there are infinitely many of them), so pattern-matching is not an option.– ForceBru
Nov 11 at 17:49
|
show 10 more comments
1 Answer
1
active
oldest
votes
You can pattern match against bools because there's a finite (and small) number of combinations. Consider the whole combination space of Bool
s (which is defined as n^n
where n
is the size of the space)
False False
False True
True False
True True
It's trivial to enumerate these by hand in check
check :: Bool -> Bool -> Bool
check False False = True
check False True = False
check True False = False
check True True = True
But as you can deduce, Int
s are not as trivial, since the space of all integers is literally infinite.
You can pattern match in this way with ints if you have an infinite amount of programming time and hard drive space, because you have an infinite amount of patterns to write.
check :: Int -> Int -> Bool
check 0 0 = True
check 1 1 = True
check (-1) (-1) = True
check 2 2 = True
check (-2) (-2) = True
-- etc literally until infinity
check _ _ = False
The way to write this is to either use guards:
check :: Int -> Int -> Bool
check x y | x == y = True
| otherwise = False
Or realize that there is a pretty standard function that already does this -- maybe you've heard of it? :-)
check :: Int -> Int -> Bool
check = (==)
but in your code the definition looks different (check :: Int -> Int -> Bool)and this a homework i can't change it :,( I'm given the definition and I should continue with it
– Amerov
Nov 11 at 18:32
@Amerov What's it supposed to say?check :: Bool -> Bool -> Bool
will only accept booleans (by definition) so the whole crux of your question (comparing integers to integers) is suspect :)
– Adam Smith
Nov 11 at 19:22
@Amerov Your last example (checkC
) is failing for the same reason. You've defined a two-parameter functioncheckC :: Bool -> Bool -> Bool
, then provided a four-parameter function (checkC x y boolA boolB
)
– Adam Smith
Nov 11 at 19:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251440%2fhaskell-pattern-matching-in-a-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can pattern match against bools because there's a finite (and small) number of combinations. Consider the whole combination space of Bool
s (which is defined as n^n
where n
is the size of the space)
False False
False True
True False
True True
It's trivial to enumerate these by hand in check
check :: Bool -> Bool -> Bool
check False False = True
check False True = False
check True False = False
check True True = True
But as you can deduce, Int
s are not as trivial, since the space of all integers is literally infinite.
You can pattern match in this way with ints if you have an infinite amount of programming time and hard drive space, because you have an infinite amount of patterns to write.
check :: Int -> Int -> Bool
check 0 0 = True
check 1 1 = True
check (-1) (-1) = True
check 2 2 = True
check (-2) (-2) = True
-- etc literally until infinity
check _ _ = False
The way to write this is to either use guards:
check :: Int -> Int -> Bool
check x y | x == y = True
| otherwise = False
Or realize that there is a pretty standard function that already does this -- maybe you've heard of it? :-)
check :: Int -> Int -> Bool
check = (==)
but in your code the definition looks different (check :: Int -> Int -> Bool)and this a homework i can't change it :,( I'm given the definition and I should continue with it
– Amerov
Nov 11 at 18:32
@Amerov What's it supposed to say?check :: Bool -> Bool -> Bool
will only accept booleans (by definition) so the whole crux of your question (comparing integers to integers) is suspect :)
– Adam Smith
Nov 11 at 19:22
@Amerov Your last example (checkC
) is failing for the same reason. You've defined a two-parameter functioncheckC :: Bool -> Bool -> Bool
, then provided a four-parameter function (checkC x y boolA boolB
)
– Adam Smith
Nov 11 at 19:24
add a comment |
You can pattern match against bools because there's a finite (and small) number of combinations. Consider the whole combination space of Bool
s (which is defined as n^n
where n
is the size of the space)
False False
False True
True False
True True
It's trivial to enumerate these by hand in check
check :: Bool -> Bool -> Bool
check False False = True
check False True = False
check True False = False
check True True = True
But as you can deduce, Int
s are not as trivial, since the space of all integers is literally infinite.
You can pattern match in this way with ints if you have an infinite amount of programming time and hard drive space, because you have an infinite amount of patterns to write.
check :: Int -> Int -> Bool
check 0 0 = True
check 1 1 = True
check (-1) (-1) = True
check 2 2 = True
check (-2) (-2) = True
-- etc literally until infinity
check _ _ = False
The way to write this is to either use guards:
check :: Int -> Int -> Bool
check x y | x == y = True
| otherwise = False
Or realize that there is a pretty standard function that already does this -- maybe you've heard of it? :-)
check :: Int -> Int -> Bool
check = (==)
but in your code the definition looks different (check :: Int -> Int -> Bool)and this a homework i can't change it :,( I'm given the definition and I should continue with it
– Amerov
Nov 11 at 18:32
@Amerov What's it supposed to say?check :: Bool -> Bool -> Bool
will only accept booleans (by definition) so the whole crux of your question (comparing integers to integers) is suspect :)
– Adam Smith
Nov 11 at 19:22
@Amerov Your last example (checkC
) is failing for the same reason. You've defined a two-parameter functioncheckC :: Bool -> Bool -> Bool
, then provided a four-parameter function (checkC x y boolA boolB
)
– Adam Smith
Nov 11 at 19:24
add a comment |
You can pattern match against bools because there's a finite (and small) number of combinations. Consider the whole combination space of Bool
s (which is defined as n^n
where n
is the size of the space)
False False
False True
True False
True True
It's trivial to enumerate these by hand in check
check :: Bool -> Bool -> Bool
check False False = True
check False True = False
check True False = False
check True True = True
But as you can deduce, Int
s are not as trivial, since the space of all integers is literally infinite.
You can pattern match in this way with ints if you have an infinite amount of programming time and hard drive space, because you have an infinite amount of patterns to write.
check :: Int -> Int -> Bool
check 0 0 = True
check 1 1 = True
check (-1) (-1) = True
check 2 2 = True
check (-2) (-2) = True
-- etc literally until infinity
check _ _ = False
The way to write this is to either use guards:
check :: Int -> Int -> Bool
check x y | x == y = True
| otherwise = False
Or realize that there is a pretty standard function that already does this -- maybe you've heard of it? :-)
check :: Int -> Int -> Bool
check = (==)
You can pattern match against bools because there's a finite (and small) number of combinations. Consider the whole combination space of Bool
s (which is defined as n^n
where n
is the size of the space)
False False
False True
True False
True True
It's trivial to enumerate these by hand in check
check :: Bool -> Bool -> Bool
check False False = True
check False True = False
check True False = False
check True True = True
But as you can deduce, Int
s are not as trivial, since the space of all integers is literally infinite.
You can pattern match in this way with ints if you have an infinite amount of programming time and hard drive space, because you have an infinite amount of patterns to write.
check :: Int -> Int -> Bool
check 0 0 = True
check 1 1 = True
check (-1) (-1) = True
check 2 2 = True
check (-2) (-2) = True
-- etc literally until infinity
check _ _ = False
The way to write this is to either use guards:
check :: Int -> Int -> Bool
check x y | x == y = True
| otherwise = False
Or realize that there is a pretty standard function that already does this -- maybe you've heard of it? :-)
check :: Int -> Int -> Bool
check = (==)
edited Nov 11 at 19:28
answered Nov 11 at 18:24
Adam Smith
33.2k53174
33.2k53174
but in your code the definition looks different (check :: Int -> Int -> Bool)and this a homework i can't change it :,( I'm given the definition and I should continue with it
– Amerov
Nov 11 at 18:32
@Amerov What's it supposed to say?check :: Bool -> Bool -> Bool
will only accept booleans (by definition) so the whole crux of your question (comparing integers to integers) is suspect :)
– Adam Smith
Nov 11 at 19:22
@Amerov Your last example (checkC
) is failing for the same reason. You've defined a two-parameter functioncheckC :: Bool -> Bool -> Bool
, then provided a four-parameter function (checkC x y boolA boolB
)
– Adam Smith
Nov 11 at 19:24
add a comment |
but in your code the definition looks different (check :: Int -> Int -> Bool)and this a homework i can't change it :,( I'm given the definition and I should continue with it
– Amerov
Nov 11 at 18:32
@Amerov What's it supposed to say?check :: Bool -> Bool -> Bool
will only accept booleans (by definition) so the whole crux of your question (comparing integers to integers) is suspect :)
– Adam Smith
Nov 11 at 19:22
@Amerov Your last example (checkC
) is failing for the same reason. You've defined a two-parameter functioncheckC :: Bool -> Bool -> Bool
, then provided a four-parameter function (checkC x y boolA boolB
)
– Adam Smith
Nov 11 at 19:24
but in your code the definition looks different (check :: Int -> Int -> Bool)and this a homework i can't change it :,( I'm given the definition and I should continue with it
– Amerov
Nov 11 at 18:32
but in your code the definition looks different (check :: Int -> Int -> Bool)and this a homework i can't change it :,( I'm given the definition and I should continue with it
– Amerov
Nov 11 at 18:32
@Amerov What's it supposed to say?
check :: Bool -> Bool -> Bool
will only accept booleans (by definition) so the whole crux of your question (comparing integers to integers) is suspect :)– Adam Smith
Nov 11 at 19:22
@Amerov What's it supposed to say?
check :: Bool -> Bool -> Bool
will only accept booleans (by definition) so the whole crux of your question (comparing integers to integers) is suspect :)– Adam Smith
Nov 11 at 19:22
@Amerov Your last example (
checkC
) is failing for the same reason. You've defined a two-parameter function checkC :: Bool -> Bool -> Bool
, then provided a four-parameter function (checkC x y boolA boolB
)– Adam Smith
Nov 11 at 19:24
@Amerov Your last example (
checkC
) is failing for the same reason. You've defined a two-parameter function checkC :: Bool -> Bool -> Bool
, then provided a four-parameter function (checkC x y boolA boolB
)– Adam Smith
Nov 11 at 19:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251440%2fhaskell-pattern-matching-in-a-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Here
x
andy
seem to beBool
s, and your condition does not look correct.– Willem Van Onsem
Nov 11 at 17:42
As for
Int
s, one can writeInt
literals, but since that domain is huge, it would really be a (very) bad idea.– Willem Van Onsem
Nov 11 at 17:43
check :: Bool -> Bool -> Bool do you mean this ??
– Amerov
Nov 11 at 17:44
it's a homework and I have to do it today -_-
– Amerov
Nov 11 at 17:47
1
If
x
andy
are booleans, you can simply enumerate all possible choices and you're done. With ints, you can't do this (because there are infinitely many of them), so pattern-matching is not an option.– ForceBru
Nov 11 at 17:49