[英]Fibonacci secuence Couldn't match expected type ‘a0 -> t’ with actual type ‘[Integer]’
Down two functions.下来两个功能。 One that calculates fibonacci of a certain number and the other that tries to show the fibonacci sequence up to a certain number using map.
一个计算特定数字的斐波那契数列,另一个尝试使用地图显示特定数字的斐波那契数列。 The error occurs when I type fibonacci and a number, for example fibonacci 8 and throws error.
当我输入 fibonacci 和一个数字时会发生错误,例如 fibonacci 8 并抛出错误。 What am I doing wrong?
我究竟做错了什么?
fib :: Integer -> Integer
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
fibonacci :: [Integer]
fibonacci = map fib [0..]
When you use fibonacci
, you're writing:当您使用
fibonacci
,您正在编写:
> fibonacci 8
(error)
But, fibonacci :: [Integer]
- fibonacci
isn't a function and you can't call it!但是,
fibonacci :: [Integer]
- fibonacci
不是一个函数,你不能调用它! fibonacci
is an infinite list, so you must use list-based operations to get specific numbers. fibonacci
是一个无限列表,因此您必须使用基于列表的操作来获取特定数字。
The list indexing operator is (!!) :: [a] -> Integer -> a
, so use that:列表索引运算符是
(!!) :: [a] -> Integer -> a
,所以使用它:
> fibonacci !! 8
21
You could also use take :: Integer -> [a] -> [a]
to get the first n numbers:您还可以使用
take :: Integer -> [a] -> [a]
来获取前n 个数字:
> take 8 fibonacci
[0,1,1,2,3,5,8,13]
You can, however do this:但是,您可以这样做:
> fib 8
21
Because fib :: Integer -> Integer
is a function.因为
fib :: Integer -> Integer
是一个函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.