Guest User

Untitled

a guest
Nov 24th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
IO 0.46 KB | None | 0 0
  1. List myAverage := method(
  2.     total := 0
  3.     if (size == 0) then (
  4.         nil
  5.     ) else (
  6.         foreach(x,
  7.             if (x type == "Number") then (
  8.                 total = total + x
  9.             ) else (
  10.                 Exception raise("Cannot get the average of non-numbers!")
  11.             )
  12.             total / size
  13.         )
  14.     )
  15. )
  16.  
  17. list(1, 2, 3, 4) myAverage println      // outputs 2.5
  18. list() myAverage println            // outputs nil
  19. list(2, 3, 4) myAverage println         // outputs 3
  20. list(2, "three", 4) myAverage println       // throws an exception
Add Comment
Please, Sign In to add comment