• Home
  • Textbooks
  • Programming in Haskell
  • Interactive programming

Programming in Haskell

Graham Hutton

Chapter 10

Interactive programming - all with Video Answers

Educators


Chapter Questions

Problem 1

Redefine putstr :: string $\rightarrow$ IO () using a list comprehension and the library function sequence_ :: [IO a] $\rightarrow$ IO $(1$.

Check back soon!

Problem 2

Using recursion, define a version of putBoard :: Board $\rightarrow$ IO () that displays nim boards of any size, rather than being specific to boards with just five rows of stars. Hint: first define an auxiliary function that takes the current row number as an additional argument.

Check back soon!

Problem 3

In a similar manner to the first exercise, redefine the generalised version of putBoard using a list comprehension and sequence.

Check back soon!

Problem 4

Define an action adder :: IO () that reads a given number of integers from the keyboard, one per line, and displays their sum. For example:
$>$ adder
How many numbers? 5
1
3
5
7
9
The total is 25
$>$ adder
How many numbers? 5
1
3
5
7
9
The total is 25
Hint: start by defining an auxiliary function that takes the current total and how many numbers remain to be read as arguments. You will also likely need to use the library functions read and show.

Check back soon!

Problem 5

Redefine adder using the function sequence :: [IO a] $\rightarrow$ IO (a) that performs a list of actions and returns a list of the resulting values.

Check back soon!
00:49

Problem 6

Using getch, define an action readline :: to string that behaves in the same way as getLine, except that it also permits the delete key to be used to remove characters. Hint: the delete character is ' $\triangle \mathrm{DEL}^{\prime}$, and the control character for moving the cursor back one space is $' \backslash b^{\prime}$.

Ernest Castorena
Ernest Castorena
Numerade Educator