如何在haskell中输入整数? (在控制台中输入) [英] How to input an integer in haskell? (input in console)

查看:680
本文介绍了如何在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)

我这样做的方式给了我一个结果
16920.0

The way I do it gives me as a result 16920.0

推荐答案

使用 getLine readLn 然后将输入值解析为您需要的类型:

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)

这篇关于如何在haskell中输入整数? (在控制台中输入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