PDA

View Full Version : Standard ML programming


edeita2
04-10-2004, 01:10 PM
Hi, I`m totally new to ML.
The problem: create a student type and two student records:
Lastname id gpa
----------- -- ----
Smith 12345 3.6
Jones 98765 3.2
write a function that takes two student records and returns the last name with the higher Gpa. Here`s my way of solution: (using putty)

- type student = {lastname : string, id : int, gpa : real};
type student = {gpa:real, id:int, lastname:string}
- val r1 : student = {lastname = "Smith", id = 12345, gpa = 3.6};
val r1 = {gpa=3.6,id=12345,lastname="Smith"} : student
- val r2 : student = {lastname = "Jones", id = 98765, gpa = 3.2};
val r2 = {gpa=3.2,id=98765,lastname="Jones"} : student
- fun getlastname(r1 : student, r2 : student) = if (#gpa(r1) > #gpa(r2)) then #lastname(r1) else #lastname(r2);
val getlastname = fn : student * student -> string
- getlastname( #gpa(r1), #gpa(r2));
stdIn:50.1-50.33 Error: operator and operand don't agree [tycon mismatch]
operator domain: student * student
operand: real * real
in expression:
getlastname ((fn <pat> => <exp>) (r1),(fn <pat> => <exp>) (r2))
I appreciate if anyone tells me where I go wrong! thanks

Smerdyakov
04-11-2004, 01:36 AM
Do you understand what the error message means?

edeita2
04-11-2004, 12:20 PM
Originally posted by Smerdyakov
Do you understand what the error message means?
I think I have realized that I was using wrong parameters and I have corrected it.
It should be getlastname(r1, r2) which is yielding the correct answer.