Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Note</strong>: using an F# compiler here; I don't have an OCaml compiler on this computer.</p> <p>The last line of your <code>subst</code> function has an error: It should be as follows:</p> <pre><code>| L l :: t -&gt; L (subst a b l) :: (subst a b t) </code></pre> <p>So the complete code would look like this:</p> <pre><code>type 'a Sexp = | S of 'a | L of 'a Sexp list let rec subst (a) (b) (lst : 'a Sexp list) = match lst with | [] -&gt; lst | S s :: t -&gt; if s = a then (S b) :: (subst a b t) else (S s) :: (subst a b t) | L l :: t -&gt; L (subst a b l) :: (subst a b t) let test () = let (lst : int Sexp list) = [S 1; L [S 2; L [S 3]; S 4]; S 5] let a = 2 let b = 3 subst a b lst </code></pre> <p>The output of <code>test()</code> is</p> <pre><code>[S 1; L [S 3; L [S 3]; S 4]; S 5] </code></pre> <p>The reason is that your function <code>subst</code> returns a <code>'a Sexp list</code>. If you omit the <code>L</code> constructor from the last line, then <code>subst a b l</code> is of type <code>'a Sexp list</code>, which you are attempting to cons with another list of type <code>'a Sexp list</code>. This does not work.</p> <p>Nor was this your intention, since you wanted to end up with an entity of type <code>'a Sexp list</code>, which means you must cons an element of type <code>'a Sexp</code> with a list of type <code>'a Sexp list</code>. By specifying the <code>L</code> constructor, you are creating an element of type <code>'a Sexp list</code>, which you can now cons with the rest of the list.</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.
    2. 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