错误“No instance for(Num [t])”在Collatz功能 [英] Error "No instance for (Num [t])" in Collatz function
问题描述
collatz n =(collatz'n):1
其中collatz'n =(takeWhile(> ; 1)(collatz''))
其中collatz''n = n:collatz''(collatz'''n)
其中collatz'''1 = 1
collatz' ''n = if(even n)then(div n 2)else((3 * 2)+1)
当我在GHCi中运行这个时,我得到错误:
$ p $ 没有实例用于(Num [t])$在< interactive>:1:7
pre>
的文字'2'产生的b $ b可能的修正:为(Num [t])
我不知道这是什么意思。问题似乎在列表中追加1。出现这个问题的原因是 在正确的Collatz序列之后生成无限序列的1;然而, 会从 n 生成除1以外的所有Collatz数字。我做错了什么? ony的回答是正确的,但是因为你是Haskell的新手,也许这是一个更清晰的说明。 I am new to Haskell, and programming in general. I am trying to define a function which generates the sequence of Collatz numbers from n. I have: When I run this in GHCi, I get the error: I don't know what this means. The problem seems to be appending "1" to the list. This problem emerges because generates an infinite sequence of "1"s following the correct Collatz sequence; however, generates all Collatz numbers from n except "1". What am I doing wrong? ony's answer is correct, but since you're new to Haskell, maybe this is a clearer explanation. The Try replacing the 这篇关于错误“No instance for(Num [t])”在Collatz功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
pre $
collatz'n =(takeWhile(> 1)(collatz''n))
code> :
运算符 pre 将值赋予一个列表,所以 somelist:7
是无效,因为它试图将值存入列表。这就是为什么(collatz'n):1
不能编译的原因,因为(collatz'n)
的类型是尝试用 ++ [1]替换
。:1
collatz n = (collatz' n) : 1
where collatz' n = (takeWhile (>1) (collatz'' n))
where collatz'' n = n : collatz'' (collatz''' n)
where collatz''' 1 = 1
collatz''' n = if (even n) then (div n 2) else ((3*2)+1)
No instance for (Num [t])
arising from the literal `2' at <interactive>:1:7
Possible fix: add an instance declaration for (Num [t])
collatz' n = (takeWhile (>0) (collatz'' n))
collatz' n = (takeWhile (>1) (collatz'' n))
:
operator prepends a value to a list, so doing somelist : 7
is invalid since that's trying to append a value to a list. That's why (collatz' n) : 1
doesn't compile, since the type of (collatz' n)
is a list of numbers.: 1
with ++ [1]
.