Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>StandardML is a strict, statically typed language. Therefore, you can't have a function which accepts an int in the first case and a string in the second. The error you get is</p> <pre><code>this clause: string * string -&gt; 'Z previous clauses: int * string -&gt; 'Z in declaration: firstStr = (fn (0,&lt;pat&gt; : string) =&gt; n | (&lt;pat&gt; : string,&lt;pat&gt; : string) =&gt; if &lt;exp&gt; &gt; &lt;exp&gt; then n else b) </code></pre> <p>If you want to have one case which is a string and one case which is an int, you can make a new type, a "<a href="http://en.wikipedia.org/wiki/Tagged_union" rel="noreferrer">tagged union</a>" (aka "discriminated union"), which is designed to be easy to use with pattern matching. It would look like this:</p> <pre><code>datatype Wrapper = Int of int | String of string fun firstStr(Int 0, n:string) = n | firstStr(String b, n:string) = if b&gt;n then n else b </code></pre> <p>Of course, you might want to find some more appropriate name for this Wrapper type, something that makes sense in the context of your programme. Also please note that the type annotation on <code>n</code> is not really necessary; it would be more idiomatic to write</p> <pre><code>fun firstStr(Int 0, n) = n | firstStr(String b, n) = if b&gt;n then n else b </code></pre> <p>Additionally, the compiler will tell you you have left a case uncovered: What if the first argument is an integer not equal to zero?</p> <p>Finally, it's not really clear what you mean by the comparison <code>b&gt;n</code>, what aspect of the two strings did you want to compare? I see that when I compare two strings in SML, I see a lexicographic (aka alphabetic) comparison. Is that what you wanted?</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.
    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