Could not match expected type 'Double' with current type 'Integer'

2

Code haskell:

bin2dec::[Integer]->Integer
bin2dec (h:[]) = h
bin2dec (h:t) = h*(2^length(t))+bin2dec(t)

bin2frac :: ([Integer], [Integer]) -> Double
bin2frac(x,y) = fromDouble(bin2dec(x)) * 10 ^ bin2dec(y)

Goal:

Define a recursive function that receives a tuple with two binary values representing, respectively, the mantissa and the exponent of a number and returns the corresponding fractional decimal.

    
asked by anonymous 07.06.2015 / 01:05

1 answer

1

I do not know where this fromDouble function comes from, but the problem without it is that bin2dec algumaCoisa returns a Integer . You must use the fromInteger :: Num a => Integer -> a function:

bin2dec :: [Integer] -> Integer
bin2dec (h:[]) = h
bin2dec (h:t)  = h * (2 ^ length t) + bin2dec t

bin2frac :: ([Integer], [Integer]) -> Double
bin2frac (x, y) = fromInteger $ (bin2dec x * 10) ^ bin2dec y

I have not reviewed the logic of your code.

    
01.09.2015 / 00:25