CS 222

Logo

Programming Languages

Haskell Activity 2

Open the comand prompt / terminal, navigate to your desktop, and enter ghci. Open a code editor (e.g., Atom, Visual Studio Code, Sublime Text) and create a file activity02.hs; save it to the cs222 folder on your desktop. As you write code, test it in ghci.

  1. Copy the following code into your file. Test it from ghci. What does it do? What does -- do in a .hs file? How does this compare to a .lhs file? Write your answer in comments above the function.
    -- what does this function do???
    mystery :: Integer -> Integer
    mystery n = if n >= 0 then n else (-n)
    
  1. Write a function called doubleSmall that takes an integer x and doubles it if it is less than or equal than 100, otherwise it returns the given number x.

  2. Add to your file a definition a function hyp that computes the hypotenuse given two integer sides of a right triangle. Recall that . Hint: It may help to refer to Fig. 1 on page 13 of your book.

  1. Write a function that computes the area of a circle given the radius.

  2. Add a function whatToWear :: Float -> (Float, String) that takes degrees fahrenheit as input. Use the where clause to compute degrees in celsius. Recall that degrees celsius is given by C = (F - 32) * (5/9), where C is degrees celsius and F is degrees fahrenheit. The function should output a tuple consisting of degrees celsius followed by a string saying what to wear.

    • If degrees in cl is less than or equal to 0, the string in the tuple should be “Wear a coat!”
    • If degrees in celsius is greater than 0 but less than 40, the string should be “Consider a light jacket”
    • If degrees in celsiusu is greater than or equal to 40, the string should be “Avoid going outside at all.”

    For example:

    > whatToWear 5
    (-15.0,"Wear a coat!")
    

If you finish early

  1. Write a function called lucky which takes an integer and evalutes to “LUCKY NUMBER SEVEN!” if the integer is 7, and “No dice.” otherwise.

  2. Write a function that computes the circumference of a circle given the radius.

  3. Write a function that computes the average of two numbers.

How to submit

Submit your file to Moodle which has all function definitions.