Worksheet One: The Job Prospects of a Computer Scientist

You are a blossoming young computer scientist, and have just landed the job of your dreams, working at the most reputable internet giant- Buzzfeed. (insert name here)* **, we’re so happy to have you here! We have some incredibly challenging and fulfilling jobs for you! your boss tells you, as you walk down the glistening corriders. She directs you into a small cleaning closet that contains something vaguely cube-shaped… could it be a computer?!

The boss hands you a sticky note, smiles caringly, then strides back out into those prosperous hallways. The door of the closet slowly creaks shut after her, while you gaze uncertainly at your new office.

“Everyone starts somewhere, right? you whisper to the mop beside you. It drips despondently in reply. Withou further ado, you kick the computer into life, and examine the sticky note, eager to program the infrastructure of the internet’s most vital news source. It reads:

Your first task is to build the functions underpinning our latest mind-blowing quiz- which determines the zodiac of a given person, just from their name!!!

Question 1: Match a name to a year

a) Introduce yourself to the other people in your group and write down everybody’s name and birth year on a whiteboard. Using case statements or guards, write a function called NameToYear which inputs a group member’s name (as a String) and outputs their birth year (as an Int).

b) What is the difference between case and guard statements? Which one should you use here?

c) What happens if you input a name that is not listed in your NameToYear function? Edit your function from part a) to include a relevant error message. Hint: Use a “wild card” (underscore).

d) Error messages are not elegant, and should instead be handled by a Maybe type. Edit your function from part c) so that names which are not listed return Nothing.

Question 2: Match a year to a zodiac

a) Using the table provided as a reference, create a new custom data type called Zodiac which includes the Chinese zodiac signs of your group members.

b) Write a function called YearToZodiac, which inputs a year and outputs the corresponding zodiac sign.

c) Edit your function from Question 2 b) using a Maybe type. You should handle years not listed in your YearToZodiac function so that they return Nothing.

d) Edit your function from Question 2 c) so that it accepts a Maybe Int as an input. Your type definition should be:

YearToZodiac :: Maybe Int -> Maybe Zodiac

Question 3: Match a name to a zodiac

You now have a way of turning a name into a year, and turning a year into a zodiac. Using your NameToYear and YearToZodiac functions, write a final function called NameToZodiac which inputs someone’s name and outputs their zodiac sign.