haskell代写

Haskell Coursework 2018

November 27, 2018

1 Introduction

The Haskell coursework for this year is a programming challenge. This challenge is modular: it is not necessary to complete every aspect to get a passing mark. If a function is marked as being higher order, it should written using higher order functions. Submit a Haskell file (extension *.hs). This file must load into ghci without type or other errors. You must provide type signatures for all defined functions. Finally, while it is acceptable to use functions provided by Prelude, please write any other required functions yourself. Do not use functions imported from libraries. While this is bad practice for real development, it is

good practice for learning.

2 Basics

A Horse is an ascii image of a horse. Learn about newtypes (via Google) and instantiate a Horse type with at least one member, e.g.

horse : : Horse
horse = [ ” ,//) ”

,”;;’\” , ” ,;;’ ( ’\ ” , ” / ’\ ) ”]

3 Rotations

Define a higher order function, transpose that takes a Horse and rotates it 90◦ to the right. Define another (higher order) function mirror which flips the Horse on its vertical access. Show that it is possible to perform all possible rotatations using just these two functions (hint: think compositionally).

1

4 Integer Sequences

Define functions to generate two mathematical sequences, such as the Fibonacci sequence. These should be higher order functions. Hint: you may want to look at the On-Line Encyclopedia of Integer Sequences https://oeis.org/ for inspiration. You might want to make sure that the sequences do not grow too quickly. Note: do not use the Fibonacci sequence!

As such sequences may be infinite, it should be possible to pass a parameter to the functions to limit the length of the returned list.

5 IO

Write a function pretty :: Horse −> IO () which takes an element of type Horse and prints it to the terminal.

Write a function horseSeq which takes a function f, a positive integer n and a Horse as an argument and pretty prints Horses in accordance with f, e.g.

horseSeq :: (Int −> [Int]) −> Int −> Horse −> IO () horseSeq f n h = …

horseSeq fibonacci 5 horse = ∗∗ no horse ∗∗

,//)

;;’\ ,;;’ ( ’\

/ ’\ ) ,//) ;;’\

,;;’ ( ’\ / ’\ )

,//) ,//)

;;’ \ ;;’ \ ,;;’ ( ’\ ,;;’ ( ’\

/ ’\ ) / ’\ )
,//) ,//) ,//) ;;’\ ;;’\ ;;’\

,;;’ ( ’\ ,;;’ ( ’\ ,;;’ ( ’\

/ ’\ )

6 Type Classes

/ ’\ ) / ’\ )

What typeclass(es) does Horse belong to? Write at least one typeclass instance for Horse.

2