Question
WRITE IN PROLOG, PLS DO NOT COPY FROM CHAT GPT THE TESTS FAIL please make sure passes all test cases. Thanks! Define a relation my_add
WRITE IN PROLOG, PLS DO NOT COPY FROM CHAT GPT THE TESTS FAIL
please make sure passes all test cases. Thanks!
Define a relation my_add that takes two lists of single digit integers, N1 and N2, which represent large magnitude positive integer numbers called big_nums, and binds the third parameter to a list in this big_num representation corresponding to adding the two big_nums N1 and N2. Each element of a big_num is in the range 0 to 9. big_nums are stored in reverse order so that the first element is the ones digit, the second element is the tens, the third is the hundreds, etc. Be sure to handle carry, and ensure no integer element exceeds 9 in value. Valid big_nums will never be nil from the start. Zero is represented by the list [0], 10 is [0,1], 1999 is [9,9,9,1].
my_add([0],[0],R). R=[0]
my_add([1],[1],R). R=[2]
my_add([9],[9],R). R=[8,1]
my_add([1,1,1,1,1,1,1,1,1,1],[9,9,9,9,9,9,9,9,9,9],R). R=[0,1,1,1,1,1,1,1,1,1,1]
my_add([1],[9,9,9,9,9,9,9,9,9,9],R). R=[0,0,0,0,0,0,0,0,0,0,1]
Define the relation my_assoc that takes an atom A and an association list ALIST and binds R to the value associated with A in ALIST. If there is no association for A, it fails. Prolog does not have association lists, but we can invent thema little different than they are in Lisp. An association list is a list of key/value pairs where the key [always a symbol] is followed by the value, so the length of an ALIST is always even. An association list is of the form [key_1,value_1,key_2,value_2], key_n,value_n] Examples:
my_assoc(a,[],R). false
my_assoc(a,[a,b,c,e,f,b],R). R=b
my_assoc(c,[a,b,c,e,f,b],R). R=e
my_assoc(f,[a,b,c,e,f,b],R). R=b
my_assoc(b,[a,b,c,e,f,b],R). false (PLEASE MAKE SURE THIS TEST IS PASSED, THIS IS THE ONE THAT IS CAUSING ISSUES )
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started