十进制和二进制数字处理 [英] Decimal and binary number handling
问题描述
我正在研究能够将十进制转换为二进制值的代码。与其他语言相比,haskell需要几行代码才能实现。以下是我编制的代码。
binaryToDec :: [Int] - > Int
binaryToDec [] = []
binaryToDec(x:xs)=(+)(x * 2 ^(length xs))
这段代码给出了一个我试图弄清楚的错误。但我不能。
Assignment.hs:61:18:
无法与实际类型为'[Int' a0]'
在表达式中:[]
在`binaryToDec'的等式中:binaryToDec [] = []
我该怎么办?
binaryToDec :: [Int] - > Int
也就是说 binaryToDec
是一个函数,它以 Int
s作为参数列表,并返回一个 Int
作为结果。
binaryToDec [] = []
这就是说,当 binaryToDec
的参数是一个空列表时,其结果是一个空列表。
空白列表不是 Int
,所以这是一个错误。解决方案: code> binaryToDec [] 应计算到等式的右边。
但这不是全部...
binaryToDec(x:xs)=(+)(x * 2 ^(length xs))
也就是说,当 binaryToDec
的参数是 not 是一个空列表,其结果是一个将 x * 2 ^(length xs)
添加到其参数的函数。 / p>
函数不是 Int
,所以这也是一个错误。解决方案:找出你想添加到 x * 2 ^(长度xs)$ c $
以前的错误消息让你难住。所以让我们一行一行地解构错误信息: 这给出了编译器判断出现错误的行上的文件名,行号和近似位置。 这意味着周围的上下文需要类型 这是表达式类型
Assignment.hs:61:18 :
无法与实际类型为[[a0]'的预期类型'Int'匹配
Int
的值,但是提供的值是类型 [a0]
。 ( a0
这里的意思是不知道,所以编译器会发现你提供了一个列表,但还没有算出它的列表。)在表达式中:[]
[a0]
,但预期类型为 Int
。这并不总是出现你的错误 - 只是编译器在发现错误的地方 - 但在这种情况下,是,你的错误是: [ ]
显然不是 Int
。
[]
而不是表达式 []
。)
这只是给你更多的上下文关于编译器发现错误的地方。
I'm working on a code which capable of converting decimal to a binary value. Comparing to other languages haskell needs few lines of code to bring this up. Below is the code i worked out.
binaryToDec :: [Int] -> Int
binaryToDec [] = []
binaryToDec (x:xs) = (+) (x * 2 ^(length xs))
this code gives an error which i tried so hard to figure out. but i couldn't.
Assignment.hs:61:18:
Couldn't match expected type `Int' with actual type `[a0]'
In the expression: []
In an equation for `binaryToDec': binaryToDec [] = []
what should i do?
binaryToDec :: [Int] -> Int
That says that binaryToDec
is a function, which takes a list of Int
s as its parameter, and returns an Int
as its result.
binaryToDec [] = []
That says that when binaryToDec
's parameter is an empty list, its result is an empty list.
An empty list is not an Int
, so this is an error.
Solution: decide what binaryToDec []
should evaluate to, and use that on the right hand side of the equation.
But that's not all...
binaryToDec (x:xs) = (+) (x * 2 ^(length xs))
That says that when binaryToDec
's parameter is not an empty list, its result is a function that adds x * 2 ^(length xs)
to its parameter.
A function is not an Int
, so this is also an error.
Solution: work out what you want to add to x * 2 ^(length xs)
, and, er, add it.
You have been stumped by error messages like this before. So let's deconstruct the error message line-by-line:
Assignment.hs:61:18:
This gives the filename, line number and approximate position on the line where the compiler decided there was an error.
Couldn't match expected type `Int' with actual type `[a0]'
This means that the surrounding context expected a value of type
Int
, but the value provided was of type[a0]
. (a0
here means "don't know", so the compiler's worked out that you provided a list, but hasn't worked out what it's a list of.)In the expression: []
This is the expression that has type
[a0]
, but was expected to have typeInt
. This is not always where your mistake is --- just where the compiler was when it noticed something was wrong --- but in this case it is where your mistake is:[]
is clearly not anInt
.(n.b. If the error was the
[]
on the left of the equals sign, the error message would have referred to the pattern[]
instead of the expression[]
.)In an equation for `binaryToDec': binaryToDec [] = []
This is just giving you a bit more context about where the compiler found the error.
这篇关于十进制和二进制数字处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!