Lab 1

Welcome to lab 1

  • Become familiar with DrRacket procedures
  • Use cons, list and append
  • Use map
  • Use let*

Procedures

  • Create a directory for this lab in your CS account.
  • Download the asmt-helper.scm (same as last week) and lab1-template.scm files.
  • They should be in the same subdirectory (!)
  • Open lab1-template.scm in DrRacket
  • Use the “Save As” menu item to give your lab1-template.scm file a new name that includes your last name
  • Important note! do not keep 'template' as part of the file name.
Part 1

Consider the following definitions:

     (define item 1)
     (define mylist '(1 2))
The following tester expressions represent the 4 possible ways to order item and mylist
with cons.
Copy/paste them into your definitions window:
     (tester '(cons item item))
     (tester '(cons item mylist))
     (tester '(cons mylist item))
     (tester '(cons mylist mylist))
Not every possible collection will provide "good" results. Expressions such as "(1 . 1)" are
called "dotted lists". They do *not* represent well-formed lists.
Write a similar set of expressions for the built-in LIST and APPEND functions.
Review the definitions for LIST and APPEND in DrRacket documentatioin.
Comment out the expressions that cause an error. Be sure you understand
all of the results.
Part 2: maps without cartology
Enter all of the following TESTER expressions into your definitions
window.  Before hitting the RUN button, guess what the results will be.
  (tester '(map even? '(1 2 3 4 5)))
  (tester '(map (lambda (x) (* x x)) '(1 2 3 4 5)))
  (tester '(map first '((1 2 3) (4 5) (6 7 8 9))))
  (tester '(map rest '((1 2 3) (4 5) (6 7 8 9))))
  (tester '(map (lambda (subsetlist)
                  (list (first subsetlist) (second subsetlist)))
                '((1 2 3) (4 5) (6 7 8 9))))
Define a function, called prependToList, that satisfies the following contract:
  ;;  prependToList
  ;; -----------------------------------------
  ;;  INPUTS:  ITEM, an item
  ;;           LOL:-) a list of lists
  ;;  OUTPUT:  A list-of-lists just like LOL, except that each
  ;;             subsidiary list has been prepended with ITEM.
    > (prependToList 1 '((2 3) (4 5 6) (8 9)))
    ((1 2 3) (1 4 5 6) (1 8 9))
  HINT:  Use an expression of the form:  (map (lambda (subsetlist) ...))
         as in the last of the above examples. However, the body
         of the lambda function will have to be different. Think about using CONS, LIST or APPEND.
Part 3

Copy/paste the following two let* expressions into your definitions window. What is the difference in output? Why? (These are from The Racket Guide, section 4.6.)

(tester '(let* ([x (list “Burroughs”)]

       [y (cons "Rice" x)]
       [z (cons "Edgar" y)])
  z)

(tester '(let* ([name (list “Burroughs”)]

       [name (cons "Rice" name)]
       [name (cons "Edgar" name)])
  name)
  
  
Type the following TESTER expression into your definitions window.
Before hitting the RUN button, guess what the result will be.
  (tester '(let* ((x 2)
                  (y (* x x))
                  (z (+ x y))
                  (w (* z 10)))
             (list x y z w)))
Create a function, called swissArmyFuncL, that satisfies the following
contract.
  ;;  swissArmyFuncL
  ;; -----------------------------------------
  ;;  INPUT:  listOfInts, a list of integers
  ;;  SIDE EFFECT:  Prints out listOfInts-ONE, listOfInts-TWO and listOfInts-THREE
  ;;   where  listOfInts-ONE produces the same as listOfInts except that each
  ;;                 number has been incremented by one;
  ;;          listOfInts-TWO is the same as listOfInts-ONE except that
  ;;                 each element has been squared;
  ;;          listOfInts-THREE is a list of booleans:  #t if the
  ;;                 corresponding element of listOfInts-TWO is even,
  ;;                 #f otherwise.
  ;;  OUTPUT:  listOfInts-THREE
  Example:
    > (swissArmyFuncL '(1 2 3))
    listOfInts-one:   (2 3 4)
    listOfInts-two:   (4 9 16)
    listOfInts-three: (#t #f #t)
    (#t #f #t)                    <----- output value
  Note:  You may wish to use PRINTF to print out lists, as illustrated
         below:
         (printf "listOfInts: ~A~%" '(1 2 3 4))
  Note:  Use LET* to generate the sequence of lists.
         For each one, use a MAP expression!