2

我正在尝试使用 Haskel 实现 collat​​z-list:这是我的代码:

collatz n
     | mod n 2 == 0 = div n 2
     | otherwise =  3 * n + 1


collatzList n
        | n < 1 = error "Cannot have negative number"
collatzList
        | n == 1 = [n]
        | otherwise = n:collatzList (collatz n)

我收到的错误消息是:输入 `collat​​zList' [1 of 1] 解析错误 编译 Main (exer.hs,解释) 失败,加载模块:无。

谁能告诉我为什么我会收到这条消息?

4

3 回答 3

4

我得到不同的错误(使用 GHC 7.4.1):

> :load "/tmp/C.hs"
[1 of 1] Compiling Main             ( /tmp/C.hs, interpreted )

/tmp/C.hs:9:11: Not in scope: `n'

/tmp/C.hs:9:21: Not in scope: `n'

/tmp/C.hs:10:23: Not in scope: `n'

/tmp/C.hs:10:46: Not in scope: `n'
Failed, modules loaded: none.

这是因为您忘记了n第二个等式中的参数collatzList。您可以添加此参数

collatzList n
        | n < 1 = error "Cannot have negative number"
collatzList n -- the n was missing here
        | n == 1 = [n]
        | otherwise = n:collatzList (collatz n)

或者,由于左侧现在相同,您可以简单地将其与第一个连接:

collatzList n
        | n < 1 = error "Cannot have negative number"
        | n == 1 = [n]
        | otherwise = n:collatzList (collatz n)
于 2012-11-25T23:04:04.697 回答
2

你正在重新定义collatzList.

collatzList
        | n == 1 = [n]
        | otherwise = n:collatzList (collatz n)

做这个:

collatz n
     | mod n 2 == 0 = div n 2
     | otherwise =  3 * n + 1

collatzList n
        | n < 1 = error "Cannot have negative number"
        | n == 1 = [n]
        | otherwise = n:collatzList (collatz n)
于 2012-11-25T22:57:43.063 回答
0

生成仅依赖于前一个值的值列表是unfoldr函数 ( Data.List.unfoldr) 的典型应用:

import Data.List (unfoldr)
collatzList = unfoldr nextCollatz
      where nextCollatz n | n <= 0    = Nothing
                          | n == 1    = Just (1,0)
                          | even n    = Just (n, n `quot` 2)
                          | otherwise = Just (n, 3*n+1)

unfoldr f x0取一个起始值x0,并对其应用一个函数f。如果f x0Nothing,算法终止;如果是Just (push, next),则添加push到结果列表中,并next用作 的新值x0。另一个例子,使用展开器生成最多 100 个正方形:

import Data.List (unfoldr)
squareList = unfoldr pow2
      where pow2 n | n > 100   = Nothing
                   | otherwise = Just (n, 2*n)
                   -- "Add n to the result list,
                   -- start next step at 2*n."

(关于 的强制性说明error:通常最好让函数返回一些虚拟值。例如,在我上面的 Collat​​z 函数中,非正整数的结果是空列表而不是异常。)

于 2012-11-27T21:56:40.097 回答