This assignment is due Thursday, May 1, at the start of section. You are to do this assignment individually . Write functions for the following problems in common lisp.
There is a lot of on-line documentation available for lisp. The tutorial by Geoffry Gordon is a good starting point. The Common Lisp Manual has more information that you will ever want.
Below is a script showing my solutions:
Script started on Tue Apr 22 13:06:21 1997
% cl
Allegro CL 4.1 [DEC MIPS; R1] (9/21/92 16:49)
USER(1): (load 'PROG.L)
;; Problem 1 - addition of complex numbers
;; Note the representation of numbers:
;; x + iy is represented as (cons x y), which can be expressed as (x . y)
;; The complex addition routines should handle both complex and real numbers
USER(6): (setf c1 '(1 . 2) c2 '(2 . 3) c3 '(2 . -3))
(2 . -3)
USER(7): (complex-add c1 c2)
(3 . 5)
USER(8): (complex-add c2 c3)
4
USER(9): (complex-add c1 3)
(4 . 2)
USER(10): (complex-add 1 4)
5
;; Polynomial addition
;; The dense representation is just the list of coefficients (both real
;; and complex
USER(11): (setf p1 '( 1 (2 . 3) 0 (2 . 2)))
(1 (2 . 3) 0 (2 . 2))
USER(12): (setf p2 '( 0 (3 . 5) (3 . 3) (2 . -2) 2 (3 . 2)))
(0 (3 . 5) (3 . 3) (2 . -2) 2 (3 . 2))
USER(13): (dense-poly-add p1 p2)
(1 (5 . 8) (3 . 3) 4 2 (3 . 2))
;; And now sparse representation - a list of coefficient, exponent
;; pairs (with the coefficients possibly complex numbers)
USER(14): (setf s1 '((1 . 2) (1 . 4) ((2 . 2) . 6) (10 . 100)))
((1 . 2) (1 . 4) ((2 . 2) . 6) (10 . 100))
USER(15): (setf s2 '((1 . 0) (3 . 2) ((3 . 2) . 3) ((1 . 1) . 4) (7 . 7)))
((1 . 0) (3 . 2) ((3 . 2) . 3) ((1 . 1) . 4) (7 . 7))
USER(16): (sparse-poly-add s1 s2)
((1 . 0) (4 . 2) ((3 . 2) . 3) ((2 . 1) . 4) ((2 . 2) . 6) (7 . 7) (10 . 100))
;; Filtering a list - keep the elements which are true for the predicate.
;; Note that the numberp predicate has to be marked as a function
USER(17): (setf nlist '(1 two 3 four 5 six 7))
(1 TWO 3 FOUR 5 SIX 7)
USER(18): (filter #'numberp nlist)
(1 3 5 7)
;; A list for testing wine routines
USER(19):(setf wines '((1996 Chardonnay Gallo 6.95) (1997 PinotNoir Gallo 5.95)
(1995 Burgundy Ripple 3.95) (1996 CabernetSauvignon Gallo 4.95)
(1997 PinotNoir MadDog 3.95) (1997 Chardonnay NightTrain 4.99)
(1996 Bordeaux Ripple 2.99) (1997 Bordeaux Ripple 2.95)
(1997 Chardonnay Gallo 5.95) (1994 Chardonnay Gallo 16.99)
(1997 PinotNoir OldTavern 2.95) (1996 Burgundy MadDog 3.95)))
;; List wines of a given year
USER(20): (vintage 1997 wines)
((1997 PINOTNOIR GALLO 5.95) (1997 PINOTNOIR MADDOG 3.95)
(1997 CHARDONNAY NIGHTTRAIN 4.99) (1997 BORDEAUX RIPPLE 2.95)
(1997 CHARDONNAY GALLO 5.95) (1997 PINOTNOIR OLDTAVERN 2.95))
;; List wines of a given variety and winery
USER(25): (wineType 'Bordeaux 'Ripple wines)
((1996 BORDEAUX RIPPLE 2.99) (1997 BORDEAUX RIPPLE 2.95))
;; Determine the minimum price of a wine
USER(26): (elCheapo wines)
2.95
;; list all of the cheapest wines
USER(27): (cheapwines wines)
((1997 BORDEAUX RIPPLE 2.95) (1997 PINOTNOIR OLDTAVERN 2.95))
;; list the cheapest wines of each type (note that duplicates are allowed -
;; but not required)
USER(28): (cheaplist wines)
((1995 BURGUNDY RIPPLE 3.95) (1996 CABERNETSAUVIGNON GALLO 4.95)
(1997 CHARDONNAY NIGHTTRAIN 4.99) (1997 BORDEAUX RIPPLE 2.95)
(1997 PINOTNOIR OLDTAVERN 2.95) (1996 BURGUNDY MADDOG 3.95))
USER(29): (exit)
; Exiting Lisp
script done on Tue Apr 22 13:31:55 1997