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 编程语言中显示斐波那契数列。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP