Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe the following example should be close to what you had in mind. First we declare type-level natural numbers. Then we define vectors, which carry their length as a phantom type (see <a href="http://www.haskell.org/pipermail/haskell/2005-May/015815.html" rel="nofollow">Fixed-length vectors in Haskell, Part 1: Using GADTs</a>). And then we define a structure for nested lists of lists of ... which carries the depth as a phantom type. Finally we can define correctly typed <code>rGroupBy</code>.</p> <pre class="lang-hs prettyprint-override"><code>{-# LANGUAGE GADTs #-} {-# LANGUAGE EmptyDataDecls #-} import Data.List (groupBy) data Zero data Succ n data Vec n a where Nil :: Vec Zero a Cons :: a -&gt; Vec n a -&gt; Vec (Succ n) a data LList n a where Singleton :: a -&gt; LList Zero a SuccList :: [LList n a] -&gt; LList (Succ n) a -- Not very efficient, but enough for this example. instance Show a =&gt; Show (LList n a) where showsPrec _ (Singleton x) = shows x showsPrec _ (SuccList lls) = shows lls rGroupBy :: Vec n (a -&gt; a -&gt; Bool) -&gt; [a] -&gt; LList (Succ n) a rGroupBy Nil = SuccList . map Singleton rGroupBy (Cons f fs) = SuccList . map (rGroupBy fs) . groupBy f -- TEST ------------------------------------------------------------ main = do let input = ["1.1", "1.2.1", "1.2.2", "2.1", "2.2", "3"] -- don't split anything print $ rGroupBy Nil input -- split on 2 levels print $ rGroupBy (Cons (deweyGroup 1) (Cons (deweyGroup 2) Nil)) input where deweyGroup :: Int -&gt; String -&gt; String -&gt; Bool deweyGroup i a b = a!!idx == b!!idx where idx = 2*(i-1) </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.
    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