First Quarter Test Review Problems
> (setf a '(a b c) b '(d e f))
> a --> _________________ (A B C)
> 'a --> _________________ A
> (eval 'a) --> _________________ (A B C)
> (eval a) --> _________________ Error, cannot evaluate
the expression (a b c)
> (setf c '(+ 1 2 3)) --> _________________ (+ 1 2 3)
> (eval 'c) --> _________________ (+ 1 2 3)
> (eval c) --> _________________ 6
> (cons 'a b) --> _________________ (A D E F)
> (cons '(a) b) --> _________________ ((A) D E F)
> (cons a b) --> _________________ ((A B C) D E F)
> (append '(a) b c) --> _________________ (A D E F + 1 2 3)
> (append 'a b c) --> _________________ Error: A is not of type LIST.
> (append (eval c) b) --> _________________ Error: 6 is not of type LIST.
> (append (eval 'c) b) --> _________________ (+ 1 2 3 D E F)
> (append (list (eval c)) b) --> _________________ (6 D E F)
> (apply #'first ((q w r t))) --> ________________ Error: Need to quote
the list argument
> (apply #'first '((q w r t))) --> _________________ Q
> (list 'a b 'c) --> _________________ (A (D E F) C)
> (list a b c) --> _________________ ((A B C) (D E F) (+ 1 2 3))
> (apply #'append '((1 2 3) (4 5 6))) --> _________________ (1 2 3 4 5 6)
> (apply #'list '((1 2 3) (4 5 6))) --> _________________ ((1 2 3) (4 5 6))
> (apply #'+ '((1 2 3))) --> _________________ Error: + with (1 2 3)
as argument
> (apply #'+ '(1 2 3)) --> _________________ 6
> (mapcar #'first '((1 2 3) (a b c) (d e f))) --> ___________ (1 A D)
> (apply #'first '((1 2 3) (a b c) (d e f))) --> ____________ Error: first with
(1 2 3) (A B C) (D E F)
has the wrong number of arguments.
> (apply #'first '((1 2 3))) --> _________________ 1
> (mapcar #'list '(1 2 3)) --> _________________ ((1) (2) (3))
> (mapcar #'list '(a b c)) --> _________________ ((A) (B) (C))
> (mapcar #'list '((eval a) b c)) --> _________________ (((EVAL A)) (B) (C))
> (mapcar #'oddp '(1 2 3)) --> _________________ (T NIL T)
> (mapcar #'= '(1 2 3) '(3 2 1)) --> _________________ (NIL T NIL)
Association Lists and Lambda: (see p. 97)
(defun make-book (title author classification)
(list (list 'title title)
(list 'author author)
(list 'classification classification)))
(setf book-example
(make-book '(Common Lisp)
'(Guy Steele)
'(Technical Lisp)))
--> ((TITLE (COMMON LISP)) (AUTHOR (GUY STEELE)) (CLASSIFICATION (TECHNICAL LISP)))
(defun book-author (book)
(second (assoc 'author book)))
> (book-author book-example) --> (GUY STEELE)
> (setf books (list (make-book '(moby dick) '(herman melville) (fiction))
(make-book '(tom sawyer) '(mark twain) (fiction))
(make-book '(ai) '(patrick winston) (technical ai))))
>(
((TITLE (MOBY DICK)) (AUTHOR (HERMAN MELVILLE)) (CLASSIFICATION (FICTION)))
((TITLE (TOM SAWYER)) (AUTHOR (MARK TWAIN)) (CLASSIFICATION (FICTION)))
(TITLE (AI)) (AUTHOR (PATRICK WINSTON)) (CLASSIFICATION (TECHNICAL AI)))
)
> (mapcar #'book-author books) --> ___________________ ((HERMAN MELVILLE) (MARK TWAIN) (PATRICK WINSTON))
> (mapcar #'(lambda (book) (first (last (book-author book))))
books)
--> _________________ (MELVILLE TWAIN WINSTON)
PROPERTY LISTS
> (setf (get 'rambler 'isa) 'house) --> _________________ HOUSE
> (setf (get 'colonial 'isa) 'house) --> _________________ HOUSE
> (setf (get 'house 'hasa) 'windows) --> WINDOWS
> (get 'house 'hasa) --> _________________ WINDOWS
> (get 'rambler 'isa) --> _________________ HOUSE
> (get 'rambler 'hasa) --> _________________ NIL
> (setf (get 'house 'hasa) '(windows door pool))
(WINDOWS DOOR POOL)
> (get 'house 'hasa) --> _________________ (WINDOWS DOOR POOL)
> (remove 'a '(a b c a d a e)) --> _________________ (B C D E)
> (setf (get 'house 'hasa) (remove 'pool (get 'house 'hasa)))
> (get 'house 'hasa) --> _________________ (WINDOWS DOOR)
> (defun putprop (object property-value property-type)
--> _________________ ) (setf (get object property-type)
property-value)
LOOPS AND RECURSION
(defun mysteryStudy (x)
(cond ((null x) nil)
((numberp (first x))
(cons (first x) (mysteryStudy (rest x))))
(T (mysteryStudy (rest x))))
> (mysteryStudy '(a 12 c 7)) --> _________________ (12 7)
Tail recursive version 1:
(defun mysteryStudy-Tail (x)
(mysteryStudy-aux _____________)) --> x nil
(defun mysteryStudy-aux (x ans)
(cond ((null x) _______) --> ans
((numberp (first x))
_______________________________________________)
(T _________________________________)
--> (mysteryStudy-aux (rest x) (append ans (list (first x))))
--> (mysteryStudy-aux (rest x) ans)))
OR USE &optional:
(defun mysteryStudy-Tail (x &optional (ans nil))
(cond ((null x) ans) etc. No aux procedure is needed
DOLIST VERSION:
(defun mysteryStudy-Loop (x)
(let ((ans nil))
(dolist (el x _____) --> ans
(if ________________ --> (numberp el)
________________ --> (setf ans (append ans (list el)))
)
)
)
)
Also try to write the same version using a general DO loop.
MATCHING
Practice Matching problem.
Practice Matching problem - Answers.
SEARCHES
There will be an A* search tracing question.
Best first heuristic function f = h (for example, h is longitudinal distance)
A* heuristic function f = g + h (for example, g is actual distance traveled
so far from the START node,
and h is 10*longitudinal distance)