Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a number of useful examples of SML functors. They are made on the following premise: If you can do one set of things, this enables you to do another set of things.</p> <p><strong>A functor for sets:</strong> If you can compare elements, you can create sets using balanced data structures (e.g. binary search trees or other kinds of trees).</p> <pre><code>signature SET = sig type elem type set val empty : set val singleton : elem -&gt; set val union : set -&gt; set -&gt; set val intersect : set -&gt; set -&gt; set end signature ORD = sig type t val compare : t * t -&gt; order end functor BalancedSetFunctor(structure Cmp : ORD) :&gt; SET = struct type elem = Cmp.t type set = ... val empty = ... fun singleton x = ... fun union s1 s2 = ... fun intersect s1 s2 = ... end </code></pre> <p><strong>A functor for iteration:</strong> For any kind of collection of things (e.g. lists), if you can iterate them, you can automatically fold them. You can also create different structures for different ways to fold across the same datatype (e.g. pre-order, in-order and post-order traversal of trees).</p> <pre><code>signature ITERABLE = sig type elem type collection val next : collection -&gt; (elem * collection) option end signature FOLD = sig type elem type collection val fold : (elem * 'b -&gt; 'b) -&gt; 'b -&gt; collection -&gt; 'b end functor FoldFunctor(Iter : ITERABLE) :&gt; FOLD = struct type elem = Iter.elem type collection = Iter.collection fun fold f e xs = case Iter.next xs of NONE =&gt; e | SOME (x, xs') =&gt; fold f (f (x, e)) xs' end </code></pre>
    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.
    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