Anıl Aşık
Anıl Aşık

Reputation: 351

Having an issue with records and tuples in SML

I am trying to solve an exercise in SML like ;

Write an ML program to be used for a multiple choice exam containing 10 questions. Your program should include the following:

o The answer key of the exam e.g.

  val key= “adabcbaadb”;

o Type definition for a record (info) which contains name and answers of a student. e.g. {name=”Ali”,ans=”abadccdadb”}

o Type definition for a tuple (result) which contains name and score of a student. e.g. ("Ali",60)

o Write as many functions as needed to take a list of records of type info containing the information of 3 students, calculate their scores, and convert them into a list of tuples of type result. You can write other supporting functions as well

e.g.

-  val stuInfo:info list=[{name=”Ali”,ans=”abadccdadb”},
                             {name=”Ege”,ans=”cbbdacabda”},
                             {name=”Can”,ans=”adabcbaadb”}];

        - val results = calculate(stuInfo);
 val results = [("Ali",60),("Ege",20),("Can",100)] : result list

o Write as many functions as needed to calculate the average score, and return the students who received a score above the average. e.g.

-val aboveList=aboveAvg(results);
       val aboveList = [("Ali",60),("Can",100)] : result list

Notes:  Make sure you give the types of your parameters and return value in all your functions.  Paranthesize your expressions.  Use explode function which converts a string to a character array e.g.

 - explode "abc";
        val it = [#"a",#"b",#"c"] : char list

My written code for this exercise is below ; but my code is not working :/ Where is my mistake ?

val answer_key="ddacbadbca";
type student_information={s_name:string,s_choice:string};
type student_result=string*int;
val student:info list=[{s_name="David",s_choice="adcbbaccad"},{s_name="John",s_choice="ccdabdbbcc"},{s_name="Alice",s_choice="abdaccacdb"}];
val 3studentsResult:student_result=average(student_information);
fun average ((h::t):student_information list):student_result list=student_score(explode"#ans h",explode"key")::average(t);
val sum=0;
fun student_score(((a::b,c::d):'a list):'a list) = (if(a=c) then sum=sum+10 else sum=sum+0 )::student_score(b,d);

Thanks

Upvotes: 2

Views: 547

Answers (1)

Joomy Korkut
Joomy Korkut

Reputation: 514

There are multiple issues about this code. Before starting, I should recommend you to work on your styling, you can use some spaces and extra lines between functions to see what you are doing easily, like this:

val answer_key = "ddacbadbca";

type student_information = {s_name:string, s_choice:string};
type student_result = string * int;

val student : student_information list =
  [{s_name="David", s_choice="adcbbaccad"},
   {s_name="John",  s_choice="ccdabdbbcc"},
   {s_name="Alice", s_choice="abdaccacdb"}];

You might also want to keep your variable and type names consistent. I think the convention is to use snake case (like snake_case) for type names and camel case (like camelCase) for variable and function names.

Here are some of your actual mistakes:

For your student variable, you give the type info list, which doesn't exist in your program. I assume your student_information variable used to be named info, so you should change one of those to the other.

You have a variable named 3studentsResult. Variables cannot start with numbers, they have to start with letters. You have to rename that variable, to something like threeStudentsResult.

In your 3studentsResult variable, you are using the average function that you define later. That doesn't work in Standard ML. You should define a function before using it. (Unless you need mutual recursion, you can use the and keyword then, but it's irrelevant to your problem.) Therefore, define average before 3studentsResult, and define student_score before average.

Even after fixing these, your student_score function is incorrect. I assume sum=sum+10 means that you are trying to change sum's value, like you would do in an imperative programming language. However, Standard ML is a functional language and changing values of variables is something you should avoid in functional programming. In fact, when I talk about "variables" in SML, I mean value declarations that cannot be changed. I recommend you to think about your problem a little bit more and maybe you can divide your problem into sub-problems. Good luck.

Upvotes: 1

Related Questions