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

Recommended Answers

All 2 Replies

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\"".)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.