CS 119 Lab 3Higher Order Function

Objectives

  • Use the higher order functions every, keep, and accumulate
  • Perform the following tasks in the order given.

    1. Download the Lab3 project and import it into Eclipse as a Haskell project.
       
    2. Try out the examples of every, keep, and accumulate that appear in the notes and make sure you understand how they work.  We will have exercises that use each of these separately and then we will see how we can combine them in interesting ways.  In all these assignments, you will not have to use recursion since all the recursion is built into the higher order functions.
       
      Assignment:
      Use every to write a function exaggerate that takes a sentence and doubles all numbers in the sentence and replaces the word "good" with the word "great" and the word "bad" with the word "terrible"

      > exaggerate (sent "I ate 3 good hotdogs")
      [I ate 6 great hotdogs]

      Hint:  The function applied to each word in the sentence should check for the special cases and return the appropriate results.  Otherwise it should just return the word unchanged.


       

    3. Now let's try using keep.
       
      Assignment:
      Use keep to write a function firstLast which returns the words in a sentence whose first and last letters are the same.

      > firstLast (sent "california ohio nebraska alabama maryland")
      [ohio alabama]


       

    4. Now for accumulate..
       
      Assignment:
      Use accumulate to hyphenate a sentence of words together.

      > hyphenate (sent "one thousand forty five")
      one-thousand-forty-five

      Hint:  The function used as an argument to accumulate must take two words and combine them.


       

    5. We can combine these functions to do useful tasks.
       
      Assignment:
      Write a function acronym using only every, keep, and accumulate.

      > acronym (sent "reduced instruction set computer")
      risc

      > acronym (sent "foundations of computer science")
      fcs

      Small connecting words like "of" are not part of the acronym.  You may use the function realWord to determine if a word is irrelevant or not.


       

    6. Email your modified zipped project to me for grading.