TypeScript 中的遞迴(不僅僅是遞迴)


遞迴是一個基本的程式設計概念,指的是函式呼叫自身。它可以是解決問題的強大工具,但也可能是困惑和沮喪的根源,尤其對於初學者而言。在本教程中,我們將探討如何在 TypeScript 中有效地使用遞迴,TypeScript 是 JavaScript 的一個流行超集,它添加了可選的靜態型別和其他功能。

使用遞迴時,要記住的一件重要事情是定義一個基本情況,這是一個使函式不再呼叫自身的條件。如果沒有基本情況,函式將無限地呼叫自身,導致無限迴圈。

在 TypeScript 中處理遞迴涉及理解如何在 TypeScript 程式中有效地使用遞迴函式。這包括定義一個基本情況以防止函式無限地呼叫自身,考慮遞迴函式的效能,並可能使用記憶化和尾呼叫最佳化等技術來提高函式的效能。它還涉及理解 TypeScript 的特定語法和功能,例如可選的靜態型別和編譯器標誌,這些可以在遞迴函式中使用。

在 TypeScript 中使用遞迴的步驟

除了理解如何在 TypeScript 中有效地使用遞迴之外,在處理 TypeScript 程式中的遞迴時還需要考慮其他一些事項:

  • 選擇合適的工具 - 遞迴功能強大,但可能存在更好的解決方案。考慮一下迭代(基於迴圈)的解決方案或其他方法是否更合適。

  • 徹底測試您的程式碼 - 遞迴函式可能難以除錯,因此務必測試您的程式碼以確保其正常工作。

  • 瞭解遞迴的侷限性 - 由於為每個函式呼叫建立新的堆疊幀,遞迴函式可能會消耗大量記憶體來處理大型輸入。這可能會導致堆疊溢位錯誤。

  • 謹慎使用遞迴 - 雖然遞迴是一個有用的工具,但務必謹慎使用,並且僅在它是解決問題的最合適解決方案時才使用它。

記住這些要點,您可以有效地處理 TypeScript 程式中的遞迴。

示例 1

以下是如何在 TypeScript 中處理遞迴的示例。在這個例子中處理遞迴,我們首先定義停止函式無限呼叫自身的基本情況。在這種情況下,基本情況是當 n 為 0 或 1 時。然後,我們定義當 n 大於 1 時的遞迴情況,並指定函式如何計算斐波那契數列中的第 n 個數。斐波那契函式接受單個引數 n 並返回一個數字。該函式使用基本情況在 n 為 0 或 1 時分別返回 0 或 1。在遞迴情況下,該函式返回斐波那契數列中第 (n - 1) 個數和第 (n - 2) 個數的和。

最後,我們使用不同的輸入值測試該函式以確保其正常工作。透過遵循這些步驟,我們可以有效地處理此 TypeScript 函式中遞迴的使用。

// function that calculates the nth number in the Fibonacci sequence
function fibonacci(n: number): number {
   // the base case: when n is 0 or 1, return n
   if (n === 0 || n === 1) {
      return n
   }
   // In the recursive case, calculate the nth number by adding the
   // (n - 1)th and (n - 2)th numbers in the sequence
   return fibonacci(n - 1) + fibonacci(n - 2)
}

// Examine the function using various input values
console.log('Fibonacci of 0th term: ', fibonacci(0)) // 0
console.log('Fibonacci of 1st term: ', fibonacci(1)) // 1
console.log('Fibonacci of 5th term: ', fibonacci(5)) // 5
console.log('Fibonacci of 10th term: ', fibonacci(10)) // 55

編譯後,它將生成以下 JavaScript 程式碼:

// function that calculates the nth number in the Fibonacci sequence
function fibonacci(n) {

   // the base case: when n is 0 or 1, return n
   if (n === 0 || n === 1) {
      return n;
   }
   
   // In the recursive case, calculate the nth number by adding the
   
   // (n - 1)th and (n - 2)th numbers in the sequence
   return fibonacci(n - 1) + fibonacci(n - 2);
}

// Examine the function using various input values
console.log('Fibonacci of 0th term: ', fibonacci(0)); // 0
console.log('Fibonacci of 1st term: ', fibonacci(1)); // 1
console.log('Fibonacci of 5th term: ', fibonacci(5)); // 5
console.log('Fibonacci of 10th term: ', fibonacci(10)); // 55

輸出

以上程式碼將產生以下輸出:

Fibonacci of 0th term:  0
Fibonacci of 1st term:  1
Fibonacci of 5th term:  5
Fibonacci of 10th term:  55

示例 2

在這個例子中處理遞迴,我們首先定義停止函式無限呼叫自身的基本情況。在這種情況下,基本情況是當陣列為空時。然後,我們描述陣列不為空時的遞迴情況,並指定函式如何計算陣列元素的總和。sum 函式接受一個數字陣列並返回一個數字。該函式使用基本情況在陣列為空時返回 0。在遞迴情況下,該函式返回陣列中的第一個元素加上其餘元素的總和。

最後,我們使用不同的輸入值測試該函式以確保其正常工作。透過遵循這些步驟,我們可以有效地處理此 TypeScript 函式中遞迴的使用。

// function that calculates the sum of all numbers in an array
function sum(arr: number[]): number {

   // the base case: when the array is empty, return 0
   if (arr.length === 0) {
      return 0
   }
   
   // In the recursive case, return the first element in the array plus the sum
   
   // of the remaining elements
   return arr[0] + sum(arr.slice(1))
}

// Test the function with different input values
console.log('Sum of array [1, 2, 3, 4, 5]: ', sum([1, 2, 3, 4, 5])) // 15
console.log('Sum of array [-1, 2, -3, 4, -5]: ', sum([-1, 2, -3, 4, -5])) // -3
console.log('Sum of array []: ', sum([])) // 0

編譯後,它將生成以下 JavaScript 程式碼:

// function that calculates the sum of all numbers in an array
function sum(arr) {

   // the base case: when the array is empty, return 0
   if (arr.length === 0) {
      return 0;
   }
   
   // In the recursive case, return the first element in the array plus the sum
   
   // of the remaining elements
   return arr[0] + sum(arr.slice(1));
}

// Test the function with different input values
console.log('Sum of array [1, 2, 3, 4, 5]: ', sum([1, 2, 3, 4, 5])); // 15
console.log('Sum of array [-1, 2, -3, 4, -5]: ', sum([-1, 2, -3, 4, -5])); // -3
console.log('Sum of array []: ', sum([])); // 0

輸出

以上程式碼將產生以下輸出:

Sum of array [1, 2, 3, 4, 5]:  15
Sum of array [-1, 2, -3, 4, -5]:  -3
Sum of array []:  0

更新於:2023年1月20日

4K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.