Advertisement
VladNitu

Untitled

Mar 28th, 2023
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. object Solution {
  2.  
  3. /**
  4. * Implement a "clock" object.
  5. * The object must have a single field, and three methods:
  6. * - The field should contain a number which is initially 0.
  7. * - The "tick" method should increment the number by 1.
  8. * - The "reset" method should reset the clock to 0.
  9. * - The "read" method should return the current clock value.
  10. */
  11. val clock =
  12. """
  13. (object
  14. ((field cnt 0))
  15. (
  16. (method tick () (set cnt (+ 1 cnt)))
  17. (method reset () (set cnt 0))
  18. (method read () cnt)
  19. (method tick-x-times (x) (set cnt (+ x cnt)))
  20. ))
  21. """
  22.  
  23. /**
  24. * Implement a "clock factory" object.
  25. * The clock factory object must have a single field, and two methods:
  26. * - The field should contain a number which is initially 0. The value of this field decides
  27. * what the initial value is for the clocks that the factory produces. (See the test
  28. * template for examples.)
  29. * - The "set-init" method updates the field of the clock factory (and hence the initial value
  30. * of produced clocks).
  31. * - The "produce" method returns a "clock" object (where a "clock" object is as defined above).
  32. */
  33. val clockFactory =
  34. s"""
  35. (object
  36. ((field init-val 0))
  37. (
  38. (method set-init (new-init-val) (set init-val new-init-val))
  39. (method produce () (let ((cl $clock)) (msg cl tick-x-times init-val)))
  40. ))
  41. """
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement