0

Hi. I've been trying to get this simple function in Haskell to let me call it for many hours and come to a problem I can't find an answer for. The function is of type

Float -> Float -> [Int]

so I need to pass it two Float values. I've fed the result of getLine (d :: IO String, I believe) to the "det" function, where it's passed through a "show " and then a "read" function. I think this should yield a value of type Float and the compiler seems to agree, as it compiles fine. Then, at runtime I get a "Prelude.read: no parse" exception. The code is below. Can anyone explain what I'm doing wrong here?

module Main where

decimal_binary :: Float -> Float -> [Int]
decimal_binary d_num factor = if d_num < 0 || d_num > 255
then [0]
else if (d_num - factor) < 0
then 0 : decimal_binary d_num (factor / 2)
else if (d_num - factor) == 0
then [1]
else 1 : decimal_binary (d_num - factor) (factor / 2)

det a = read(show a)::Float

main = do putStrLn "Enter an integer in the range 0 - 255: "
d <- getLine
putStrLn "The binary form of your input is: "
print (decimal_binary (det d) 128)

Steven

3
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by pyTony
0

Looks like you've messed up the code!

Because Daniweb now sucks for pasting code. Good job Daniweb.

decimal_binary :: Float -> Float -> [Int]
decimal_binary d_num factor = if d_num < 0 || d_num > 255
                              then [0]
                              else if (d_num - factor) < 0
                                   then 0 : decimal_binary d_num (factor / 2)
                                   else if (d_num - factor) == 0
                                        then [1]
                                        else 1 : decimal_binary (d_num - factor) (factor / 2)

det a = read(show a)::Float

main = do putStrLn "Enter an integer in the range 0 - 255: "
          d <- getLine
          putStrLn "The binary form of your input is: "
          print (decimal_binary (det d) 128)

Yes, let's MANUALLY INDENT ALL OUR CODE 4 CHARACTERS.

(read (show "4.5") evaluates to read "\"4.5\"".)

0

Because Daniweb now sucks for pasting code. Good job Daniweb.
You can put ~~~ before and after code in separate lines and surround the code with empty lines before and after, then you do not need to indent it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.