如何在控制台中输入整数,将其存储在变量中,然后将其作为我创建的函数的参数传递?
到目前为止,它的工作原理我必须做如下:
在最后一行中,您可以看到我是如何应用该函数的,我想要做的是通过控制台要求变量作为整数应用于函数,然后打印结果。
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)
我这样做的方式给了我一个结果 16920.0
答案 0 :(得分:4)
使用getLine
或readLn
,然后将输入值解析为您需要的类型:
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)
答案 1 :(得分:0)
如果在同一行输入整数,我们可以执行以下操作。下面的程序读取两个用空格分隔的数字并打印它们的总和。
main :: IO()
main = do
line <- getLine
let a = (read (takeWhile (/= ' ') line) :: Int)
let b = (read (drop 1 (dropWhile (/= ' ') line)) :: Int)
putStrLn (show (a+b))