CS 119 Lab 3 – Higher-Order Procedures

Objectives

  • Illustrate the power of the higher-order procedures every, keep, and accumulate.
  • Perform the following tasks in the order given.

    1. Evaluate the files words.scm and lab3.scm. The file words.scm contains the higher-order functions every, keep, and accumulate. The functions are a bit fancier than the versions discussed in class.
       
    2. The function every may take as arguments a function, which operates on words, and a sentence. The function is then applied to every word in the sentence, returning a new sentence. Try the following:

        (every first ‘(american civil liberties union))
        (every abs ‘(-2 4 –5 –3 0))

      You can also use a word as the second argument. In this case, the first argument function is applied to every letter in the word. The results are collected in a sentence. Try

      (every (lambda(wd)(se (word ‘with wd) ‘you)) ‘(in out))

      Assignment:
      Use every to write a procedure exaggerate which exaggerates sentences in the following way:

      Þ (exaggerate ‘(i ate 3 hotdogs))
      (i ate 6 hotdogs)

      Þ (exaggerate ‘(this is a good time))
      (this is a great time)

      The function doubles all the numbers in the sentence and it replaces "good" with "great", and replaces "bad" with "terrible".

      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. The function keep takes a predicate and a sentence as arguments. It returns a sentence containing only the words of the argument sentence for which the predicate is true. Try
          (keep number? ‘(4 calling bird 3 french hens 2 turtle doves))

      You can also use a word as the second argument. In this case, it applies the predicate to every letter of the word and returns another word. Try
          
      (keep (lambda(letter) (member? letter ‘(a e i o u))) ‘piggies)

       
      Assignment:
      Use keep to write a procedure first-last which returns a sentence containing only those words in the argument whose first and last letters are the same:

      Þ (first-last ‘(california ohio nebraska alabama massachesetts))
      (ohio alabama)


       

    4. The function accumulate takes a procedure and a sentence as arguments. It applies the procedure to two of the words in the sentence and gets back another word. Then it applies the procedure to the resulting word and the next word in the sentence, and so on. It ends when it has combined all the words in the sentence into a single result. Try

             
      (accumulate + ‘(6 3 4 –5 7 8 9))
              (accumulate word ‘(a c l u))
              (accumulate max ‘(128 32 134 136))

      You can also use a word as the second argument, using the letters of the word as the elements to be accumulated. Try
               (accumulate sentence ‘computer)
       
      Assignment:
      Use accumulate to hyphenate a sentence of words together:

      Þ (hyphenate ‘(one thousand forty five))
      one-thousand-forty-five

      Hint: Remember that the function given to accumulate must take two words and combine them.


       

    5. We can of course combine these higher-order functions in all kinds of interesting ways.
       
      Assignment:
      Write a function acronym using only every, keep, and accumulate which behaves as follows:

      Þ (acronym ‘(reduced instruction set computer))
      risc

      Þ (acronym ‘(stucture and interpretation of computer programs))
      sicp

      Note that the small connecting words "and" and "of" are not part of the acronym. You may use the predicate real-word? in lab3.scm to determine which words are not irrelevant.


       

    6. Email your files containing the assignments to jzimmerm@goucher.edu.