;; =====================================================================================
;; _____ __ __ ____ _ __ __ ____ ___
;; / ____/ |/ / __ \/ / / / < / __ < / Spring 2019 Section 54
;; / / / /|_/ / /_/ / / / /_____/ / / / / /
;; / /___/ / / / ____/ /_/ /_____/ / /_/ / / Quiz 04
;; \____/_/ /_/_/ \____/ /_/\____/_/ Friday, 1st March, 2018
;;
;; .d8888. .d88b. db db db d888888b d888888b .d88b. d8b db .d8888.
;; 88' YP .8P Y8. 88 88 88 `~~88~~' `88' .8P Y8. 888o 88 88' YP
;; `8bo. 88 88 88 88 88 88 88 88 88 88V8o 88 `8bo.
;; `Y8b. 88 88 88 88 88 88 88 88 88 88 V8o88 `Y8b.
;; db 8D `8b d8' 88booo. 88b d88 88 .88. `8b d8' 88 V888 db 8D
;; `8888Y' `Y88P' Y88888P ~Y8888P' YP Y888888P `Y88P' VP V8P `8888Y'
;;
;; =====================================================================================
*** QUESTION 1 *** [ 4 points ]
AND OR
IN OUT IN OUT
#t #t -> #t #t #t -> #t
#t #f -> #f #t #f -> #t
#f #t -> #f #f #t -> #t
#f #f -> #f #f #f -> #f
*** QUESTION 2 *** [ 3 points ]
Below is the truth table for a predicate function called IMPLY?.
ONE TWO OUTPUT
#t #t #t
#t #f #f
#f #t #t
#f #f #t
As this is a predicate function, we should really not use IF or COND. However, to
explore the method I shall begin with a solution using IF.
(define imply?-if
(lambda (one two)
(if (eq? one #t)
(if (eq? two #t)
#t
#f)
#t)))
This helps us to see the logic of the situation. If ONE is true, then if TWO is
true, then output #t, but if TWO is not true then output #f; if ONE is not true,
just output #t.
So we can see that this function generally outputs #t, *except* when ONE is true
and TWO is false (i.e. NOT true).
Here is another version:
(define imply?-v2
(lambda (one two)
(if (or (eq? one #f)
(and (eq? one #t)
(eq? two #t)))
#t
#f)))
This version uses IF only once, and uses AND and OR to bring together cases which
output the same value. #t is output if ONE is false *or* ONE is true *and* two is
true; otherwise, IMPLY? outputs false.
We can refine this a step further. Remember that NOT can 'invert' output: that is,
it turns a true into a false and a false into a true. We can look at the truth
table for IMPLY? and see that it outputs true if it is *not* the case that ONE is
true and TWO is false.
(define imply?-v3
(lambda (one two)
(if (not (and (eq? one #t)
(eq? two #f)))
#t
#f)))
We can remove the EQ? predicates at this point. We're going to make another step
at the same time, and say that
(eq? two #f) --> (not two)
Here is the next version:
(define imply?-v4
(lambda (one two)
(if (not (and one
(not two)))
#t
#f)))
Finally, we can simplify again to remove the IF, since we don't need it: since
the result of evaluating the NOT expression is going to be #t or #f, we can
just output that value directly, and save ourselves an IF.
So what we end up with is this:
(define imply?-v5
(lambda (one two)
(not (and one (not two)))))
:-)
As there was no restriction on using IF or COND for this question, any of the
above examples, or any program which produced the correct output as given in
the truth table, received full credit.
*** QUESTION 3 *** [ 4 points ]
FIRST This outputs the first datum (or symbol) in a non-empty input
list.
> (first '(a b c d e f)
a
> (first '(1))
1
REST This outputs the contents of the input list *except* for the
first element. This may be a null list.
> (rest '(a b c d e f))
(b c d e f)
> (rest '(1))
()
CONS This function takes two parameters, a datum and a list, and
outputs a new list with the datum inserted at the beginning
of the list.
> (cons 'a '(2 3 4 5 6))
(a 2 3 4 5 6)
> (cons 'listy-o ())
(listy-o)
It was important to include that these *output* values, as they are functions. In
the case of CONS it is important also to include the data types and that it
outputs a new list (i.e. not just the input list with the new datum in it, but a
whole new list.)
*** QUESTION 4 *** [ 5 points ]
The QUOTH function looks like this:
(define quoth
(LAMBDA (listo)
(COND
((null? LISTO)
(printf "$ "))
(ELSE
(quoth (REST LISTO))
(PRINTF "- ~A" (FIRST LISTO))))))
Symbols missing in the question are given here in capitals.
[ 16 points ]