Couldn’t match expected type `Double' with actual type `Integer'

Asked

Viewed 117 times

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 takes a tuple with two binary values representing, respectively, the mantissa and the exponent of a number and returns the corresponding decimal fraction.

  • 1

    What is your doubt?

  • @Patrick I think it’s the mistake in the title: Couldn't match expected type Double' with actual type Integer', that must be it.

1 answer

1

I don’t know where that function comes from fromDouble, but the problem without it is that bin2dec algumaCoisa returns a Integer. You need to use the function fromInteger :: Num a => Integer -> a:

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 haven’t revised the logic of your code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.