Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you represent a (binary) tree with some form of nodes, we have several possible ways to represent them in Common Lisp.</p> <p>We could define a node structure:</p> <pre><code>(defstruct node left right) </code></pre> <p>Then we have a function <code>MAKE-NODE</code>.</p> <p>We could define a node class:</p> <pre><code>(defclass node () (left right)) </code></pre> <p>Nodes would be made with <code>(make-instance 'node)</code>.</p> <p>We could make nodes with lists:</p> <pre><code>(defun make-node (left right) (list left right)) </code></pre> <p>Above uses two cons cells.</p> <p>We could make nodes for a binary tree a bit smaller with just one cons cell:</p> <pre><code>(defun make-node (left right) (cons left right)) </code></pre> <p>We could make nodes as a vector:</p> <pre><code>(defun make-node (left right) (vector left right)) </code></pre> <p>There are many possibilities. If the node should also have some other information, then the single cons cell approach is not enough. Something like a structure or class is fine, since the data item can have more information and the object knows its type: we can easily ask <code>(node-p something)</code> and get a useful answer.</p> <p>The style rules for choosing the right data structure for any slightly more advanced software you want to write:</p> <ul> <li>by default use CLOS</li> <li>if CLOS is too slow (for example slot access is too slow), then try to optimize CLOS</li> <li>if CLOS is still too slow, then use structures. Slot access should be faster</li> <li>try to avoid representing data structures as untyped lists or cons trees</li> </ul>
    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.
    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