简体   繁体   English

Fibonacci secuence 无法将预期类型 'a0 -> t' 与实际类型 '[Integer]' 匹配

[英]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.

相关问题 无法将预期类型[a0]与实际类型IO()匹配 - couldn't match expected type [a0] with actual type IO () Haskell“无法将预期类型“ a”与实际类型“ [a0]”匹配” - Haskell “Couldn't match expected type ‘a’ with actual type ‘[a0]’” 无法将预期的类型“ IO()”与实际类型“ a0-> m0 a0”匹配 - Couldn't match expected type `IO ()' with actual type `a0 -> m0 a0' 无法将预期类型“(t1,t2,t0)”与实际类型“ [a0]”匹配 - Couldn't match expected type `(t1, t2, t0)' with actual type `[a0]` 有人可以解释此类型错误“无法将期望的类型'IO()'与实际类型'a0-> c0'匹配” - could someone explain this type error “Couldn't match expected type `IO ()' with actual type `a0 -> c0' ” Haskell无法将期望的类型“ Integer”与实际类型“ [Integer]”进行匹配 - Haskell Couldn't match expected type `Integer' with actual type `[Integer]' 无法匹配预期类型`[([Char],a0)]'与实际类型`([Char],t0)'Haskell - Couldn't match expected type `[([Char], a0)]' with actual type `([Char], t0)' Haskell 无法将期望的类型[a]与实际类型“整数-> [整数]”匹配 - Couldn't match expected type [a] with actual type `Integer -> [Integer]' 无法将预期类型“ [Integer]”与实际类型“ Integer”匹配 - Couldn't match expected type ‘[Integer]’ with actual type ‘Integer’ Haskell编程分配,“无法将期望的类型'Int'与实际类型'[a0]-> Int'匹配”以及其他一些错误 - Haskell Programming Assignment, “Couldn't match expected type ‘Int’ with actual type ‘[a0] -> Int’ ”and a few more Errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM