## ICS 361 -- ARTIFICIAL INTELLIGENCE PROGRAMMING

### Logic Programming

#### Assignment #6

This assignment worth 100 points.

1. (20 P) Create a PROLOG program LISTLENGTH that counts the number of items (at the top level) in a list.
```LISTLENGTH([9, 5, 6, 4],L).
RETURNS ==>   L = 4.
LISTLENGTH([5, [6, 7, 8]], L).
RETURNS -->   L = 2  ```
2. (20 P) Create a recursive FACTORIAL program in PROLOG. Your program should work for Integers N >= 0 (include a check for negative numbers).
```MYFAC(0,X).
RETURNS ==> X = 1
MYFAC(3, Z).
RETURNS ==> Z = 6 ```
3. (60 P) Find solutions for each of the following colored ball problems with different sets of constraints.
• Situation #1 (15 P)
You have six colored balls: 2 GREEN, 2 BLUE AND 2 YELLOW
Constraints:
• No balls of the same color may be adjacent to one another.
• Situation #2 (15 P)
You have six colored balls: 4 BLACK, 1 RED AND 1 BLUE.
• There are no more than 2 BLACK balls in a row.
• Situation #3 (30 P)
You have eight colored balls: 1 BLACK, 2 WHITE, 2 RED AND 3 GREEN.
1. The balls in positions 2 and 3 are not GREEN.
2. The balls in positions 4 and 8 are the same color.
3. The balls in positions 1 and 7 are of different colors.
4. There is a GREEN ball to the left of every RED ball.
5. A WHITE ball is neither first nor last.
6. The balls in positions 6 and 7 are not RED.

Write PROLOG programs to find all solutions to each situation. Print your solutions with nice (compact) formatting (e.g. not one color per line).
Mathematically calculate how many solutions exist to each of the above problems with each of the two situations below:

1. Balls with the same color are indistinguishable.
2. All balls are distinct, even those with the same color. I.E. For each color (1 to M), each ball of that color has a distinct identifier on it. This means that for each color I, there will be CI-1, CI-2, .. CI-NI where NI is the number of balls of color I.
You may choose to create PROLOG programs for each as well as mathematical justifications. Compare the solutions found by your program to your mathematical calculations.
4. (25 P) Extra Credit - Write a functor named SPLIT3(N,L) that takes two parameters: N, A POSITIVE INTEGER, AND L, A LIST OF POSITIVE INTEGERS. You may assume that the list L is flat, i.e., it does not contain sublists.
The functor SPLIT3 returns TRUE if the list L can be partitioned into three sublists (with elements in the same oder), such that the sum of the integers in each subset is less than or equal to N. Otherwise, it returns FALSE. Examples:
```?-SPLIT3(5,[]).
FALSE
?-SPLIT1(6,[5, 5, 12]).
FALSE
?-SPLIT3(14,[6, 5, 10, 1, 1, 1, 14]).
TRUE
?-SPLIT3(5,[3, 1, 4, 1, 2]).
TRUE
?-SPLIT3(6,[4, 3, 5, 2, 1]).
FALSE
?-SPLIT3(7,[4, 5, 7, 2, 3]).
FALSE
?-SPLIT3(8,[3,5,4,2,7,1]).
TRUE.
?-SPLIT3(3,[1, 2, 2, 2, 1, 1]).
FALSE
```

### Turn in the following:

Plain text files containing your code and scripts. A report with your results and discussion in a standard format like rtf, pdf, or text.

(c) N. E. Reed, 2004-2016