Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a hacky solution with some extra typing, but without backticks! I first posted this on reddit, if that's okay.</p> <p>I assume you have derived <code>Enum</code> for <code>Rank</code>.</p> <pre><code>data OF = OF ace :: OF -&gt; Suit -&gt; PokerCard ace _ s = PokerCard Ace s -- or point-free two :: OF -&gt; Suit -&gt; PokerCard two _ = PokerCard Two -- or with const three :: OF -&gt; Suit -&gt; PokerCard three = const (PokerCard Three) -- you get the idea! -- the rest in one line: four,five,six,seven,eight,nine,ten,jack,king :: OF -&gt; Suit -&gt; PokerCard [four,five,six,seven,eight,nine,ten,jack,king] = map (const . PokerCard) [Four .. King] -- now you can write pokerDeck = [ ace OF Spades, two OF Spades -- and so on ] </code></pre> <p>The OF datatype isn't strictly necessary, but prevents confusing (but very metal) stuff like <code>ace "Motorhead" Spades</code>. You can still write <code>ace undefined Spades</code>, there's really no way around that, I think.</p> <p>If <code>of</code> wasn't a keyword, you could even write <code>of = OF</code>.</p> <hr> <p>There is also an utterly evil hack to get rid of the 'of' entirely, and using numbers for cards:</p> <pre><code>{-# LANGUAGE FlexibleInstances #-} -- this goes on top of your file instance Num (Rank -&gt; Suit) where fromInteger n = (undefined : map Card[Ace .. King]) !! (fromInteger n) </code></pre> <p>Now <code>2 Spades :: Card</code> typechecks (but you need the explicit type!) and is what you think it is :-) However, I strongly advise you to not do this in serious code; but it looks kind of cool.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload