Cpsc 201 Problem Set 1

Cpsc 201 Problem Set 1 - 1. John C. Rimson has defined...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1. John C. Rimson has defined reverse-if as follows: The problem with this implementation is the (predicate) – which (because it is enclosed in parentheses) is a call to the (no-arguments) procedure predicate . So, one can write an if expression that will evaluate to the string “Consequence” as follows: However, if one tries to perform a similar operation using John’s reverse-if (with the alternative and consequence inverted): an error is returned. In an if expression, the first argument (the predicate) can be any expression which evaluates to a value (even if that value is a procedure): But expressions which do not evaluate to anything, will cause an error: However, John’s reverse-if requires that predicate be a no-arguments procedure, making it far less versatile than the standard if . This problem can be fixed by using the following version of reverse-if : Now, for our earlier example, reverse-if behaves as expected: But suppose I define a procedure PrintLineAndEvaluate as follows: This procedure takes a single argument, which it prints and returns as its value: Using this method as part of an if expression yield the expected results: But, a reverse-if expression does not have equivalent results:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
If is a special form which does not evaluate in the same way as a standard Scheme expression. Normally, a procedure is evaluated by first evaluating each of its arguments (in some arbitrary order) and binding these values to the process’s variables (a process being an evaluation of a procedure for a specific set of arguments). However, an if is evaluated by first evaluating the predicate. If the predicate evaluates to true, the consequence (and only the consequence) is evaluated
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/19/2008 for the course CPSC 201 taught by Professor Brianscassellati during the Fall '06 term at Yale.

Page1 / 8

Cpsc 201 Problem Set 1 - 1. John C. Rimson has defined...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online