Haskell 程式顯示斐波那契數列


本教程討論瞭如何在 Haskell 程式語言中編寫程式來顯示斐波那契數列。

斐波那契數列是一個數列,其中每個數字都是前兩個數字的和。前五項斐波那契數列是 0 1 1 2 3。

在本教程中,我們將看到:

  • 列印第 n 個斐波那契數的程式。
  • 列印斐波那契數列前 n 項的程式。
  • 使用列表推導式列印斐波那契數列前 n 項的程式。
  • 使用 map 函式列印斐波那契數列前 n 項的程式。

演算法步驟

  • 輸入所需的範圍值 n。
  • 實現列印斐波那契數列的程式邏輯。

示例 1

列印第 n 個斐波那契數的程式。

-- function declaration
getNthFibo :: Int->Int

-- function definition
-- base case-1
getNthFibo 1 = 0
-- base case-2
getNthFibo 2 = 1
getNthFibo n = getNthFibo (n-1) + getNthFibo (n-2)

main :: IO()
main = do
-- declaring and initializing variable n
   let n = 11
-- invoking the function getNthFibo and printing the returned output
   print (show n ++ "th Fibonacci number is:")
   print (getNthFibo n)

輸出

"11th Fibonacci number is:"
55

在上面的程式中,我們聲明瞭一個函式 getNthFibo,它接受一個整數作為引數並返回一個整數。在其函式定義中,我們接受一個引數 n 並返回對自身進行遞迴呼叫(引數分別為 (n-1) 和 (n-2))的結果之和。遞迴呼叫會一直進行,直到到達基本情況,即當引數為 0 時函式返回 0,當引數為 1 時函式返回 1。程式會列印第 n 個斐波那契數。在主函式中,我們宣告並初始化了一個變數 n。我們呼叫了函式 getNthFibo 並列印了返回的整數。

示例 2

顯示斐波那契數列前 n 項的程式。

-- function declaration
getNthFibo :: Int->Int

-- function definition
-- base case-1
getNthFibo 1 = 0
-- base case-2
getNthFibo 2 = 1
getNthFibo n = getNthFibo (n-1) + getNthFibo (n-2)

-- function declaration
getFiboSeries :: Int -> [Int]
-- function definition
-- base case
getFiboSeries 0 = []
getFiboSeries n = getFiboSeries (n-1)++[getNthFibo n]

main :: IO()
main = do
-- declaring and initializing variable n
    let n = 11
-- invoking the function getFiboSeries and printing the returned output
    print ("The first "++ show n ++ " terms in the Fibonacci series are:")
    print (getFiboSeries n)

輸出

"The first 11 terms in the Fibonacci series are:"
[0,1,1,2,3,5,8,13,21,34,55]

在上面的程式中,我們實現了一個函式 getNthFibo,它與前一個示例中的函式相同,用於返回第 n 個斐波那契數。我們聲明瞭一個函式 getFiboSeries,它接受一個整數作為引數並返回一個整數列表。在其函式定義中,它接受一個引數 n 並返回一個遞迴呼叫,該呼叫與包含單個元素(第 n 個斐波那契數)的列表連線在一起。遞迴呼叫會一直進行,直到函式達到基本情況,即引數值為 0。在這種情況下,函式返回一個空列表。此函式返回斐波那契數列前 n 項的列表。在主函式中,我們宣告並初始化了一個變數 n。我們呼叫了函式 getFiboSeries 並列印了返回的整數列表。

示例 3

使用列表推導式顯示斐波那契數列前 n 項的程式。

-- function declaration
getNthFibo :: Int->Int

-- function definition
-- base case-1
getNthFibo 1 = 0
-- base case-2
getNthFibo 2 = 1
getNthFibo n = getNthFibo (n-1) + getNthFibo (n-2)

main :: IO()
main = do
-- declaring and initializing variable n
   let n = 11
   let fiboSeries = [getNthFibo x | x<-[1..n]]
   print ("The first "++ show n ++ " terms in the Fibonacci series are:")
   print (fiboSeries)

輸出

"The first 11 terms in the Fibonacci series are:"
[0,1,1,2,3,5,8,13,21,34,55]

在上面的程式中,我們宣告並初始化了一個變數 n。我們使用列表推導式生成從 1 到 n 的整數列表,並返回函式 getNthFibo 對元素作為引數執行的結果。我們將列表載入到變數 fiboSeries 中。函式 getNthFibo 返回第 n 個斐波那契數。最後,我們列印了列表。

示例 4

使用 map 函式顯示斐波那契數列前 n 項的程式。

-- function declaration
getNthFibo :: Int->Int

-- function definition
-- base case-1
getNthFibo 1 = 0
-- base case-2
getNthFibo 2 = 1
getNthFibo n = getNthFibo (n-1) + getNthFibo (n-2)

main :: IO()
main = do
-- declaring and initializing variable n
   let n = 11
   let fiboSeries = map getNthFibo [1..n]
   print ("The first "++ show n ++ " terms in the Fibonacci series are:")
   print (fiboSeries)

輸出

"The first 11 terms in the Fibonacci series are:"
[0,1,1,2,3,5,8,13,21,34,55]

在上面的程式中,我們宣告並初始化了一個變數 n。我們使用 map 函式在列表上執行函式 getNthFibo。map 函式接受一個函式和一個列表作為引數,並返回一個列表,其元素是函式在列表元素上執行後返回的元素。函式 getNthFibo 是一個返回第 n 個斐波那契數的函式。

結論

在本教程中,我們討論了四種不同的方法來實現一個程式,以便在 Haskell 程式語言中顯示斐波那契數列。

更新於: 2022-12-14

2K+ 閱讀量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告