Your name:
(+ (* 2 3) (+ 8 2))16
(cadddr '(a b c d e))D
(first (rest (rest '((a) (b) (c)) )))(C)
(setf x '(1 2 3))
(setf y '(4 5 6))
(let ((x '(a b))
(y '(x y z)))
(append x y))
(A B X Y Z)
(mapcar #'(lambda (x) (cons x nil)) '(a b c))((A) (B) (C))
(setf x '(1 2 3))
(setf y '(4 5 6))
(let ((x '(a b))
(y x))
(append x y))
(A B 1 2 3)
square that takes a list and returns
a new list with each element squared. Here are some examples of
using twice:
(square '(1 2 3)) evaluates to (1 4 9) (twice '()) evaluates to NIL (twice '(4 3 1 2)) evaluates to (16 9 1 4)
You don't need to worry about an argument that isn't a list of integers.
(defun (s)
(if (null s) nil
(cons (* (first s) (first s)) (twice (rest s)))))
OrderedCollection new will make a
new, empty instance of OrderedCollection. If c is an
instance of OrderedCollection, evaluating c add: x will
add an element x at the end of c. Write a
square method for OrderedCollection that returns a new
ordered collection with each element squared. For example, if
c contains 1, 2, and 3, then c twice should
return a new ordered collection consisting of 1,4,9. You can assume
the collection contains only members which respond to the * operator.
square
| c |
c := OrderedCollection new.
self do: [:element | c add: (element * element)].
^ c
square rule in Prolog.
square([],[]). square([X|Xs],[Sq|Ys]) :- Sq is X*X, square(Xs,Ys).
m1, and another method m2, as follows:
m1 message to an instance of
A? To an instance of B? To an instance of C?
When we send the message m1 to an instance of A,
m1 here in class A m2 here in class Ais printed. (Well, actually, they would be run together on the same line, since there isn't a carriage return, but I don't care whether you put that in your answer.)
When we send the message m1 to an instance of B,
m1 here in class B m1 here in class A m2 here in class Bis printed.
When we send the message m1 to an instance of C,
m1 here in class C m1 here in class B m1 here in class A m2 here in class Cis printed.
weight,
size, and
Object
ElectronicDevice
Computer
DiskDrive
HardDiskDrive
FloppyDiskDrive
CPU
Pentium
Monitor
In other words, Computer and DiskDrive are both subclasses of
ElectronicDevice, etc. Note in particular that for example DiskDrive
is NOT a subclass of Computer -- a disk drive is a part of a computer,
but a disk drive is not a kind of computer.
An instance of Computer should have instance variables
hardDisk, floppy, cpu, and
monitor, which are instances of
HardDiskDrive, FloppyDiskDrive,
CPU, and Monitor respectively. In other
words, a computer has as its parts a hard disk drive, a floppy disk
drive, a CPU, and a monitor (plus some other stuff which wasn't listed
in this question and so doesn't need to be part of your answer).
append([],Ys,Ys). append([X|Xs],Ys,[X|Zs]) :- append(Xs,Ys,Zs). member(X,[X|Xs]). member(X,[Y|Ys]) :- member(X,Ys).What answers are produced for the following goals? If there is more than one, give all of the answers. If at some point Prolog gets into an infinite search, say so.
?- member(A,[1,2,3]).
A = 1 ; A = 2 ; A = 3 ; no
?- append(A,[B],[1,2,3]).
A = [1,2], B = 3 ; no
?- member(A,[1,2,3,4]), member(A,[2,3,4,5,6]).
A = 2 ; A = 3 ; A = 4 ; no
?- member(4,A), append(A,B,[1,2,3,4,5]).
A = [1,2,3,4], B = [5] ; A = [1,2,3,4,5], B = [] ; [Prolog goes into an infinite search at this point]
?- append(A,B,[1,2,3,4,5]), member(4,A).
A = [1,2,3,4], B = [5] ; A = [1,2,3,4,5], B = [] ; no
not(X) :- call(X), !, fail. not(X). append([],Ys,Ys). append([X|Xs],Ys,[X|Zs]) :- append(Xs,Ys,Zs).What answers are produced for the following goals? If there is more than one, give all of the answers. If at some point Prolog gets into an infinite search, say so.
?- not(X=Y), X=3, Y=4.no
?- X=3, not(X=Y), Y=4.
no
?- X=3, Y=4, not(X=Y).
X = 3, Y = 4 ; no
?- append(X,Y,[1,2,1,2]), not(X=Y).
X = [], Y = [1,2,1,2] ; X = [1], Y = [2,1,2] ; X = [1,2,1], Y = [2] ; X = [1,2,1,2], Y = [] ; no
?- not(append([1,2],[3,4,5],[1,2,3,4,5])).
no
flatten(S,F) :- flatten_dl(S,F\[]). flatten_dl([],X\X). flatten_dl([X|Xs],Y\Z) :- flatten_dl(X,Y\T), flatten_dl(Xs,T\Z). flatten_dl(X,[X|Z]\Z).Suppose that one replaced the base case of the recursion with
flatten_dl([],[]\[]).Indicate which of the following statements are true and which are false.
In the answers I put in explanations -- you don't need these though in your answers.
False. (It will fail in all cases except flattening an empty list.)
False. []\[] IS a correct difference list representation of the empty list.
False (see above).
False (see above).
True (see above).
True
False. All the rules are logically correct -- the new rule for the base case isn't general enough though.
True (see above).
(clam 7) is
evaluated. Explain your reasoning.
Here is the first sequence:
(setf x 4) (setf y 3) (defun clam (x) (squid)) (defun squid () (+ x 100)) (clam 7)In this case 104 is returned. We evaluate
(clam 7). This
calls the clam function, binding x to 7 within
the body of clam. In clam we call
squid. The squid function evaluates (+ x
100). The variable x is not local to
squid. Since it is lexically scoped, we find the global
binding of x, which is 4, and return 104. Then 104 is
returned from clam as well.
Here is the second sequence -- the only difference from the first is that x and y are declared as dynamically scoped variables.
(defvar x 4) (defvar y 3) (defun clam (x) (squid)) (defun squid () (+ x 100)) (clam 7)In this case 107 is returned. We evaluate
(clam 7). This
calls the clam function, binding x to 7 within
the body of clam. In clam we call
squid. The squid function evaluates (+ x
100). The variable x is not local to
squid. In this case, it is dynamically scoped. So we look up
the calling stack, and find the binding in clam, which is 7,
and return 107. Then 107 is returned from clam as well.