1/16/2023 0 Comments Haskell functional programmingthe loop unfortunately is not side-effect-free. ![]() Looks pretty safe to me too, for the same array we will get the same sum so the function sumArray looks pure to me. We used a loop here (javascript for-of loop) to determine the sum of the elements in an array. Let's take a look at this loop below: sumArray(arr) What's the first thing we remember about functional programming? Oh right, it focuses on using pure functions and doesn't like side effects. Why doesn't functional programming use loops?Įven if we don't have to use loops, why does functional programming not like them? And you can use a loop for that, but this can also be done in other ways, (ie recursion). ![]() What you may need is a way to do a certain task a number of times, or iteration. But, we do know the code in the loop could run more than once.ĭo we ever NEED a loop? No, not really. Sometimes we know how many iterations we will need, sometimes we don't. In imperative languages, we use loops to do a task a certain number of times. What does a loop do exactly?īefore we leave loops behind, let's try to understand what we need(ed) them for. Hopefully, it'll all be a little less scary after this. Below I’ll summarize my understanding of why functional programming languages usually emphasize the use of recursion (or higher-order functions) over loops and show a few examples. ![]() Also, why don't we use them? What do you do instead? This was very overwhelming as you can see. ![]() What do you mean there are no loops? It's such a basic fundamental tool. No loops!? That's crazy, I code with loops left, right, and center. One of the biggest things that scared me about learning Haskell and Functional Programming was that I had heard that they don't use loops. Let's continue on the functional programming journey this week.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |