88908

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

Question:

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

Answer1:

Use getLine or readLn and then parse the input value to the type you need it to be like so :

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 putStrLn "enter value for x: " input1 <- getLine putStrLn "enter value for y: " input2 <- getLine let x = (read input1 :: Int) let y = (read input2 :: Int) print (compra x y 7 14 10 1500)

Answer2:

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))

Recommend

  • How to refactor code to use MonadRandom
  • boost graph understanding vertex creation behaviour
  • Python Ctypes weird behavior
  • How to iterate through all nodes of a tree?
  • Using Nokogiri's CSS method to get all elements within an alt tag
  • sending email in linux as subject and attachment
  • Dynamic allocation (malloc) of contiguous block of memory
  • Excel VBA: Macros assigned to data validation menus
  • Dynamic viewport resizing - Phonegap ignores viewport
  • function cannot handle big values of n (close to 400)
  • GWT: retrieve Components from FormPanel/VerticalPanel which are added dynamically
  • How to input an integer in haskell? (input in console)
  • Haskell - Couldn't match type ‘PersistEntityBackend record0’ with ‘SqlBackend’
  • Getting hold of a variable in complex compound term in Prolog
  • Magento get list of item SKUs from orders
  • Pipe in for loop breaks double quoted variables
  • Use of this Javascript
  • Linq Objects Group By & Sum
  • Rearranging Cells in UITableView Bug & Saving Changes
  • align graphs with different xlab
  • Return words with double consecutive letters
  • Numpy divide by zero. Why?
  • php design question - will a Helper help here?
  • How to format a variable of double type
  • Buffer size for converting unsigned long to string
  • AngularJs get employee from factory
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Reading document lines to the user (python)
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal