📓Implement functions with Ability operations

rangeAverage : Nat ->{Store [Nat]} Float
rangeAverage n = base.todo "implement me"

Using theStoreability to keep track of aListof numbers, write a function which takes in some numbernofNatas an upper bound of a list from0up to (but not including)nand returns the average of the final list.

So, given an upper value of5,the average produced would be the average of[0, 1, 2, 3, 4],or2.0

🔑Answer
🔑Answer

One possible solution is as follows:

averageOfRange : Nat ->{Store [Nat]} Float
averageOfRange n =
  use Float / fromNat
  use List +:
  use Nat + ==
  go = cases
    i
      | i == n     ->
        myList = Store.get
        listSize = List.size myList
        sum =
          lib.base.data.List.foldLeft
            (acc element -> element + acc) 0 myList
        fromNat sum / fromNat listSize
      | otherwise  ->
        Store.modify (currentList -> i +: currentList)
        go (i + 1)
  go 0