| CS 291 | University of Puget Sound | Spring, 2020 |
|
Programming Language Paradigms:
|
||
|
Explorations with Functional Problem Solving (supported by Scheme/Haskell)
and Declarative Prblem Solving (supported by Prolog) |
||
This lab introduces the specification of comments within Scheme programs, and some Scheme predicates — procedures that return Boolean values (true #t or false #f).
Although this lab focuses on comments and predicates, the lab begins with two useful notes.
Arithmetic operations as an n-ary operations: In many computer languages, the usual arithmetic operations ( +, −, *, / ) are binary operations — they apply only to 2 operands. Scheme, however, defines these operators as applying to as many operands as desired.
For example, the volume of a sphere of radius r is given by 4/3 Pi r3. If the radius r is 5, then Scheme allows this to be computed in one statement as follows:
(define volume (* 4/3 3.14159265358979 5 5 5))
The Quote Procedure: Sometimes we want Scheme to print the symbol, not its value. For example, suppose we have defined the symbol pi:
(define pi 3.14159265358979)
Then we might want to print pi, not 3.14159265358979. This is done with the quote procedure:
(quote pi)
In this context, pi is returned (and printed), rather than the value that pi represents.
(quote pi) may be abbreviated 'pi . Try typing this at the keyboard as well.
As you know from your past experience, programs accomplish at least two tasks. First, computers can interpret program syntax and execute the instructions specified. Second, human beings can read the code to understand what it does and how it does it. To accomplish this second task, a programmer should include in the program explanations directed to the human reader. (These explanations also can help the programmer clarify what she or he is thinking.) While the computer ignores these explanations, they are an essential part of the program, because communicating with human readers is an essential part of the programmer's job.
A Scheme comment begins with a semicolon and extends to the end of a line. A multi-line comment can be constructed by starting each of the lines with a semicolon. For instance, the instructions in the supplemental problems (Scheme) for this course state that each program listing you contain comments giving your name, your mailbox number, and an identification of assignment being solved. For example:
;;;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ;;; Henry M. Walker ;;; Math/CS Office Box ;;; Supplemental Problem 2 ;;;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ;;; Academic Honesty Certification ;;; Written/online sources used: ; class reading on comments and predicates ; class lab on installing the environment ;;; Help obtained ; ITS help desk regarding local software installation ; ;;; Signature: ;;;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Although one semicolon is enough to begin a comment, most Scheme programmers use several semicolons to indicate the scope or importance of a comment. A three-semicolon comment line is more likely to apply to the program as a whole, or to be extremely important; a similar line beginning with just one semicolon is likely to apply only to the line that immediately precedes or follows it and to be an off-hand remark.
| Predicate | Example that returns True (#t) | Comment |
|---|---|---|
| number? | (number? 3.1415) | Is argument a number? |
| symbol? | (symbol? 'pi) | Is argument a symbol? |
| boolean? | (boolean? #t) | Is argument a boolean value? |
| procedure? | (procedure? sqrt) | Is argument a procedure? |
| eq? | (eq? 'a 'a) | Do arguments represent identical symbols? |
| eqv? | (eqv? 'a 'a) | Similar to eq? |
| equal? | (equal? (+ 1 1) (sqrt 4)) | Are arguments the same symbols, numbers, booleans, or lists? |
Each of these predicates return true (#t) if the specified condition is satisfied, and false (#f) if the condition is not satisfied.
|
created 22 January 1977 revised 18 January 2009 revised November-December 2019 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |