Guest User

Untitled

a guest
Jan 16th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.90 KB | None | 0 0
  1. open W3q6h
  2.  
  3. (*lfreq_: value, list, freqCounter*)
  4. let rec lfreq_ x l n = match l with
  5.                     | hd::tl ->
  6.                         if hd = x then
  7.                             lfreq_ x tl n+1
  8.                         else
  9.                             lfreq_ x tl n
  10.                     | _ -> n
  11.  
  12. (*lfreq: value, list*)
  13. let lfreq x l = lfreq_ x l 0
  14.  
  15. (*delete_: value, inList, outList*)
  16. let rec delete_ x li lo = match li with
  17.                     | hd::tl ->
  18.                         if hd != x then
  19.                             delete_ x tl (hd::lo)
  20.                         else
  21.                             delete_ x tl lo
  22.                     | _ -> lo (*removed reverse - not needed here*)
  23.  
  24. (*delete: value, list*)
  25. let delete x l = delete_ x l []
  26.  
  27. (*freq_: list, element, elementFreq*)
  28. let rec freq_ l e ef = match l with
  29.                 | hd::tl ->
  30.                             let cef = lfreq hd l in
  31.                             if cef > ef then
  32.                                 freq_ (delete hd l) hd cef
  33.                             else
  34.                                 freq_ (delete hd l) e ef
  35.                 | _ -> e
  36.  
  37. (*freq: list*)
  38. (*Returns the most frequent element in the list*)              
  39. let freq l = freq_ l 0 0
  40.  
  41. let _ = test freq
Add Comment
Please, Sign In to add comment