fork(2) download
  1. fun max lst =
  2. foldl (fn (e, max) => Int.max(e, max)) (hd lst) (tl lst)
  3.  
  4. fun count_gaps lst=
  5. case lst of
  6. [] => 0
  7. | [_] => 0
  8. | head::neck::tail => (neck-head-1) + count_gaps(neck::tail)
  9.  
  10. fun insert_sorted(lst, vs) =
  11. let fun insert_v(lst, v) =
  12. case lst of
  13. [] => [v]
  14. | head::tail => if v<=head then v::head::tail else head::insert_v(tail, v)
  15. in
  16. case vs of
  17. [] => lst
  18. | h::t => insert_v(insert_sorted(lst, t), h)
  19. end
  20.  
  21. fun filter_indexes f lst =
  22. let fun help f lst c =
  23. case lst of
  24. [] => []
  25. | h::t => if f(h) then c::(help f t (c+1)) else help f t (c+1)
  26. in
  27. help f lst 0
  28. end
  29.  
  30. fun compute input =
  31. let
  32. val max = max(input)
  33. fun helper(n) =
  34. case n of
  35. 0 => 0
  36. | _ => count_gaps(filter_indexes (fn e=>e>=n) input) + helper(n-1)
  37. in
  38. helper(max)
  39. end
  40.  
  41. val c1 = compute [2,5,1,2,3,4,7,7,6] = 10
  42. val c2 = compute [0,5,0,1,0,2,1,0] = 5
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
File "prog.ml", line 1, characters 12-13:
Syntax error
stdout
Standard output is empty