Home CPSC 401

Haskell Assignment

 

Due: April 17

 

Overview

For this assignment, you will implement the Vigenère cipher in Haskell. The cipher takes 2 strings as input: a plaintext message and a key.

It starts by rotating the first character in the message by the amount specified by the first character of the key. This is done by adding the ASCII values together. Then it rotates the second character in the message by the second character in the key. When the key runs out of characters, it loops around back to the start of the key.

If the resulting character is too large, it will wrapped around until it is in the valid alphabet range. This is done by subtracting 26.

For example, if the message is "hello", and the key is "abc", the cipher will work as follows:

  h (104) e (101) l (108) l (108) o (111)
+ a  (97) b  (98) c  (99) a  (97) b  (98)
  ---------------------------------------
     201     199     207     205     209

(wrap until less than or equal to z (122))

   a (97) y (121) g (103) e (101) i (105)

So the encoded message is "aygei".

Your program should ask the user for the message and the key, then perform the cipher and print the encoded message. For example:

Enter text:
hello
Enter key:
abc
aygei

Your program only needs to encode loewr-case letters, any other characters should be unchanged. For example:

Enter text:
This is a test!
Enter key:
key
Tezv zv r qvvq!

 

Helpful Functions

The following two functions are inside of the Char module. To access them, you would need the following in your program:
import Data.Char

Also, keep in mind that strings are simply lists of characters, so the list functions we discussed in class can be used on strings.


 

Submitting

Submit your Haskell file to Canvas for this assignment.

Copyright © 2024 Ian Finlayson | Licensed under a Creative Commons BY-NC-SA 4.0 License.