Basic Haskell IO (IO String & # 8594; Int)?

Beginner Haskeller. I'm trying to represent the pizza equation using a simple command line that takes multiple people and returns the corresponding number of pizzas per order. I know I need to convert my input ( IO String

) to Int and then convert the result to a string using show

. How am I IO String -> Int

? Or am I skinning this cat wrong?

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."

      

+3


source to share


1 answer


Using read will convert the type from a string to the appropriate type if possible

And using show converts an integer to this string representation

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

      



Or do the following:

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

      

+3


source







All Articles