March 2019
Intermediate to advanced
208 pages
5h 11m
English
You may have noticed that the logic of the recursion in all the preceding examples has looked a lot like a while loop in most programming languages. This has been intentional. Look back at the examples, and you’ll see that the recursive call has always been the very last part of the recursion. Compare these two recursive functions for repeating a string a given number of times:
| | let rec repeatRec = (s: string, accumulator: string, n: int) : string => { |
| | switch (n) { |
| | | 0 => accumulator /* base case; we’re finished */ |
| | | _ => repeatRec(s, accumulator ++ s, n - 1) |
| | }; |
| | }; |
| | |
| | let rec repeatRec2 = (s:string, n:int) : |
Read now
Unlock full access