smlsmlnj

Syntax errors in main function - SML/NJ [deleting DO VAL, deleting VAL ID, SEMICOLON ID, deleting SEMICOLON END SEMICOLON]


May someone highlight to me why I am getting the syntax errors for the main function, so that I can fix it. I am quite new to the language. Actually I was introduced to it through the assignment, so I am totally lost as to how to refactor it to avoid the syntax error:

val IDs = [410021001,410021002,410021003,410021004,410021005,410021006,410021007,410021008,410021009,410021010];
val Names = ["Alan","Bob","Carrie","David","Ethan","Frank","Gary","Helen","Igor","Jeff"]: string list;
val HW1 = [90.0,85.0,90.0,117.0,85.0,90.0,117.0,117.0,117.0,117.0] : real list;
val HW2 = [84.5,49.0,110.5,85.0,56.0,65.0,65.0,59.5,50.0,50.0] : real list;
val HW3 = [117.0,117.0,117.0,0.0,65.0,117.0,50.0,51.0,75.0,75.0] : real list;
val Midterm = [60.0,57.0,6.0,44.0,72.0,43.0,54.0,75.0,53.0,75.0] : real list;
val Final = [66.0,64.0,62.0,55.0,66.0,75.0,75.0,75.0,75.0,75.0] : real list;


fun score(HW1, HW2, HW3, Midterm, Final) =
    round(HW1 * 0.1 + HW2 * 0.1 + HW3 * 0.1 + Midterm * 0.3 + Final * 0.4);


fun letterGrade(score) =
    if score >= 90 then "A+"
    else if score >= 85 then "A"
    else if score >= 80 then "A-"
    else if score >= 77 then "B+"
    else if score >= 73 then "B"
    else if score >= 70 then "B-"
    else if score >= 67 then "C+"
    else if score >= 63 then "C"
    else if score >= 60 then "C-"
    else if score >= 50 then "D"
    else "E";


val i = 0
val max = length(IDs)
fun main() = 
    while i < max do
        var ind_score = score(HW1[i], HW2[i], HW3[i], Midterm[i], Final[i])
        var grade = letterGrade(ind_score)
        print(IDs[i], "    ", Names[i], "    ", ind_score, "    ", grade)
        i = i + 1
    end
end

This is the error I am producing after running my programme, which shows that my errors start at this function: Terminal feedback


