fork download
  1. fun higher_or_max(vs, v) =
  2. let fun iterate(vs, max) =
  3. case vs of
  4. [] => max
  5. | h::t => if h >= v then h else iterate(t, Int.max(max, h))
  6. in
  7. iterate(tl vs, hd vs)
  8. end
  9.  
  10. fun compute lst =
  11. let fun sum(vs, height, v) =
  12. case vs of
  13. [] => 0
  14. | h::t => if h=v
  15. then find_pit(h::t)
  16. else (Int.min(height, v)-h) + sum(t, height, v)
  17. and
  18. find_pit(vs) =
  19. case vs of
  20. h::n::t => if h>n
  21. then sum(n::t, h, higher_or_max(n::t, h))
  22. else find_pit(n::t)
  23. | _ => 0
  24. in
  25. find_pit lst
  26. end
  27.  
  28.  
  29. val t1 = [2,5,1,2,3,4,7,7,6]
  30. val t2 = [0,5,0,1,0,2,1,0]
  31. val t3 = [0,0,5,0,4,0,4,0,3]
  32. val t4 = [0,0,5,0,100,0,4,0,3]
  33. val t5 = [0,0,5,0,7,0,10,10,10]
  34. val t6 = [0,0,7,0,3,0,5,0,2]
  35.  
  36. val c1 = compute(t1) = 10
  37. val c2 = compute(t2) = 5
  38. val c3 = compute(t3) = 11
  39. val c4 = compute(t4) = 5+4+3
  40. val c5 = compute(t5) = 12
  41. val c6 = compute(t6) = 14
  42.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
File "prog.ml", line 1, characters 25-26:
Syntax error
stdout
Standard output is empty