Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>At first glance, I was going to suggest <code>toList</code> and <code>fromList</code>. It's more code, but it enables elegant composition in the end.</p> <pre><code>toList :: QT a -&gt; [QT a] toList (Q w x y z) = [w,x,y,z] fromList :: [QT a] -&gt; QT a fromList [w,x,y,z] = Q w x y z listOpOnQT :: ([QT a] -&gt; [QT a]) -&gt; QT a -&gt; QT a listOpOnQT _ (C a) = C a listOpOnQT f q = fromList . map (listOpOnQT f) . f . toList $ q flipv :: QT a -&gt; QT a flipv = listOpOnQT reverse </code></pre> <p>Loosely tested in ghci</p> <pre><code>ghci&gt; let q = Q (Q (C 1) (C 2) (C 3) (C 4)) (C 22) (C 33) (C 44) ghci&gt; q Q (Q (C 1) (C 2) (C 3) (C 4)) (C 22) (C 33) (C 44) ghci&gt; flipv q Q (C 44) (C 33) (C 22) (Q (C 4) (C 3) (C 2) (C 1)) </code></pre> <p>You can easily make 'sort' work on your QT structure as well, now.</p> <pre><code>import Data.List (sort) instance (Ord a) =&gt; Ord (QT a) where compare (C x) (C y) = x `compare` y compare (C x) _ = LT compare _ (C x) = GT compare _ _ = EQ sortv :: (Ord a) =&gt; QT a -&gt; QT a sortv = listOpOnQT sort </code></pre> <p>Tested as part of the previous ghci session...</p> <pre><code>ghci&gt; sortv it Q (C 22) (C 33) (C 44) (Q (C 1) (C 2) (C 3) (C 4)) ghci&gt; sortv q Q (C 22) (C 33) (C 44) (Q (C 1) (C 2) (C 3) (C 4)) </code></pre> <p>Notice sorting the flipped q and just plain q both came out with the same result (therefore the sorting probably works! yay). You might want to pick a better implementation of <code>compare</code>, I just threw that one together to see stuff happen.</p> <hr> <p><strong>So how does it work?</strong></p> <p>The magic sauce, as you might have guessed, is <code>listOpOnQT</code>. In the non-trivial case, it turns the QT structure into a list, applies the listy function to the list, maps the <em>lifted</em> listy function on all elements of the list, and then pulls the list back into a QT structure. A better name for <code>listOpOnQT</code> might be <code>liftQT</code>, though it only works for a very special kind of function...</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. This table or related slice is empty.
    1. 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