Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The short answer is no. When programming in Haskell you should think of your data structures as pure mathematical objects and not worry about how they're represented in memory. The reason for this is that, in the absence of side-effects, there's really nothing <em>to</em> data except the functions that create it and the functions you can use to extract the simpler parts out of which it was constructed.</p> <p>To see information about data constructors like <code>(:)</code>, or any other terms, use the <code>:type</code> (or just <code>:t</code> for short) command inside GHCi:</p> <pre><code>:Prelude&gt; :type (:) (:) :: a -&gt; [a] -&gt; [a] </code></pre> <p>That tells you that the <code>(:)</code> constructor (pronounced "cons"), takes a value of any type, and a list of the same type, and returns a list of the same type. You can also get a bit more info by using the <code>:info</code> command. This will show you what the data definition looks like:</p> <pre><code>Prelude&gt; :info (:) data [] a = ... | a : [a] -- Defined in GHC.Types infixr 5 : </code></pre> <p>This tells you that <code>(:)</code> is the constructor which prepends an element to an existing list.</p> <p>I also highly recommend <a href="http://www.haskell.org/hoogle/" rel="nofollow">Hoogle</a> not only for looking up things by name but for doing the reverse kind of search; where you know the signature of the function you're looking for and want to find if someone's already written it for you. Hoogle is nice because it gives descriptions and example usages.</p> <h3>Shapes of Inductive Data</h3> <p>I said above that it's not important to know the representation of your data in memory... you should, however, understand the <em>shape</em> of the data you're dealing with, to avoid poor performance decisions. All data in Haskell is inductively defined, meaning it has a tree-like shape which unfolds ever outwards recursively. You can tell the shape of data by looking at its definition; there's really nothing hidden about its performance characteristics once you know how to read this:</p> <pre><code>data MyList a = Nil | Cons a (MyList a) </code></pre> <p>As you can see from the definition, the only way you can get a new <code>MyList</code> is by the <code>Cons</code> constructor. If you use this constructor multiple times, you'll end up with something roughly of this shape:</p> <pre><code>(Cons a5 (Cons a4 (Cons a3 (Cons a2 (Cons a1 Nil))))) </code></pre> <p>It's just a tree with no branches, that's the definition of a list! And the only way to get at <code>a1</code> is by popping off each of the <code>Cons</code>s in turn; hence access to the last element is <strong>O(n)</strong>, whereas access to the head is constant time. Once you can do this kind of reasoning about data structures based on their definitions, you're all set.</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