Professional Documents
Culture Documents
SCHEME: LAB 1
1 - Convert the following arithmetic expressions into Scheme expressions and evaluate them:
2 - Convert the following Scheme expressions into arithmetic expression and evaluate them.
The first element of a list is often called the "car" of the list, and the rest of the list is often called the
"cdr" of the list. The “cdr” of a list with one element is (), the empty list.
Example:
The procedure “cons” constructs list. It takes two arguments. The second argument is usually a list,
and in that case “cons” returns a list.
Example:
and
The expressions are evaluated from left to right, and the value of the first expression that evaluates to
a false value is returned. Any remaining expressions are not evaluated. If all the expressions evaluate
to true values, the value of the last expression is returned. If there are no expressions then #t is
returned.
or
The expressions are evaluated from left to right, and the value of the first expression that evaluates to
a true value is returned. Any remaining expressions are not evaluated. If all expressions evaluate to
false values, the value of the last expression is returned. If there are no expressions then #f is returned.
Example:
1. “eq?” is pointer comparison. It returns #t if its arguments literally refer to the same objects in
memory. Symbols are unique ('fred always evaluates to the same object). Two symbols that look
the same are “eq”. Two variables that refer to the same object are “eq”.
2. “eqv?” is like “eq?” but does the right thing when comparing numbers. “eqv?” returns #t if its
arguments are “eq” or if its arguments are numbers that have the same value. “eqv?” does not
convert integers to floats when comparing integers and floats though.
3. “equal?” returns true if its arguments have the same structure. Formally, we can
define “equal?” recursively. “equal?” returns #t if its arguments are “eqv”, or if its arguments are
lists whose corresponding elements are equal (note the recursion). Two objects that are “eq” are
both “eqv” and equal. Two objects that are eqv are equal, but not necessarily eq. Two objects that
are equal are not necessarily eqv or eq.eq is sometimes called an identity comparison and equal is
called an equality comparison.
Example:
“define” is a special form used to define other functions. Typically, you equate a function symbol and
a set of parameters with a parameterized expression. When you invoke the specified function, you are
really evaluating the expression associated with the function name.
(define (printMessage)
(display "Hello world."))
1 - Write a simple function in Scheme that is able to calculate the total salary of an employee when
given its current monthly salary and bonus received.
(+ monthlySalary bonus))
2 - Write a simple function in Scheme that is able to calculate and display the average marks for five
quizzes.