class Foo implements Sortable
) and why coding "to an interface"
increases code reusability
a
is an instance of class A
, and object b
is an instance
of class B
, and B is a subclass of A, then the amount of data
in b
is less than or equal to the amount of data in a
.
Why is this true or false?
<face> ::= <eyes><mouth> | <mouth><eyes> <eyes> ::= ":" | ";" <mouth> ::= "(" | ")" | "<" | ">"
{-:#
',
`]=:
', `[^:
', `(v:
'and similar ones.
<hair> ::= "#" | "<!-- %" --> <brow> ::= "<" | ">" | "/" | "\" | "|" <eyes> ::= ":" | ";" | "!" <nose> ::= "-" | "~" | "=" | "+" | "v" | "^" <mouth> ::= "(" | ")" | "<" | ">" | "[" | "]" | "{" | "}" | "O" | "C" | "D" | "Q"Write a revised set of grammar rules for
so that
:+Q
D-;|
(defun weird (i) (let ((i (* i 2))) (let ((i (* i 3))) i)))
(defun w1 (i) (setq i 1) i) (defun w2 (i) (setf (car i) 2) 'i) (defun w3 () (let ((i 3) (j '(4 5))) (w1 i) (w2 j) (list i j)))
what are the values of (w1 3)
, (w2 '(4 5))
, and
(w3)
?
mapcar
applies a function to each
element of a list and returns a list of all results. Implement it in Lisp
recursively without using any other mapping function or global variables.
You may not use `map
', `mapc
', `mapcan
',
etc. You will at least need `if
', `null
', `cons
',
`car', `cdr', and `apply
'. Examples:
(defun add (lst) (+ (car lst) (cadr lst))) (mapcar #'add '()) => () (mapcar #'add '((1 2) (3 4))) => (3 7) (mapcar #'(lambda (i) (+ i 1)) '(1 2 3 4)) => (2 3 4 5)
Write mapcar
for 2 arguments only -- don't worry the extra
cases described in books.
iota
as a LISP function that takes a positive integer
as an argument and returns a list of the numbers between 1 and that integer.
clisp> (iota 5) (1 2 3 4 5) clisp> (iota 0) nil clisp> (iota -2) nil
(defun mystery (lis a) (cond ((null lis) a) (t (mystery (cdr lis) (cons (car lis) a))) ))
Give the values of the following expressions, explaining your reasoning.
- (mystery '() '(1 2 3 4 5))
- (mystery '(1 2 3 4 5) '())
Clisp> (defun foo (x) (setf (car x) 1)) Clisp> (setf y '(a b c)) Clisp> (foo y) Clisp> y
member/2
. For each version,
simulate repeated attempts to satisfy the goal member(X,[a,b,c,d])
and give the sequence of instantiations of X
which are produced
from these attempts.
member(X,[X|_]). member(X,[_|Y]) :- member(X,Y).
member(X,[X|_]) :- !. member(X,[_|Y]) :- member(X,Y).
member(X,[X|_]). member(X,[_|Y]) :- member(X,Y),!.
male(paul):-!. male(harry). female(sue). likes(paul,X):-female(X). likes(X,jody):-male(X). likes(paul,jody):-!. likes(X,jody):-female(X).
Give the sequence of all satisfactions of ?- likes(X,Y).
in
order.
?- fun(X,[A|B])=fun(a,[B,c,d]).
?- [A,b,[C,d]]=[[b,C],C|T].
?- [a,b]=[A]
. interlace([],[],[]). interlace(A,X,B):-A=[C|T],B=[D|U],C=D,interlace(T,X,U). interlace(X,A,B):-A=[C|T],B=[D|U],C=D,interlace(X,T,U).
?- interlace([a,b],[c,d],X).
foo(a,100). foo(b,200). foo(c,150). foo(c,200):-!. foo(c,250). foo(X,Y):-foo(X,_),!,foo(_,Y).
Give all instantiations of variables (in order) for repeated satisfaction attempts of the goals:
?- [a,b,c]=[H,I|T].
?- [X,a,Y]=[[c,d]|[a,X]].
?- foo(X,Y).
?- foo(b,Y).
?- foo(X,150).
iota
that takes a single argument that
is a positive integer, and returns a list of the integers between 1 and that
argument(iota
is handy when you want to write a `for' loop in
prolog. iota(X,Y),member(Z,Y)
is the same as `for Z from 1 to
X'.). ?- iota(5,X). X=[1,2,3,4,5]; no ?- iota(0,X). no ?- iota(-10,X). no