Haskell 整数转字符串
Haskell Int to String
我知道这个问题以前出现过,但是 this question 中的答案出于某种原因对我不起作用。我的例子是下面的代码:
fiblist = 0 : 1 : (zipWith (+) fiblist (tail fiblist))
fib :: (Integral a) => a -> String
fib n
| n < 10000 = show (fiblist !! n)
| otherwise = error "The number is too high and the calculation might freeze your machine."
我试图将索引 n
处的元素转换为字符串,以便该函数符合其签名,但出现以下错误:
MyLib.hs:63:34:
Couldn't match expected type ‘Int’ with actual type ‘a’
‘a’ is a rigid type variable bound by
the type signature for fib :: Integral a => a -> String
at MyLib.hs:61:8
Relevant bindings include
n :: a (bound at MyLib.hs:62:5)
fib :: a -> String (bound at MyLib.hs:62:1)
In the second argument of ‘(!!)’, namely ‘n’
In the first argument of ‘show’, namely ‘(fiblist !! n)’
Failed, modules loaded: none.
那我怎么转换呢?
编辑#1:
我知道命令行选项 +RTS -M256m -K256m
等,但它们似乎不适用于此代码,它仍然几乎占用了我所有的内存,如果 n
太高的。 length
无限列表的不同行为,命令行参数起作用并停止执行代码。
编辑#2:
我找到了导入的方法 genericIndex
:
import Data.List
我猜这与 here 上显示的相同。
现在当我使用下面的代码时:
fib :: (Integral a) => a -> String
fib n
| n < 10000 = genericIndex fiblist n
| otherwise = error "The number is too high and the calculation might freeze your machine."
我收到以下错误:
MyLib.hs:64:11:
No instance for (Num String) arising from the literal ‘0’
In the first argument of ‘(:)’, namely ‘0’
In the expression: 0 : 1 : (zipWith (+) fiblist (tail fiblist))
In an equation for ‘fiblist’:
fiblist = 0 : 1 : (zipWith (+) fiblist (tail fiblist))
Failed, modules loaded: none.
因为你声称 fib
在 Integral
的所有实例上都是多态的,最简单的解决方法可能是从使用 (!!)
切换到使用 genericIndex
,它有这些类型签名:
(!!) :: [a] -> Int -> a
genericIndex :: Integral i => [a] -> i -> a
我知道这个问题以前出现过,但是 this question 中的答案出于某种原因对我不起作用。我的例子是下面的代码:
fiblist = 0 : 1 : (zipWith (+) fiblist (tail fiblist))
fib :: (Integral a) => a -> String
fib n
| n < 10000 = show (fiblist !! n)
| otherwise = error "The number is too high and the calculation might freeze your machine."
我试图将索引 n
处的元素转换为字符串,以便该函数符合其签名,但出现以下错误:
MyLib.hs:63:34:
Couldn't match expected type ‘Int’ with actual type ‘a’
‘a’ is a rigid type variable bound by
the type signature for fib :: Integral a => a -> String
at MyLib.hs:61:8
Relevant bindings include
n :: a (bound at MyLib.hs:62:5)
fib :: a -> String (bound at MyLib.hs:62:1)
In the second argument of ‘(!!)’, namely ‘n’
In the first argument of ‘show’, namely ‘(fiblist !! n)’
Failed, modules loaded: none.
那我怎么转换呢?
编辑#1:
我知道命令行选项 +RTS -M256m -K256m
等,但它们似乎不适用于此代码,它仍然几乎占用了我所有的内存,如果 n
太高的。 length
无限列表的不同行为,命令行参数起作用并停止执行代码。
编辑#2:
我找到了导入的方法 genericIndex
:
import Data.List
我猜这与 here 上显示的相同。
现在当我使用下面的代码时:
fib :: (Integral a) => a -> String
fib n
| n < 10000 = genericIndex fiblist n
| otherwise = error "The number is too high and the calculation might freeze your machine."
我收到以下错误:
MyLib.hs:64:11:
No instance for (Num String) arising from the literal ‘0’
In the first argument of ‘(:)’, namely ‘0’
In the expression: 0 : 1 : (zipWith (+) fiblist (tail fiblist))
In an equation for ‘fiblist’:
fiblist = 0 : 1 : (zipWith (+) fiblist (tail fiblist))
Failed, modules loaded: none.
因为你声称 fib
在 Integral
的所有实例上都是多态的,最简单的解决方法可能是从使用 (!!)
切换到使用 genericIndex
,它有这些类型签名:
(!!) :: [a] -> Int -> a
genericIndex :: Integral i => [a] -> i -> a