Assignment 3 - Prolog and SML

NOTE - slight update to an error. In the my_set_add predicate, if item is already in set, result should be set (not item) Assignment is due March 21st (Wednesday) at 11:00pm. Submission instructions: Submit your files with the submit command:
submit -c cscc24w12 -a A3
Submit the code files as a3.pl and a3.sml.

Your Prolog will be evaluated with swipl on mathlab. Your ml with be evaluated with sml on mathlab.

For this assignment, you may work with a partner, or you may work on your own. If you choose to work with a partner, submit one assignment from one of your accounts. Include both partners' names and IDs in a header comment. You are expected to colaborate with your partner but not with other students, although you may discuss syntax and general approach with others.

Attempt to use clear style. Your code should be lightly commented. Include a header with your name and UTorID, and that of your partner, if you have one. In Prolog, do not use singleton variables, instead use "__". A rule like: g(A,B):-A=B. should be written g(A,A). Style rules you've learned in previous courses still apply.

Your Prolog code should not produce duplicate results, or end in an infinite loop. Use careful construction of your predicates, and cuts where appropriate, to ensure this.

For both Prolog and ML, you may assume your input is valid. Document any unclear cases (if you've chosen them as valid or invalid) and what you know of the behaviour for invalid input or unclear cases.

Please let me know of any ambiguities in this assignment specification as soon as possible.

Prolog Practice (36)

Set Predicates

We've been working with order lists in Prolog, that is

[1,2,3,4,5] and [5,4,3,2,1]

are different lists. Without using any of the built-in set predicates, your task is to build predicates to manipulate sets and test set properties. Sets can be considered unordered lists, with no duplicate elements allowed. Implement the following predicates:

my_set_add(+item, +set, ?result)

This predicate should return true if result is set, with item added. If item is already in the set, result should be equal to set. item and set should be input, result may be input, but you should be able to query on it.

my_set_union(?set1, ?set2, ?union)

union is the union of set1 and set2. Your order may anything you would like, although it should be documented. You should be able to query on any one of the inputs.

my_set_intersection(?set1, ?set2, ?intersection)

intersection should be the intersection of set1 and set2. Order may be anything, though it must be documented. You should be able to query on any one of the inputs.

my_set_equal(+set1, +set2)

Returns true if set1 contains all of the same elements as set2.

my_subsets(+set,?subsets)

should be true iff subsets is the unordered list of all subsets of set. You should be able to query on subsets, and get a list of all subsets of set. Order should not matter, you may choose the order of elements in the subsets list returned.

Each predicate should be documented with a comment, about a paragraph in length, explaining your reasoning, and how the code will execute. This comment should make it clear that you understand how each predicate will be executed.

Gentle introduction to SML (24)

All ML code should include explicit type declarations. All the rules for nice recursion and use of higher order functions from Scheme still apply. Your functions should be lightly commented. You may create helper functions as necessary, use anonymous functions if these helpers are only used once. If you have read ahead about let expressions, you may use them, but you are not required to do so.

A - Write a function list_of_squares which takes an integer n, and returns a list of the first n squares.

B - Palindrome. Write a function palindrome which takes a list, and returns true if the list is the same forwards as backwards.

C - sum_of_digits. Write a function which takes and integer n, and returns an integer which is the sum of the digits of n. You may assume the number is greater than or equal to zero.

D - multiple_of_nine. Write a function which uses the function in C, which takes in an integer (may be assumed to be greater than or equal to zero) and returns true iff it is a multiple of nine.

E - intersection. Write a function, intersection, which takes two pairs of reals (m1, b1),(m2, b2), and interprets them as the slope and Y-intersept of two lines. Return a real * real tuple representing the intersection of the two lines. You should handle the case of parallel lines.