Computer Science Fundamentals (CS153 2003S)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
a. Please scan through the reading on variable arity procedures.
b. Start DrScheme.
Consider the following procedures:
(define proc1 (lambda (stuff) stuff)) (define proc2 (lambda stuff stuff)) (define proc3 (lambda (more stuff) stuff)) (define proc4 (lambda (more . stuff) stuff))
a. What do you expect the result of (procn 1)
to be for each variant?
b. Verify your results experimentally.
c. What do you expect the result of (procn 1 2)
to be for each variant?
d. Verify your results experimentally.
e. What do you expect the result of (procn 1 2 3)
to be for each variant?
f. Verify your results experimentally.
display-line
Here is the display-line
procedure from the reading.
;;; Procedure: ;;; display-line ;;; Parameters: ;;; val1 ... valn, 0 or more values ;;; Purpose: ;;; Displays the strings terminated by a carriage return. ;;; Produces: ;;; [Nothing] ;;; Preconditions: ;;; (none) ;;; Postconditions: ;;; All of the values have been displayed. ;;; The output is now at the beginning of a new line. (define display-line (lambda arguments (let kernel ((rest arguments)) (if (null? rest) (newline) (begin (display (car rest)) (kernel (cdr rest)))))))
a. Try out some other calls to display-line
to check what it
prints. For example, try the following:
(display-line "going" "going" "gone") (display-line "countdown:" 5 4 3 2 1 "done") (display-line) ;; apply display-line to no arguments
b. Explain your results.
display-line
The current version of display-line
prints all text together
without spaces. Modify the code, so that one space is printed between any
two adjacent values supplied as arguments to display-line
.
For instance, after your modifications, the example from the reading will
change. It will now be ...
> (display-line "+--" "Here is a string!" "--+")+-- Here is a string! --+
You may not use display-separated-line
in your answer to
this question, although you may refer to it for ideas.
Define and test a procedure named call-arity
that takes any
number of arguments and returns the number of arguments it received
(ignoring their values):
> (call-arity 'a #\b "c" '(d)) 4 > (call-arity 0.0) 1 > (call-arity) 0
display-separated-line
a. What do you think should happen if you invoke
display-separated-line
without giving it any arguments?
Verify your results experimentally.
b. What do you think should happen when you give it only one argument? Verify your results experimentally.
c. What do you think should happen when you give it two arguments? Verify your results experimentally.
d. What do you think should happen when you give it three arguments? Verify your results experimentally.
Define and test a procedure clicker
that takes one or more
arguments, of which the first must be an integer and each of the others must
be either the symbol 'up
or the symbol 'down
.
Clicker
should start from the given integer, add 1 for
each 'up
argument, subtract 1 for each 'down
argument, and return the result:
> (clicker 17 'up 'up) 19 > (clicker -12 'down 'up 'down 'down 'down) -15 > (clicker 100) 100
In writing, we often separate the last element of a list using a
different separator than for the prior elements. For example, we
might separate the all but the last element with commas and the last
element with "and". Extend display-separated-line
so
that it requires two parameters (the default separator and the final
separator) and supports as many the client provides.
Tuesday, 31 October 2000 [Samuel A. Rebelsky]
http://www.math.grin.edu/~stone/courses/scheme/variable-arity.xhtml
and is now at
http://www.cs.grinnell.edu/~stone/courses/scheme/spring-2000/variable-arity.xhtml
.
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000F/Labs/variable-arity.html
.
Thursday, 12 April 2001 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/Labs/variable-arity.html
.
Monday, 12 November 2002 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Labs/variable-arity.html
.
Friday, 7 February 2003 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Labs/variable-arity.html
.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue May 6 09:20:06 2003.
The source to the document was last modified on Fri Feb 7 07:09:54 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Labs/variable-arity.html
.
You may wish to
validate this document's HTML
;
;
Check with Bobby