簡體   English   中英

基本的Haskell IO(IO String - > Int)?

[英]Basic Haskell IO (IO String -> Int)?

初學者哈斯克勒在這里。 我試圖通過一個簡單的命令行程序來代表比薩餅方程式 ,該程序需要一些人並返回適當數量的比薩餅來訂購。 我知道我需要將輸入( IO String )轉換為Int,然后使用show將結果轉換為字符串。 我如何IO String -> Int 或者我剝掉這只貓都錯了?

import System.Environment
import System.IO

pizzas :: Integral a => a -> a
pizzas x = div (x * 3) 8

main = do
    putStrLn "How many people are you going to feed?"
    arg <- getLine
    -- arg needs to IO String -> Int
    -- apply pizzas function
    -- Int -> String
    putStrLn "You will need to order " ++ string ++ " pizzas."

如果可能,使用read會將字符串中的類型轉換為適當的類型

使用show會將整數轉換為它的字符串表示形式

arg <- getLine
let num = pizzas (read arg)
putStrLn $ "You will need to order " ++ (show num) ++ " pizzas."

或者這樣做:

arg <- readLn :: IO Int
let num = pizzas arg
putStrLn $ "You will need to order " ++ (show num) ++ " pizzas."

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM