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;








-1















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).










share|improve this question






























    -1















    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).










    share|improve this question


























      -1












      -1








      -1








      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).










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 22 at 16:21









      amalloy

      60.4k6107159




      60.4k6107159










      asked Mar 22 at 3:30









      Gusti AryaGusti Arya

      765923




      765923






















          2 Answers
          2






          active

          oldest

          votes


















          7














          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





          share|improve this answer






























            4














            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





            share|improve this answer

























              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
              );



              );













              draft saved

              draft discarded


















              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









              7














              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





              share|improve this answer



























                7














                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





                share|improve this answer

























                  7












                  7








                  7







                  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





                  share|improve this answer













                  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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 22 at 3:42









                  arrowdarrowd

                  23.2k45584




                  23.2k45584























                      4














                      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





                      share|improve this answer





























                        4














                        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





                        share|improve this answer



























                          4












                          4








                          4







                          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





                          share|improve this answer















                          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






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 22 at 3:52

























                          answered Mar 22 at 3:44









                          Adam SmithAdam Smith

                          35.5k73377




                          35.5k73377



























                              draft saved

                              draft discarded
















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                              용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                              155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해