# CSE 341, Prolog Assignment

## Due Monday, November 10, 1:30 pm.

Write prolog relations for the following problems:
1. Write a routine for adding a pair of polynomials with integer coefficients where a dense representation is used. (Hint, if you want to require that ` H1 + H2 = H3 `, then write it as ` H3 is H1 + H2 `.
2. Write a routine for adding a pair of polynomials with integer coefficients where a sparse representation is used. Represent a coefficient/exponent pair as a two element list, so the polynomial 2x + 3x4 is `[[2,1], [3,4]]`. You may assume that the terms are in order of increasing exponent.
3. Assume that Wines are represented as lists of four elements - [year, variety, winery, price]. Write routines to:
• Find all wines of a given year (`vintage(Y, L1, L2)`)
• Find all wines of a given variety and winery (`wineType(W, V, L1, L2) `)
• Find the price of the cheapest wine (`minPrice(L, P) `)
• Find the cheapest wine (`minPriceWine(L, P)`)
Here is a script of my solution. The wine functions take as input a list of all of the wines. You could of course, type in this list every time, but you probably would rather just assign it to a variable - but you can't do that in prolog. So, in my source file I declare the wineList predicate, with the following fact:
```wineList([[1996, chardonnay, gallo, 6.95],
[1997, pinotnoir, gallo, 5.95],
[1995, burgundy, ripple, 3.95],
[1996, cabernet, gallo, 4.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],
```
Now when I want to find the minimum price wine in the list, I make the following query:
```7 ?- wineList(L), minPrice(L, P).
```
The meaning of the query is, find a list L and value P, such that L statisfies the wineList predicate, and L and P satisfy the minPrice relation. Since the only list which satisfies the wineList predicate is the given list, this has the effect of assigning the list to L. Yes, this does seem like a bit of a hack.

Here is the script:

```
Script started on Mon Nov 03 13:07:33 1997
orcas% prolog
Welcome to SWI-Prolog (Version 2.9.5)

For help, use ?- help(Topic). or ?- apropos(Word).

1 ?- [assign].
assign compiled, 0.02 sec, 11,800 bytes.

Yes
2 ?- sumPoly([1,2,3], [1, -2, 3, -4, 5], L).

L = [2, 0, 6, -4, 5]

Yes
3 ?- sumDense([[1, 2], [3, 5]], [[1, 1], [2, 2], [3, 3]], L).

L = [[1, 1], [3, 2], [3, 3], [3, 5]]

Yes
4 ?- wineList(L).

L = [[1996, chardonnay, gallo, 6.95], [1997, pinotnoir, gallo, 5.95], [1995, burgundy, ripple, 3.95], [1996, cabernet, 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]]

Yes
5 ?- wineList(L), vintage(1996, L, L1).

L = [[1996, chardonnay, gallo, 6.95], [1997, pinotnoir, gallo, 5.95], [1995, burgundy, ripple, 3.95], [1996, cabernet, 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]]
L1 = [[1996, chardonnay, gallo, 6.95], [1996, cabernet, gallo, 4.95], [1996, bordeaux, ripple, 2.99], [1996, burgundy, maddog, 3.95]]

Yes
6 ?- wineList(L), wineType(chardonnay, gallo, L, L1).

L = [[1996, chardonnay, gallo, 6.95], [1997, pinotnoir, gallo, 5.95], [1995, burgundy, ripple, 3.95], [1996, cabernet, 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]]
L1 = [[1996, chardonnay, gallo, 6.95], [1997, chardonnay, gallo, 5.95], [1994, chardonnay, gallo, 16.99]]

Yes
7 ?- wineList(L), minPrice(L, P).

L = [[1996, chardonnay, gallo, 6.95], [1997, pinotnoir, gallo, 5.95], [1995, burgundy, ripple, 3.95], [1996, cabernet, 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]]
P = 2.95

Yes
8 ?- wineList(L), minPriceWine(L, W).

L = [[1996, chardonnay, gallo, 6.95], [1997, pinotnoir, gallo, 5.95], [1995, burgundy, ripple, 3.95], [1996, cabernet, 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]]
W = [1997, bordeaux, ripple, 2.95]

Yes
9 ?- halt.
orcas% ^D
script done on Mon Nov 03 16:10:44 1997

```