- The complex number x + yi can be represented in scheme by (cons x y). Write a routine to add a pair of complex numbers. Your routine should also take real numbers as inputs.
- Write a routine for adding a pair of polynomials with complex (or real coefficients) where a dense representation is used.
- Write a routine for adding a pair of polynomials with complex (or real coefficients) where a sparse representation is used.
- Write a routine for filtering a list - keeping only the elements of the list which satisfy the given predicate.
- Assume that Wines are represented as lists of four elements -
(Year Variety Winery Price). Write routines to:
- Find all wines of a given year
- Find all wines of a given variety and winery
- Find the price of the cheapest wine
- Find the cheapest wine

Below is a script showing my solutions.

Script started on Thu Oct 23 15:46:14 1997 orcas% scheme Scheme Microcode Version 11.146 MIT Scheme running under OSF 1 ]=> (load "s1.l") ;Loading "s1.l" -- done ;Value: cheapest-wine ;; 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 ;; You may want to write additional functions which convert from real to ;; complex, and from complex to real 1 ]=> (complex-add 1 2) ;Value: 3 1 ]=> (complex-add 1 '(1 . 2)) ;Value 1: (2 . 2) 1 ]=> (complex-add '(1 . 2) '(2 . 2)) ;Value 2: (3 . 4) 1 ]=> (complex-add '(1 . 1) '(1 . -1)) ;Value: 2 ;; Polynomial addition ;; The dense representation is just the list of coefficients (both real ;; and complex) 1 ]=> (define p1 '(1 0 (1 . 1) (2 . -1))) ;Value: p1 1 ]=> (define p2 '(0 2 (3 . 1) (2 . 1))) ;Value: p2 1 ]=> (dense-poly-add p1 p2) ;Value 3: (1 2 (4 . 2) 4) ;; And now sparse representation - a list of coefficient, exponent ;; pairs (with the coefficients possibly complex numbers) 1 ]=> (define s1 '((1 . 2) (1 . 4) ((2 . 2) . 6) (10 . 100))) ;Value: s1 1 ]=> (define s2 '((1 . 0) (3 . 2) ((3 . 2) . 3) ((1 . 1) . 4) (7 . 7))) ;Value: s2 1 ]=> (sparse-poly-add s1 s2) ;Value 4: ((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. ;; We will test it with both a built in function, and an anonymous function 1 ]=> (define number-list '(1 two 3 four 5)) ;Value: number-list 1 ]=> (filter number? number-list) ;Value 5: (1 3 5) 1 ]=> (filter (lambda (x) (equal? x 3)) number-list) ;Value 6: (3) ;; A list of fine wines for testing the wine routines 1 ]=> (define 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))) ;Value: wines ;; List the wines of a given year 1 ]=> (vintage 1997 wines) ;Value 7: ((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 the wines of a given variety and winery 1 ]=> (wine-type 'bordeaux 'ripple wines) ;Value 8: ((1996 bordeaux ripple 2.99) (1997 bordeaux ripple 2.95)) ;; Find the minimum price 1 ]=> (cheapest-price wines) ;Value: 2.95 ;; Find a wine of minimum price (note that this wine is not unique) 1 ]=> (cheapest-wine wines) ;Value 9: (1997 pinotnoir oldtavern 2.95) 1 ]=> ^D End of input stream reached Happy Happy Joy Joy. orcas% script done on Thu Oct 23 15:56:35 1997