RU beehive logo ITEC dept promo banner
ITEC 380
2008fall
ibarland

homeinfolecturesexamsarchive

hw05
prolog exercises

Write the following Prolog predicates:

All of the (non-extra-credit) predicates fail if the first argument is not a list (unless, of course, it is a variable).
You can refer to any of prolog notes linked to from lect11b—prolog.

Here are some examples:

last([1,2,3], 3).
Yes
last([1,2,3], 4).
No

last([1,2,3], Y).
Y=3

last([], Y).
No

last(Y, 3).
Y=[3].

nextToLast([1,2,3], 2).
Yes

nextToLast([1,2,3], 3).
No

nextToLast([1,2,3], Y).
Y=2

nextToLast([1], Y).
No

nextToLast(Y, 3).
Y=[3, _h114].      % does not have to be 114

lastTwoReversed([1,2,3], Y).
Y=[3,2]

lastTwoReversed([1], Y).
No

reverseLastTwo([1,2,3,4], Y).
Y=[1,2,4,3]

reverseLastTwo([1,2], Y).
Y=[2,1]

reverseLastTwo([1], Y).
No

Note that xsb Prolog contains several list functions which you are NOT to use for this assignment. If you need a list function, then should simply write it yourself.

This assignment can be done relatively easily by reversing the list and then operating on it, and if necessary reversing the results. Please note that this method of solution will not earn full marks for the assignment. Of course, you can try this method as you are figuring out how to do the assignment, but if you want full credit for what you do, you should not use reverse in your solution.

Please be certain to use these exact names. I will not grade your program if your names are not exactly correct. Also, make sure that you do not mix up the names.

Please make sure that you test your program completely.

You may use any prolog system to write your program, but you should test it on xsb on rucs which is where I will run it for grading. Several free versions on prolog are available for download, including xsb and jlog.

As ever, your program should follow good style, including appropriate white space, meaningful variable names, and as well as a header comment with your name, the name of the assignment, etc.. (You can have comments describing how a predicate works if you want; however, you can also assume your program is being read by somebody who understands the fundamentals of Prolog.)

The following extra credit is available, due the last day of class. Note that you must pledge that you did not find/study a solution to this problem on-line (searching in a graph).

homeinfolecturesexamsarchive


©2008, Ian Barland, Radford University
Last modified 2008.Nov.26 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme