Your task is to develop all necessary LISP functions to evaluate predicate logic
ID: 3592487 • Letter: Y
Question
Your task is to develop all necessary LISP functions to evaluate predicate logic expressions. The facts will be: leader(caesar), leader(lincoln), leader(jfk), assassinate(brutus, caesar) , assassinate(booth, lincoln), assassinate(oswald, jfk). Use the first 2 rules and don’t worry about voting and the recent events. So rule1 is: leader(X) ^ assassinate(Y,X) => notloyal(Y,X) and rule2 is: notloyal(Y, X) ^ leader(X) => hate(Y,X). To solve this you need to define a reasonable representation for facts and rules. I might suggest an arrangement of lists for each. The list for the facts needs the predicate and the instances for each predicate. For the rules I might suggest the use of a list and embedded lists being the combinations of LHS and RHS with predicates and variables to match. Your functions should be able to answer questions about facts and prove if the implication is satisfied. I might call the question answer an “ask” function and the proof of an implementation as a “prove” function. For example:
(ask leader caesar) = T
(ask leader oswald) = F
(ask assassinate brutus caesar ) = T
(ask assassinate booth jfk) = F
(prove hate brutus caesar ) = T
(prove hate booth jfk) = F
Explanation / Answer
# These are the letters a-z
from string import ascii_lowercase as letters
# Assign values to each letter as given in the assingment
# stored as a dictionary with a: -13 and so on.
v = dict(zip(letters, range(-13, 13)))
# To store tuples of the calculated values
calculations = []
# "words" is a word list with one English word per line
with open('words', 'r') as words:
for word in words:
# Each line has a ' ' newline at the end, so strip it
word = word.strip()
# Ignore words that have capital letters or other characters
if not set(word).issubset(set(letters)):
continue
# Enumerate the assigned values for letters in the word
values = list((v.get(l, 0) for l in word))
# String together a calculation (like +5-2+3...)
calculation = ''.join(('%+d' % v for v in values))
# Add a tuple of the word, the "show your work", and the total
calculations.append((word, calculation, sum(values)))
# Sort the calculations by word length
calculations = sorted(calculations, key=lambda a: len(a[0]))
print('Find a word that has a value of 18:')
# Let's grab the longest five and the shortest 10
eighteens = list((c for c in calculations if c[2] == 18))
for answer in eighteens[:10] + eighteens[-10:]:
print('%s: %s=%d' % answer)
print('Find a word that has a value of -16:')
# Let's grab the longest five and the shortest 10
minussixteens = list((c for c in calculations if c[2] == -16))
for answer in minussixteens[:10] + minussixteens[-10:]:
print('%s: %s=%d' % answer)
# The next two questions are about values of 5
fives = list((c for c in calculations if len(c[0]) == 5))
# Sort from highest value to lowest
fives = sorted(fives, key=lambda a: - a[2])
print('Find the highest value 5 letter word you can:')
for answer in fives[:10]:
print('%s: %s=%d' % answer)
print('Find the lowest value 5 letter word you can:')
for answer in fives[-10:]:
print('%s: %s=%d' % answer)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.