sort.ocm 809 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "dec.ocm"
  2. -- This file contains a recursive call to sorter, so this is not really Occam.
  3. var c:
  4. seq
  5. c:='*s'
  6. proc comparator(value num, chan in, out)=
  7. var old.num, new.num:
  8. seq
  9. old.num:=num
  10. in ? new.num
  11. while new.num
  12. seq
  13. in ? new.num
  14. if
  15. new.num<=old.num
  16. out ! true; new.num
  17. new.num>old.num
  18. seq
  19. out ! true; old.num
  20. old.num:=new.num
  21. in ? new.num
  22. out ! true; old.num; false
  23. :
  24. proc sorter(chan out)=
  25. chan in:
  26. var num:
  27. seq
  28. decin(input, num, c)
  29. if
  30. c<0
  31. out ! false
  32. c>=0
  33. par
  34. sorter(in)
  35. comparator(num, in, out)
  36. :
  37. chan out:
  38. var num:
  39. par
  40. sorter(out)
  41. seq
  42. out ? num
  43. while num
  44. seq
  45. out ? num
  46. decout(output, num, 0)
  47. output ! '*n'
  48. out ? num