Solution

  • Part 1 - Straightforward corrections

    I'll go from the simplest to the most complex. I'll also provide a more functional implementation in the end, without the while loop.

    1. The construct var does not exist in ML. You probably meant val ind_score = ...
    2. Array indexing is not done by array[i]. You need (as with everything else) a function to do that. The function happens to be List.nth. So, everywhere you have HW1[i], you should have List.nth(HW1, i).
    3. Most language constructs expect a single expression, so you usually cannot simply string commands as we do in imperative languages. Thus, there are some constructs missing after the do in your while.
    4. Variables in functional languages are usually immutable by default, so you have to indicate when you want something to be mutable. In your while, you want i to be mutable, so it has to be declared and used as such: val i = ref 0. When using the value, you have to use the syntax !i to get the 'current' value of the variable (essentially, de-referencing it).
    5. The function call syntax in ML does not use (). When you call a function like score(a, b, c, d) what you are doing is creating a tuple (a, b, c, d) and passing it as a single argument to the function score. This is an important distinction because you are actually passing a tuple to your print function, which does not work because print expects a single argument of type string. By the way, the string concatenation operator is ^.

    If you do all these changes, you'll get to the following definition of main. It is quite ugly but we will fix that soon:

    val i = ref 0   (* Note that i's type is "int ref". Think about it as a pointer to an integer *)
    val max = length(IDs)
    fun main() =
        while !i < max do  (* Notice how annoying it is to always de-reference i and that the syntax to access a list element is not very convenient *)
                      let  (* The 'let in end' block is the way to define values that will be used later *)
                          val ind_score = score(List.nth(HW1, !i), List.nth(HW2, !i), List.nth(HW3, !i), List.nth(Midterm, !i), List.nth(Final, !i))
                          val grade = letterGrade(ind_score)
                      in (   (* The parenthesis here allow stringing together a series of "imperative" operations *)
                          print(Int.toString(List.nth(IDs, !i)) ^ "    " ^ List.nth(Names, !i) ^ "    " ^ Int.toString(ind_score) ^ "    " ^ grade ^ "\n");
                          i := !i + 1   (* Note the syntax := to re-define the value of i *)
                      )
                      end;
    

    Part 2 - Making it more functional

    Functional language programs are typically structured differently from imperative programs. A lot of small functions, pattern matching and recursion are typical. The code below is an example of how you could improve your main function (it is by no means "optimal" in terms of style though). A clear advantage of this implementation is that you do not even need to worry about the length of the lists. All you need to know is what to do when they are empty and when they are not.

    (* First, define how to print the information of a single student.
       Note that the function has several arguments, not a single argument that is a tuple *)
    fun printStudent id name hw1 hw2 hw3 midterm final =
        let
            val ind_score = score (hw1, hw2, hw3, midterm, final)
            val grade = letterGrade ind_score
        in
            print(Int.toString(id) ^ "    " ^ name ^ "    " ^ Int.toString(ind_score) ^ "    " ^ grade ^ "\n")
        end;
    
    (* This uses pattern matching to disassemble the lists and print each element in order.
       The first line matches an empty list on the first element (the others don't matter) and return (). Think of () as None in Python.
       The second line disassemble each list in the first element and the rest of the list (first::rest), print the info about the student and recurse on the rest of the list.
     *)
    fun printAllStudents (nil,     _,           _,         _,         _,         _,             _) = ()
      | printAllStudents (id::ids, name::names, hw1::hw1s, hw2::hw2s, hw3::hw3s, mid::midterms, final::finals) =
        (printStudent id name hw1 hw2 hw3 mid final;
         printAllStudents(ids, names, hw1s, hw2s, hw3s, midterms, finals));
    printAllStudents(IDs, Names, HW1, HW2, HW3, Midterm, Final);
    

    Note that it is a bit of a stretch to say that this implementation is more legible than the first one, even though it is slightly more generic. There is a way of improving it significantly though.

    Part 3 - Using records

    You may have noticed that there is a lot of repetition on the code above because we keep having to pass several lists and arguments. Also, if a new homework or test was added, several functions would have to be reworked. A way to avoid this is to use records, which work similarly to structs in C. The code below is a refactoring of the original code using a Student record. Note that, even though it has a slightly larger number of lines than your original code, it is (arguably) easier to understand and easier to update, if needed. The important part about records is that to access a field named field, you use an accessor function called #field:

    (* Create a record type representing a student *)
    type Student = {id:int, name:string, hw1:real, hw2:real, hw3:real, midterm:real, final:real};
    
    (* Convenience function to construct a list of records from the individual lists of values *)
    fun makeListStudents (nil,     _,           _,         _,         _,         _,             _) = nil (* if the input is empty, finish the list *)
      | makeListStudents (id::ids, name::names, hw1::hw1s, hw2::hw2s, hw3::hw3s, mid::midterms, final::finals) = (* otherwise, add one record to the list and recurse *)
        {id=id, name=name, hw1=hw1, hw2=hw2, hw3=hw3, midterm=mid, final=final} :: makeListStudents(ids, names, hw1s, hw2s, hw3s, midterms, finals);
    
    val students = makeListStudents (IDs, Names, HW1, HW2, HW3, Midterm, Final);
    
    fun score ({hw1, hw2, hw3, midterm, final, ...}: Student): int = (* Note the special patter matching syntax *)
        round(hw1 * 0.1 + hw2 * 0.1 + hw3 * 0.1 + midterm * 0.3 + final * 0.4);
    
    fun letterGrade (score) =
        if score >= 90 then "A+"
        else if score >= 85 then "A"
        else if score >= 80 then "A-"
        else if score >= 77 then "B+"
        else if score >= 73 then "B"
        else if score >= 70 then "B-"
        else if score >= 67 then "C+"
        else if score >= 63 then "C"
        else if score >= 60 then "C-"
        else if score >= 50 then "D"
        else "E";
    
    (* Note how this function became more legible *)
    fun printStudent (st: Student) =
        let
            val ind_score = score(st)
            val grade = letterGrade(ind_score)
        in
            print(Int.toString(#id(st)) ^ "    " ^ #name(st) ^ "    " ^ Int.toString(ind_score) ^ "    " ^ grade ^ "\n")
        end;
    
    (* Note how, now that we have everything in a single list, we can use map *)
    fun printAllStudents (students) = map printStudent students;
    printAllStudents(students);