[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Mode function in ML



Salam,
 
I was wondering if anyone could help me with this mode function in ML?
 

fun insert_place (a,_) = a;

fun mode nil = 0| mode (h::t) = let

fun sorted (x,y) (x',y') = if y>y' then (x,y) else (x',y')

fun item(nil,current,number_of) = sorted current number_of |

item (h::t, (x,y), number_of) = if h = x then item (t,(x,y+1),number_of)

else item (t,(h,1), sorted (x,y) number_of)

in insert_place (item(t, (h,1),(h,1))) end;

 

It is not 100% correct as the sorted function does not work properley!

 

I have found this code on an internet site but i do not understand how it works. I was told the steps to create a mode function in ml:

 

  1. Write a function to insert an item in the list, in the right place in the list.
  2. Write a function to sort the list and to identify if an item is in a list.
  3. Write a function to deduct the list.
  4. Write a function to count number of times an item appears in a list.
  5. Write a function to count number of times each item in one list appears in a second list.
  6. Write a function to identify maximum value in a list.
  7. Write a function to calculate Mode.

I am not very good at ml so i was unable to create my own mode function. Any help would be greatly appriciated.

 

Thank you