I want to accept 5 values from the user. However, in my code (below) if I enter a list like [1,2,3,4,5] I get an error. This code only accepts inputs of the form [999], for example.
Does anyone know how to fix this problem?
putStrLn("Enter 5 binary numbers [,,] : ")
input<-getLine
let n=(read input)::[Int]
let result = convertionTO binaryToDec n
putStrLn(show result)
In the above code, the line let n=(read input)::[Int] will just accept the user input, [999] or whatever, as one input. Is there a way to enter a list of values?
With the line let result = convertionTO binaryToDec n I’m trying to convert here list of binary values to decimal
You’re asking for the input to be converted into an
[Int]usingread, so it’s being converted using the same rules as Haskell source code, which means they’re being parsed as decimal numbers.As you want to parse them as binary numbers, you will have to write your own
String -> Int(or, to allow for errors,String -> Maybe Int) function to convert them intoInts from binary.There is no distinction between decimal numbers and binary numbers. There are only numbers. Decimal or binary are artifacts of how numbers are represented as strings.