• Mir
    link
    fedilink
    arrow-up
    2
    ·
    8 months ago

    What is the best practice then when you want to update a variable’s value?

    • noli
      link
      fedilink
      arrow-up
      10
      ·
      8 months ago

      Depends on how deep down the rabbit hole you want to go :p

      • creating a new variable that contains the updated value
      • recursion (e.g. it’s not possible to make a loop that increments i by 1, but it is possible to turn that loop into a function which calls itself with i+1 as argument)
      • avoiding typical types of operations that would update variable values. For example instead of a for loop that updates every element of a list, a functional programmer will use the map function, which takes a list and a function to apply to each element of that list to create an updated list. There’s several more of these very typical functions that are very powerful once you get used to using them.
      • monads (I’m not even gonna try to explain them as I hardly grasp them myself)