Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You want to traverse the whole data structure and change some items here and there. This is usually done by a function that takes the data structure as a parameter and returns the new, changed version of the structure.</p> <p>For every case of input this function defines how the new value that is returned should look like.</p> <p>The basic function that modifies a <code>Tree</code> (which is just a list of <code>DataA</code> values) probably should just return a new list of modified values. If we defer those modifications of the values to a <code>modifyA</code> function, the main modification function looks like this:</p> <pre><code>-- # function to change a |Tree| mutate :: Tree -&gt; Tree mutate as = map mutateA as -- # (The |map| function applies the |mutateA| function to every -- # element of |as|, creating a list of all the return values) </code></pre> <p>Now the <code>mutateA</code> function needs to be defined to change all possible <code>DataA</code> values, and best it is accompanied by a <code>mutateB</code> function that handles <code>DataB</code> values.</p> <p>These functions look at the different possible cases of values and return the appropriate new values: </p> <pre><code>-- # function to change |DataA| items mutateA :: DataA -&gt; DataA -- # A |DataA1| is a |DataA1| with modified values mutateA (DataA1 bs) = DataA1 (map mutateB bs) -- # A |DataA3| is a |DataA3| with modified values mutateA (DataA3 s as) = DataA3 s (map mutateA as) -- # In the remaining case(s) the value stays the same mutateA d = d -- # function to change |DataB| items mutateB :: DataB -&gt; DataB mutateB (DataB1 as) = DataB1 (map mutateA as) mutateB (DataB3 s bs) = DataB3 s (map mutateB bs) -- # Here comes a real change mutateB (DataB2 _) = DataB2 "foo" </code></pre> <p>This way for every element in the tree a new element is computed, where all the <code>DataB2</code> values anywhere in the tree are replaced by "foo".</p> <p>It's relatively verbose because you have five different cases that contain a list of values that needs to be walked through, but that is not specific to Haskell. In an imperative language you would usually have five for loops in place of the five calls to <code>map</code>.</p> <p>Maybe you could simplify your data structure to reduce this "overhead". This of course depends on your actual use case, but maybe, for example, you don't need the <code>Data2</code> cases: Is there a difference between <code>DataA2 "abc"</code> and <code>DataA3 "abc" []</code>?</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.
 

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