0

I am new to SML. I tried to create and test the following function below, but I received an error. I do not know what is the problem.

fun isOld(pFirstTuple: int*int*int, pSecondTuple: int*int*int) = 
    if (#1 pFirstTuple) < (#1 pSecondTuple)
    then 
        true
    if (#1 pFirstTuple) = (#1 pSecondTuple)
    then 
        if (#2 pFirstTuple) < (#2 pSecondTuple)
        then
            true

    else
        false

I have tried this command "val p = isOld((8,9,10),(10,11,12))", but it showed me the following error Unbound variable or constructor. How do I fix this?

1 Answer 1

1

Here's what your code looks like, stripped down by ignoring various subexpressions (replacing them with A, B, and C)

if A
then true
if B
then if C
     then true
else false

You're making extensive use of if/then/else, but the syntax is not quite correct. In SML, every if must have both a then and else clause associated with it. Here's my guess at what you actually meant:

if A
then true
else if B
then if C
     then true
     else false
else false

This is starting to get quite messy---but you can clean it up with boolean logic. Notice, for example, that if X then true else false means exactly the same thing as simply writing X, because both expressions are type bool and will always evaluate to the same boolean, regardless of what X is. You can extend this reasoning to see that

  1. if X then true else Y is equivalent to X orelse Y.
  2. if X then Y else false is equivalent to X andalso Y.

With these ideas, we can clean up your code considerably:

A orelse (B andalso C)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.