Lab: Recalling Scheme

CSC 261 - Artificial Intelligence - Weinman



Summary:
You will brush up on some Scheme fundamentals and important concepts.

Preparation

  1. Open DrScheme. Either click on the lambda in your control panel, or type drscheme at the command line.
  2. Set your language preference to "Pretty Big" as follows:
    1. Click on the "Langage" menu and select "Choose Language..."
    2. Click "Pretty Big" under the Legacy Languages heading.
    3. Click OK.
    4. Click Run.
  3. You may wish to load a Scheme reference, such as The Scheme Programming Language, in another tab.

Exercises

A: Using conditionals

  1. Write an if expression that produces the symbol shorts if a random temperature between 0 and 100 exceeds your tolerance for wearing long pants and produces the symbol pants otherwise.
    Hint: (random n) produces a random integer in [0,n).
  2. Write a cond expression that produces the symbol shorts if a random temperature exceeds some threshold, coat if the dips below some threshold, and pants otherwise.
    Note: Make sure your tests all operate on the same random temperature!

B: List operations

  1. Write an expression using only cons statements that builds the list (1 2 3).
  2. Define lst as a list of any three numbers you wish. For example,
    (define lst (list 6 4 1))
    Write an expression that doesn't change lst, but produces a version with the first two elements swapped. For example, the expression applied to lst as defined above would produce (4 6 1).
  3. Write an expression that produces a list containing the square root of every number in lst.

C: Writing procedures

  1. Write a procedure (largest-number bits) that returns the largest number that may be represented with a given number of bits.
    (largest-number 4)
    15
    (largest-number 8)
    255
    Hint: The procedure expt will be useful.
  2. Write the procedure (sum numbers) that tallies a list of numbers using basic recursion.
    Note: If your recursion is exceptionally rusty, you may wish to refer to a reading on recursion patterns in Scheme.

D: Higher-order procedures

Recall that map, along with many other useful Scheme habits often benefit from a technique called sectioning, which fixes one parameter of a binary (two-argument) procedure. For example, left-section takes a procedure and the first argument to that procedure, and returns another procedure that accepts the second (right) argument, with the operation and first (left) argument fixed.
(define left-section
  (lambda (proc left)
    (lambda (right)
      (proc left right))))
  1. Add the above to your definitions pane and write the companion procedure right-section.
  2. Re-write largest-number without using the keyword lambda.
  3. In addition to using producing procedures as return values (as left-section and compose do), we can also use procedures as parameters (as map). Recall that (apply proc args) is also in this second category. Rewrite sum using apply. As a bonus, see if you can do it without lambda.
  4. Write a procedure (dot-product v1 v2) that takes two lists of numbers having the same length and produces their dot product, that is, the sum of the products of the corresponding entries in v1 and v2. For example,
    (dot-product (list 1 2 3) (list (4 5 6)) ;; 1*4 + 2*5 + 3*6
    32

Lab Assignment

Hopefully you've dusted off your Scheme chops, which you'll need for the lab assignments throughout the semester. Proceed to begin this week's lab assignment individually.

Copyright © 2011 Jerod Weinman.
ccbyncsa.png
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.