Learn/Haskell/3. Reading input and arithmetic
Lesson 3 of 8
Sign in

Haskell, lesson 3

Reading input and arithmetic

Standard input (stdin) is the text fed into your program, such as the Input box when you run code here. In Haskell, reading it is an IO action, so it happens inside main's do block.

getLine and the arrow

getLine reads one line and gives it to you as a String, without the line break. To get the result out of an action, use the arrow <-:

Read name <- getLine as "run getLine and call its result name". Compare that with let, which names a plain value and runs nothing. Each reads the next line.

2 exercises to try what you just read.

Exercise 1

Pizza split

Press Ctrl Enter or Check to test your code.

Exercise 2

Temperature converter

Press Ctrl Enter or Check to test your code.

getLine

From text to numbers

read turns a string into a value of whatever type you ask for. Because many types can be read, you tell Haskell which one with a type annotation:

readLn combines the two steps: it reads a line and parses it.

If the text isn't a valid number, read stops the program with "no parse". For now, trust the input format the exercise gives you.

When several numbers share a line, words splits the line on whitespace and map read converts every piece:

Arithmetic

+, - and * work on any number type. Division is where Haskell is strict about types:

ExpressionWorks onResult
7 / 2Double3.5
7 `div` 2Int, Integer3 (rounded down)
7 `mod` 2Int, Integer1
2 ^ 10integer power1024

div and mod are ordinary functions. Wrapping a function name in backticks lets you write it between its arguments, which reads more naturally: a `div` b is the same as div a b.

/ doesn't work on Int at all. To divide whole numbers and get a decimal, convert them first with fromIntegral:

Other useful functions: abs, max a b, min a b, sqrt, round, floor, ceiling.

Printing decimals neatly

show prints a Double with all its digits. For a fixed number of decimals, use printf from the Text.Printf module, which comes with GHC. Import it at the top of the file:

%d formats an integer, %s a string and %.2f a decimal with two places.

Further reading

  • Prelude: getLine, readLn, read
  • A Gentle Introduction to Haskell: Numbers
  • Text.Printf

A group of friends shares a pizza. Read the number of slices on the first line and the number of friends on the second line (always at least 1). Everyone gets the same whole number of slices, and the rest stays in the box. Print:

Sample input:

Sample output:

Read a temperature in Celsius (it may have a decimal point) and print it in Fahrenheit with exactly one decimal place. The formula is F = C * 9 / 5 + 32.

Sample input:

Sample output:

Print the Celsius value exactly as it was typed, using %s for the original text.

main :: IO ()
main = do
  name <- getLine
  putStrLn ("Hello, " ++ name ++ "!")
main :: IO ()
main = do
  line <- getLine
  let n = read line :: Int
  print (n * 2)
  count <- readLn :: IO Int
  price <- readLn :: IO Double
  line <- getLine
  let [a, b] = map read (words line) :: [Int]
  let total = 10 :: Int
      avg = fromIntegral total / 4 :: Double   -- 2.5
import Text.Printf (printf)

main :: IO ()
main = printf "Average: %.2f\n" (10 / 3 :: Double)   -- Average: 3.33
Each friend gets <n> slices
Leftover: <m>
10
3
Each friend gets 3 slices
Leftover: 1
21.5
21.5 C = 70.7 F