Lab: Recalling Scheme
CSC 261 - Artificial Intelligence - Weinman
- Summary:
- You will brush up on some Scheme fundamentals and important
concepts.
Preparation
- Open DrScheme. Either click on the lambda in your control panel, or
type drscheme at the command line.
- Set your language preference to "Pretty Big" as follows:
- Click on the "Langage" menu and select "Choose Language..."
- Click "Pretty Big" under the Legacy Languages heading.
- Click OK.
- Click Run.
- You may wish to load a Scheme reference, such as The
Scheme Programming Language, in another tab.
Exercises
A: Using conditionals
- 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).
- 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
- Write an expression using only cons statements that builds
the list (1 2 3).
- 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).
- Write an expression that produces a list containing the square root
of every number in lst.
C: Writing procedures
- 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.
- 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))))
- Add the above to your definitions pane and write the companion procedure
right-section.
- Re-write largest-number without using the keyword lambda.
- 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.
- 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.
This work is licensed under a Creative
Commons Attribution-Noncommercial-Share Alike 3.0 United States License.