Lab 9 - Spring 2019

Today's lab will give you practice with using higher order functions with lambda expressions.




How to get started

  1. Use your CS account to login to one of the Linux workstations in the classroom.

  2. Open a browser and copy/paste this URL: https://classroom.github.com/a/AMPWRbkt

  3. Login to Github and you will be prompted to accept this assignment
    • Note: this will create your own copy of Lab 9 for you to work on, but it's not on your Linux account yet

  4. Open a terminal window.

  5. Create a new directory for this lab in your cs101 directory by entering the following at the Linux prompt:

    cd cs101
    mkdir lab9
    cd lab9

  6. Type the following commands to pull down a copy of this lab from GitHub:
    $ git init
    $ git remote add origin https://github.com/Vassar-cs101-mlsmith-spr2019/lab-9-yourGitHubID
    $ git pull origin master



Now you are ready to write some functions using the Design Recipe!

Instructions

  1. Launch DrRacket and open your lab9.rkt file

  2. Fill in your name where <YOUR NAME> appears in the comments at the top of the program.

  3. Develop the functions for the Exercises as described below.
    • solve each exercise in order
    • follow the steps of the Design Recipe for each function
    • be sure your solution for each exercise includes all appropriate grading artifacts.

Directions

  • Use build-list and lambda to define the following four functions that, respectively:
    1. creates the list (list 0 … (- n 1)) for any natural number n;
      In a moment of extreme weakness, here is my solution to this one, to help you get started:
      ; NN -> list-of-NN
      ; creates the list '(0 ... (- n 1)) for any NN n
      (define (build-list1 n)
        (build-list n (lambda (m) m)))
      (check-expect (build-list1 1) '(0))
      (check-expect (build-list1 5) '(0 1 2 3 4)) 
    2. creates the list (list 1 … n) for any natural number n;
    3. creates the list (list 1 1/2 … 1/n) for any natural number n;
    4. creates the list of the first n even numbers;
  • Recall that the append function in ISL concatenates the items of two lists or, equivalently, replaces '() at the end of the first list with the second list:
    (check-expect (append (list 1 2 3) (list 4 5 6 7 8))
                  (list 1 2 3 4 5 6 7 8)) 

    Use foldr to define append-from-fold.

  • The foldr function is so powerful that you can define almost any list-processing functions with it. Use foldr to define map-via-fold, which simulates map.

Be sure to include a signature, purpose statement, and check-expect's for each function you write. The body of each function, however, will use higher order functions and lambda expressions (i.e., they will not look like the function template for lists).

Submitting your work

From a terminal window, type the following commands at the Linux prompt:

$ git commit -m "finished lab9" lab9.rkt
$ git push -u origin master



Log out

When you are done, close DrRacket and log out. Always remember to log out when you are done using the system, to ensure that no one else uses your account.