Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The most direct way is to just do it by hand:</p> <pre><code>fromList :: [Double] -&gt; Maybe OrbitElements fromList [ _epoch , _ecc , _distPeri , _incl , _longAscNode , _argPeri , _timePeri , _meanMotion , _meanAnomaly , _trueAnomaly , _semiMajorAxis , _distApo , _period ] = Just $ OrbitElements _epoch _ecc _distPeri _incl _longAscNode _argPeri _timePeri _meanMotion _meanAnomaly _trueAnomaly _semiMajorAxis _distApo _period fromList _ = Nothing </code></pre> <p>However, there is a slightly sexier way, which is to parse them element by element, which is less error-prone and more descriptive of what we are trying to do:</p> <p>First we define two parsers, one of which requests a new element from the list (or fails if the list is empty), and the second of which matches the end of the list (or fails if the list is not empty):</p> <pre><code>import Control.Applicative import Control.Monad import Control.Monad.Trans.State getElem :: StateT [Double] Maybe Double getElem = do s &lt;- get case s of [] -&gt; mzero x:xs -&gt; do put xs return x endOfList :: StateT [Double] Maybe () endOfList = do s &lt;- get case s of [] -&gt; return () _ -&gt; mzero </code></pre> <p>Now we can define <code>fromList</code> in Applicative style:</p> <pre><code>fromList' :: [Double] -&gt; Maybe OrbitElements fromList' = evalStateT $ OrbitElements &lt;$&gt; getElem &lt;*&gt; getElem &lt;*&gt; getElem &lt;*&gt; getElem &lt;*&gt; getElem &lt;*&gt; getElem &lt;*&gt; getElem &lt;*&gt; getElem &lt;*&gt; getElem &lt;*&gt; getElem &lt;*&gt; getElem &lt;*&gt; getElem &lt;*&gt; getElem &lt;* endOfList </code></pre>
    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. 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