Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Main problem is you wish to have permutation instead single value from <code>view</code>. We have only one type which allow permutation - record.</p> <p>So, we can add new data, record type</p> <pre><code>data B = F|T -- just shorter name for Bool in patterns data Palette = P {isW, isU, isB, isR, isG :: B} bool2b :: Bool -&gt; B bool2b True = T bool2b False = F viewColors :: Card -&gt; Palette viewColors (Card colors) = let m = bool2b . (`member` colors) in P {isW = m W, isU = m U, isB = m B, isR = m R, isG = m G} foo :: Card -&gt; String foo (viewColors -&gt; P {isW=T, isB=T}) = "card is white and black" foo _ = "whatever" </code></pre> <hr> <p><strong>UPDATED</strong></p> <p>We also could <strong>deny</strong> wrong patterns. But this solution is more ugly, but it allow use "classic" patterns</p> <pre><code>{-# LANGUAGE GADTs #-} {-# LANGUAGE EmptyDataDecls #-} {-# LANGUAGE RankNTypes #-} data Color = W | U | B | R | G deriving (Eq) data W' data U' data B' data R' data G' data Color' a where W' :: Color' W' U' :: Color' U' B' :: Color' B' R' :: Color' R' G' :: Color' G' data M a = N | J a -- just shorter name for Maybe a in patterns data Palette = Palette (M (Color' W')) (M (Color' U')) (M (Color' B')) (M (Color' R')) (M (Color' G')) </code></pre> <p>and define <code>viewColor</code>:</p> <pre><code>viewColors :: Card -&gt; Palette viewColors (Card colors) = let m :: Color -&gt; Color' a -&gt; M (Color' a) m c e = if c `member` colors then J e else N in P (m W W') (m U U') (m B B') (m R R') (m G G') foo :: Card -&gt; String foo (viewColors -&gt; Palette (J W') N (J B') N N) = "card is white and black" foo _ = "whatever" </code></pre>
    singulars
    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.
 

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