How to get value from type The 2019 Stack Overflow Developer Survey Results Are InGetting started with HaskellHow can you do anything useful without mutable state?Large-scale design in Haskell?How can a time function exist in functional programming?Haskell: Types with parameters (edit: aka dependent types)Transforming functions of type `a -> b` into those of type `String -> String` in HaskellHow can I get the default value for a type?what is the type () :: () in haskell means?Assign some of fields from another array based on id and get final output based on value type?Utilizing map in haskell on function arguments
What is the meaning of Triage in Cybersec world?
Does a dangling wire really electrocute me if I'm standing in water?
Can someone be penalized for an "unlawful" act if no penalty is specified?
Can one be advised by a professor who is very far away?
What is the accessibility of a package's `Private` context variables?
Are there any other methods to apply to solving simultaneous equations?
Is there a symbol for a right arrow with a square in the middle?
How to deal with fear of taking dependencies
Who coined the term "madman theory"?
Right tool to dig six foot holes?
Return to UK after being refused entry years previously
What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?
Write faster on AT24C32
Does the shape of a die affect the probability of a number being rolled?
If I score a critical hit on an 18 or higher, what are my chances of getting a critical hit if I roll 3d20?
Aging parents with no investments
Identify This Plant (Flower)
Why can Shazam fly?
Building a conditional check constraint
Have you ever entered Singapore using a different passport or name?
"as much details as you can remember"
Why do we hear so much about the Trump administration deciding to impose and then remove tariffs?
Deal with toxic manager when you can't quit
Am I thawing this London Broil safely?
How to get value from type
The 2019 Stack Overflow Developer Survey Results Are InGetting started with HaskellHow can you do anything useful without mutable state?Large-scale design in Haskell?How can a time function exist in functional programming?Haskell: Types with parameters (edit: aka dependent types)Transforming functions of type `a -> b` into those of type `String -> String` in HaskellHow can I get the default value for a type?what is the type () :: () in haskell means?Assign some of fields from another array based on id and get final output based on value type?Utilizing map in haskell on function arguments
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have this type
type Card = (CardValue,Suite)
I want to write a function to show color based on Card.Suite value
colour :: Card -> Colour
colour card = if card.Suite == Spades then Red else Black
This code just illustrates what I want to achieve. I don't know how to actually get suite value from Card(CardValue,Suite).
haskell functional-programming
add a comment |
I have this type
type Card = (CardValue,Suite)
I want to write a function to show color based on Card.Suite value
colour :: Card -> Colour
colour card = if card.Suite == Spades then Red else Black
This code just illustrates what I want to achieve. I don't know how to actually get suite value from Card(CardValue,Suite).
haskell functional-programming
add a comment |
I have this type
type Card = (CardValue,Suite)
I want to write a function to show color based on Card.Suite value
colour :: Card -> Colour
colour card = if card.Suite == Spades then Red else Black
This code just illustrates what I want to achieve. I don't know how to actually get suite value from Card(CardValue,Suite).
haskell functional-programming
I have this type
type Card = (CardValue,Suite)
I want to write a function to show color based on Card.Suite value
colour :: Card -> Colour
colour card = if card.Suite == Spades then Red else Black
This code just illustrates what I want to achieve. I don't know how to actually get suite value from Card(CardValue,Suite).
haskell functional-programming
haskell functional-programming
edited Mar 22 at 16:21
amalloy
60.4k6107159
60.4k6107159
asked Mar 22 at 3:30
Gusti AryaGusti Arya
765923
765923
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The type keyword declares a new type synonim, not a new type. In your code a Card is just a tuple of CardValue and Suite.
To access pair's elements either use fst and snd:
colour card = if snd card == Spades then Red else Black
or pattern-match the argument:
colour (value,suite) = if suite == Spades then Red else Black
add a comment |
type Card = (CardValue, Suit) makes Card a type alias for a tuple of CardValue and Suit data. That might not be the best data structure (a data member using record syntax seems better) but it's trivial to do what you want:
data Suit = Spades | Clubs | Hearts | Diamonds
data Color = Red | Black
type Card = (CardValue, Suit)
getSuitColor :: Card -> Color
getSuitColor (_, Spades) = Black
getSuitColor (_, Clubs) = Black
getSuitColor _ = Red
Or you could use guards to do the same
getSuitColor :: Card -> Color
getSuitColor (_, suit)
| suit == Spades = Black
| suit == Clubs = Black
| otherwise = Red
However you'll have to use deriving Eq on your Suit to do that (since otherwise you can't == it!)
If this were me I'd probably use that record syntax I mentioned above
data Card rank :: CardValue
, suit :: Suit
Which then means you have to construct your Cards, as they're separate types.
myOldCard = (3, Spades)
myNewCard = Card 3 Spades
But it means you have ready-made lookups into them.
getColor :: Card -> Color
getColor = determineColor . suit
where
determineColor :: Suit -> Color
determineColor Spades = Black
determineColor Clubs = Black
determineColor _ = Red
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%2f55292493%2fhow-to-get-value-from-type%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The type keyword declares a new type synonim, not a new type. In your code a Card is just a tuple of CardValue and Suite.
To access pair's elements either use fst and snd:
colour card = if snd card == Spades then Red else Black
or pattern-match the argument:
colour (value,suite) = if suite == Spades then Red else Black
add a comment |
The type keyword declares a new type synonim, not a new type. In your code a Card is just a tuple of CardValue and Suite.
To access pair's elements either use fst and snd:
colour card = if snd card == Spades then Red else Black
or pattern-match the argument:
colour (value,suite) = if suite == Spades then Red else Black
add a comment |
The type keyword declares a new type synonim, not a new type. In your code a Card is just a tuple of CardValue and Suite.
To access pair's elements either use fst and snd:
colour card = if snd card == Spades then Red else Black
or pattern-match the argument:
colour (value,suite) = if suite == Spades then Red else Black
The type keyword declares a new type synonim, not a new type. In your code a Card is just a tuple of CardValue and Suite.
To access pair's elements either use fst and snd:
colour card = if snd card == Spades then Red else Black
or pattern-match the argument:
colour (value,suite) = if suite == Spades then Red else Black
answered Mar 22 at 3:42
arrowdarrowd
23.2k45584
23.2k45584
add a comment |
add a comment |
type Card = (CardValue, Suit) makes Card a type alias for a tuple of CardValue and Suit data. That might not be the best data structure (a data member using record syntax seems better) but it's trivial to do what you want:
data Suit = Spades | Clubs | Hearts | Diamonds
data Color = Red | Black
type Card = (CardValue, Suit)
getSuitColor :: Card -> Color
getSuitColor (_, Spades) = Black
getSuitColor (_, Clubs) = Black
getSuitColor _ = Red
Or you could use guards to do the same
getSuitColor :: Card -> Color
getSuitColor (_, suit)
| suit == Spades = Black
| suit == Clubs = Black
| otherwise = Red
However you'll have to use deriving Eq on your Suit to do that (since otherwise you can't == it!)
If this were me I'd probably use that record syntax I mentioned above
data Card rank :: CardValue
, suit :: Suit
Which then means you have to construct your Cards, as they're separate types.
myOldCard = (3, Spades)
myNewCard = Card 3 Spades
But it means you have ready-made lookups into them.
getColor :: Card -> Color
getColor = determineColor . suit
where
determineColor :: Suit -> Color
determineColor Spades = Black
determineColor Clubs = Black
determineColor _ = Red
add a comment |
type Card = (CardValue, Suit) makes Card a type alias for a tuple of CardValue and Suit data. That might not be the best data structure (a data member using record syntax seems better) but it's trivial to do what you want:
data Suit = Spades | Clubs | Hearts | Diamonds
data Color = Red | Black
type Card = (CardValue, Suit)
getSuitColor :: Card -> Color
getSuitColor (_, Spades) = Black
getSuitColor (_, Clubs) = Black
getSuitColor _ = Red
Or you could use guards to do the same
getSuitColor :: Card -> Color
getSuitColor (_, suit)
| suit == Spades = Black
| suit == Clubs = Black
| otherwise = Red
However you'll have to use deriving Eq on your Suit to do that (since otherwise you can't == it!)
If this were me I'd probably use that record syntax I mentioned above
data Card rank :: CardValue
, suit :: Suit
Which then means you have to construct your Cards, as they're separate types.
myOldCard = (3, Spades)
myNewCard = Card 3 Spades
But it means you have ready-made lookups into them.
getColor :: Card -> Color
getColor = determineColor . suit
where
determineColor :: Suit -> Color
determineColor Spades = Black
determineColor Clubs = Black
determineColor _ = Red
add a comment |
type Card = (CardValue, Suit) makes Card a type alias for a tuple of CardValue and Suit data. That might not be the best data structure (a data member using record syntax seems better) but it's trivial to do what you want:
data Suit = Spades | Clubs | Hearts | Diamonds
data Color = Red | Black
type Card = (CardValue, Suit)
getSuitColor :: Card -> Color
getSuitColor (_, Spades) = Black
getSuitColor (_, Clubs) = Black
getSuitColor _ = Red
Or you could use guards to do the same
getSuitColor :: Card -> Color
getSuitColor (_, suit)
| suit == Spades = Black
| suit == Clubs = Black
| otherwise = Red
However you'll have to use deriving Eq on your Suit to do that (since otherwise you can't == it!)
If this were me I'd probably use that record syntax I mentioned above
data Card rank :: CardValue
, suit :: Suit
Which then means you have to construct your Cards, as they're separate types.
myOldCard = (3, Spades)
myNewCard = Card 3 Spades
But it means you have ready-made lookups into them.
getColor :: Card -> Color
getColor = determineColor . suit
where
determineColor :: Suit -> Color
determineColor Spades = Black
determineColor Clubs = Black
determineColor _ = Red
type Card = (CardValue, Suit) makes Card a type alias for a tuple of CardValue and Suit data. That might not be the best data structure (a data member using record syntax seems better) but it's trivial to do what you want:
data Suit = Spades | Clubs | Hearts | Diamonds
data Color = Red | Black
type Card = (CardValue, Suit)
getSuitColor :: Card -> Color
getSuitColor (_, Spades) = Black
getSuitColor (_, Clubs) = Black
getSuitColor _ = Red
Or you could use guards to do the same
getSuitColor :: Card -> Color
getSuitColor (_, suit)
| suit == Spades = Black
| suit == Clubs = Black
| otherwise = Red
However you'll have to use deriving Eq on your Suit to do that (since otherwise you can't == it!)
If this were me I'd probably use that record syntax I mentioned above
data Card rank :: CardValue
, suit :: Suit
Which then means you have to construct your Cards, as they're separate types.
myOldCard = (3, Spades)
myNewCard = Card 3 Spades
But it means you have ready-made lookups into them.
getColor :: Card -> Color
getColor = determineColor . suit
where
determineColor :: Suit -> Color
determineColor Spades = Black
determineColor Clubs = Black
determineColor _ = Red
edited Mar 22 at 3:52
answered Mar 22 at 3:44
Adam SmithAdam Smith
35.5k73377
35.5k73377
add a comment |
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.
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%2f55292493%2fhow-to-get-value-from-type%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