Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The simplest option is to make Board an instance of <code>Bounded</code> (can be auto derived as well), and use the following helper functions:</p> <pre><code>next :: (Enum a, Bounded a) =&gt; a -&gt; a next = turn 1 prev :: (Enum a, Bounded a) =&gt; a -&gt; a prev = turn (-1) turn :: (Enum a, Bounded a) =&gt; Int -&gt; a -&gt; a turn n e = toEnum (add (fromEnum (maxBound `asTypeOf` e) + 1) (fromEnum e) n) where add mod x y = (x + y + mod) `rem` mod </code></pre> <p>Example Use:</p> <pre><code>&gt; next H2 G0 &gt; prev G0 H2 &gt; next F1 F2 </code></pre> <p>(inspired by the the thread at <a href="http://www.mail-archive.com/haskell-cafe@haskell.org/msg37258.html" rel="noreferrer">http://www.mail-archive.com/haskell-cafe@haskell.org/msg37258.html</a> ).</p> <p>If you really need to use <code>succ</code> and <code>pred</code> instead, I don't believe there is any laws regarding implementations of <code>Enum</code> such that <code>succ (succ x) /= x</code> for all <code>x</code> (even though that is how most work). Therefore you could just write a custom implementation of <code>Enum</code> for your type that exhibits the wraparound you desire:</p> <pre><code>instance Enum Board where toEnum 0 = G0 toEnum 1 = A1 ... toEnum 40 = H2 toEnum x = toEnum (x `mod` 40) fromEnum G0 = 0 fromEnum A1 = 1 ... fromEnum H2 = 40 </code></pre> <p>That is very tedious to implement though. Also, the type shouldn't also implement <code>Bounded</code> when using a circular definition of <code>Enum</code>, as that breaks a rule regarding <code>Bounded</code> that <code>succ maxBound</code> should result in a runtime error.</p>
    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. 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