ASSIGNMENT 2. Due December 7.
-----------------------------
1. Write a program for factorial computation using an accumulator.
=================================================
2. Write a program sum_and_squaresum that from a given list of
numbers finds the sum of its elements and the sum of their squares.
Use accumulators. Sample run:
?- sum_and_squaresum([1,-3,2,0],Sum,SQS).
Sum = 0
SQS = 14 ;
No
========================================================
3. Write a predicate twice_as_long(L1,L2) that succeeds if the list
L2 is twice as long as the list L1. Do NOT compute the lengths of
the lists. Sample run:
?- twice_as_long([],[]).
Yes.
?- twice_as_long([a],[1,2]).
Yes.
?- twice_as_long([a,b],X).
X = [_G328, _G331, _G334, _G337] ;
No
?- twice_as_long(X,[_,_]).
X = [_G328] ;
No
?- twice_as_long([_],X).
X = [_G313, _G316] ;
No
========================================================
4. Write a predicate fib(N,F) that is true if F is the Nth Fibonacci number.
Compute fib(5,F), fib(10,F), fib(50,F).
========================================================
5. Implement Gauss algorithm for calculating the date of Easter Sunday:
http://en.wikipedia.org/wiki/Computus#Gauss.27s_algorithm
========================================================
6. Write a predicate substitute(X,Y,Xs,Ys) that is true if the list Ys is the result of
substituting Y for all occurrences of X in the list Xs.
=======================================================
7. Write a predicate without_doubles_1(Xs, Ys) that is true if Ys is
the list of the elements appearing in Xs without duplication. The
elements in Ys are in the same order as in Xs with the last
duplicate values being kept.
Sample run:
?- without_doubles_1([1,2,3,4,5,6,4,4],X).
X = [1, 2, 3, 5, 6, 4];
No
=======================================================
8. Write a predicate without_doubles_2(Xs, Ys) that is true if Ys is
the list of the elements appearing in Xs without duplication. The
elements in Ys are in the reversed order of Xs with the first
duplicate values being kept.
Sample run:
?- without_doubles_2([1,2,3,4,5,6,4,4],X).
X = [6, 5, 4, 3, 2, 1];
No
=======================================================
9. Given a list of elements colored red, white, and blue, reorder
the list so that all the red elements appear first, then all the
white elements, followed by the blue elements. The reordering should
preserve the original relative order of elements of the same color.
Sample run:
?- reorder([red(1), white(2), blue(3), red(4), white(5)], Reordered).
Reordered = [red(1), red(4), white(2), white(5), blue(3)];
no
Write two versions of the reordering program: with and without difference
lists.
=======================================================
10. Implement two versions of the quicksort algorithm: with and without
difference lists.
=======================================================