Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to input an integer in haskell? (input in console)

Tags:

io

haskell

How can I enter an integer in the console, store it in a variable and then pass it as a parameter of a function that I have created?

So far so that it works I had to do as follows:

In the last line you can see how I have been applying the function, what I want to do is to ask for the variables by console to be applied as integers to the function and then print the result.

    mayor :: Int -> Int -> Double
    mayor x y =
        if  x < y 
        then 0.1
            else 0.3


    compra :: Int -> Int -> Int -> Int -> Int -> Int -> Double
    compra n v u iva p vp =
        let valor_compra = (fromIntegral v) * (fromIntegral n) * (1 - mayor n u)
            valor_iva = valor_compra * (fromIntegral iva) / 100
            valor_puntos = fromIntegral (p * vp)
            efectivo = if (valor_puntos < valor_compra) then valor_compra-valor_puntos else 0
        in  valor_iva + efectivo

    main = do
    print (compra 20 2000 7 14 10 1500)

The way I do it gives me as a result 16920.0

like image 297
delta1020 Avatar asked Feb 13 '17 21:02

delta1020


People also ask

How do you input an integer?

Whole numbers (numbers with no decimal place) are called integers. To use them as integers you will need to convert the user input into an integer using the int() function. e.g. age=int(input("What is your age?"))

How do you type something in Haskell?

If you are using an interactive Haskell prompt (like GHCi) you can type :t <expression> and that will give you the type of an expression. e.g. or e.g.

How is int defined in Haskell?

Int is defined to be a signed integer with a range of at least [-2^29, 2^29) . – Aaron Friel.

Can we input integer in string?

To take input of a integer we use nextInt() function, that does not read the new line character of your input. So, when we command nextLine() it will take it as new line and give you as a new line.


1 Answers

If the integers are entered on same line, we can do something like below. The below program reads two numbers separated by space and prints their sum.

main :: IO()
main = do
    line <- getLine
    let a = (read (takeWhile (/= ' ') line) :: Int)
    let b = (read (drop 1 (dropWhile (/= ' ') line)) :: Int)
    putStrLn (show (a+b)) 
like image 163
user2159471 Avatar answered Oct 03 '22 13:10

user2159471