[免责声明] 我对 Haskell(以及与此相关的任何 FPL)都很陌生,今天刚开始通过阅读 YAHT 来学习。所以我的代码可能看起来“有趣”。任何有助于改进编码风格的帮助也将不胜感激。
我试图在 Haskell 中编写一个函数,该函数针对给定的 n 值生成一个包含系列 1 到 n 的列表,从 +1 开始并在第 1 个数字之后首先切换符号,然后是 2,然后是 3,依此类推。
例如系列 16 应该产生 [1,-2,-3,4,5,6,-7,-8,-9,-10,11,12,13,14,15,-16](1 个正数,2 个负数, 3 个阳性, 4 个阴性, ...).
我发现每个三角数后面符号都变了,等于前几个自然数之和。
所以我写了这段代码:
module Test
where
--It accepts n and k, prints numbers 1 to n, starting with +1 and toggling their sign after each triangular number
series 0 = []
series n =
if isTriangular n
then series (getPrevTri n (n-1)) ++ getSeries (odd (n + (getPrevTri n (n-1)))) ((getPrevTri n (n-1)) + 1) (n - (getPrevTri n (n-1)))
else series (getPrevTri n (n-1)) ++ getSeries (odd ((getNextTri n (n+1)) + (getPrevTri n (n-1)))) ((getPrevTri n (n-1)) + 1) (n - (getPrevTri n (n-1)))
--The sign is negative for those numbers which follow an odd triangular number AND the triangular number previous to it is even
--OR an even number AND the triangular number previous to it is odd.
getSeries sign start 0 = []
getSeries sign start n =
if sign == True
then [start] ++ getSeries True (start+1) (n-1)
else [-start] ++ getSeries False (start+1) (n-1)
--Checks whether n is a triangular number or not
isTriangular 0 = False
isTriangular n =
checkSum n 1
--Checks whether n is equal to sum of first few natural numbers, starting from k
checkSum n 0 = False
checkSum n k =
if n == (k * k + k)/ 2
then True
else if n > (k * k + k)/ 2
then checkSum n (k+1)
else False
--Gets the triangular number just smaller than n, descending from k
getPrevTri 0 k = 0
getPrevTri n k =
if k <= n
then if isTriangular k
then truncate k
else getPrevTri n (k-1)
else 0
--Gets the triangular number just greater than n, starting from k
getNextTri 0 k = 1
getNextTri n k =
if k >= n
then if isTriangular k
then truncate k
else getNextTri n (k+1)
else 0
我不得不在“getPrevTri”和“gerNextTri”中添加对“截断”的调用,因为它开始生成小数。但我仍然收到此错误:
*Test> :load "Test.hs"
[1 of 1] Compiling Test ( Test.hs, interpreted )
Ok, modules loaded: Test.
*Test> series 16
<interactive>:1:0:
Ambiguous type variable `t' in the constraints:
`Integral t' arising from a use of `series' at <interactive>:1:0-8
`RealFrac t' arising from a use of `series' at <interactive>:1:0-8
Probable fix: add a type signature that fixes these type variable(s)
*Test>
谁能解释一下这个错误的根源是什么?
令我惊讶的是,当我尝试调试这段代码时,我将其修改为 http://pastebin.ca/1932564产生了类似的错误。
然后到http://pastebin.ca/1932556令人惊讶的是,它没有引起任何错误。
(请在相应帖子的末尾找到输出。)
我从中推断出调用
isTriangular n
导致类型错误
odd n
当 Haskell 是一个“纯”FPL 并且其中的函数没有任何副作用时,这怎么可能呢?
我在 Windows 7 x64 机器上使用 GHCi,版本 6.12.3 来编写这些代码。
最佳答案
没有同时实现Integral
(由odd
强制)和RealFrac
(由(/)<强制)的数值类型
)。 (这些是类型类,如果您不知道我在说什么,请等到您的教程展示了这一点)
您可以将 /
替换为 div
或通过 fromIntegral
或类似方式进行显式转换。您也可以使用 x/2 == 1
而不是 odd x
。
编辑:在您的第二个 pastebin 文件中,您通过 truncate
进行了转换,这也是可能的。
Haskell 的优势在于它强大的类型系统,可以让您减少编程错误,但也涉及在明显的地方出现奇怪问题的问题。我通常建议您至少在顶层函数中提供类型信息。 (如 myFunc::Int -> Integer
)。这提高了可读性和安全性,因为如果出现问题,编译器会突然提示您。在 ghci 中,您可以通过 :t
命令轻松找到类型信息:
ghci> :t odd
odd :: (Integral a) => a -> Bool
请注意,在使用此函数时,您必须将括号括在中缀函数周围:
ghci> :t ($)
($) :: (a -> b) -> a -> b
关于Haskell:此代码中错误 "Ambiguous type variable ... ` Integral t' ... `RealFrac t' ..."的来源是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3642277/