SWI-Prolog
a) Write a predicate called lastEle(Atom,List) to find the last
element of a list. You may not use the built-in last
predicate in your answer.
E.g.,
?- lastEle(X,[how,are,you,today]). X=today.
b) Write a predicate gradeMap(L,R), where L is a list of
percentage grade values (integers from 0 to 100), and R is a list
of corresponding grade letters (a-f, no +/-'s). You may assume the
grade list contains the correct types.
E.g.,
?-gradeMap([0, 16, 49, 55, 63, 78, 92], R). R=[f,f,f,d,c,b,a]
Answer a:
%The following is the prolog code for the same.
lastEle(Y,[Y]).
lastEle(X,[Head|R]):-lastEle(X,R).
Answer b:
gradeMap([],[]).
gradeMap(L,R):-maplist(grade,L,R),!.
grade(V,a) :- V>=80.
grade(V,b) :- V>=70,V<80.
grade(V,c) :- V>=60,V<70.
grade(V,d) :- V>=50,V<60.
grade(V,f) :- V<50.
Please give thumbsup, if you like it. Thanks.
SWI-Prolog a) Write a predicate called lastEle(Atom,List) to find the last element of a list. You...
In Prolog language, write a recursive predicate to find the last element of a list. You may not use the built-in last predicate in your answer. E.g., ?- lastEle(X,[how,are,you,today]). X=today.
# PrologWrite a predicate that returns the second to last element in a list. ?- second last clement(x, [a.b.c.d,e] x = d?- second last element (X, []) false. ?- second last element (3, [5,4,3,2,1])true.
PROLOG: Write a prolog predicate that counts occurrences of 'x', 'y', or 'z' in a list. For example, count([x,[y,[a,2],[a,3]],[a,4]], 2) should return true. I only want to count occurrences of x,y,z not a. the base case is that any [a,int] list will have a count of 0. So any sublist with 'a' does not count. another example of what will return true is: count([x,[a,1],[a,5]], 1) The predicate must have the form count(X,Y) where X is the list structure passed and...
In PROLOG, write a binary predicate fof, short for 'full of full', which will precede each atom in a list by the atom full. E.g. ?- fof([a,b,c]),L). L = [full,a,full,b,full,c] NOTE: only atoms, not numbers or lists or trees.
Question 3 (13] (a) Write a procedure filter (L,PredName, L1,L2) that accepts a list L and returns two lists Li and L2, where Li contains all elements in L for which PredName (x) fails, and L2 contains all elements in L for which PredName (x) succeeds. The predicate PredName/1 should be defined when calling the procedure filter. For example: let test be defined as test(N).- N > 0. 7- filter((-6,7,-1,0),test,L1,L2). L1 - (-6.-1) L2 - [7, 0] NB Use the...
///Program needs to write in prolog///
6. A binary tree is either empty or it is composed of a root element and two successors, which are binary trees themselves. In Prolog we represent the empty tree by the atom 'nil' and the non-empty tree by the term t(X,L,R), where X denotes the root node and L and R denote the left and right subtree, respectively. The following Prolog term represents the given binary tree below. T1 = t(a,t(b,t(d,nil,nil),t(e,nil,nil)),t(c,nil,t(f,t(g,nil, nil),nil))) d...
3. Write a function called sort3 that takes a 3-element vector as its sole arguments. It uses if-statements, possibly nested, to return the three elements of the vector as three scalar output-arguments in nondecreasino-roer , i.e., the first output argument equals the smallest element of the input vector and the last output argument equals the largest element. NOTE: Your function may NOT use any built-in functions, e.g., sort, min, max, median, etc. (5 points) (bonus question). Write a function called...
Write a java program to read a list of exam grades given as int's in the range of 0 to 100. Your program will display the total number of grades and the number of grades in each letter-grade category as follows: A 93 <= grade <= 100 A- 90 <= grade < 93 B+ 87 <= grade < 90 B 83 <= grade < 87 B- 80 <= grade < 83 C+ 77 <= grade < 80 C 73 <=...
JAVA:
(15 marks) Write a program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. "Out of Bounds") and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle...
Need help with a 2D list - I'm almost there! #Write a function called check_winner which takes #as input a 2D list. It should return "X" if there are four #adjacent "X" values anywhere in the list (row, column, #diagonal); "O" if there are four adjacent "O" values #anywhere in the list; and None if there are neither. # #Here are the ways Connect-4 is different from tic-tac-toe: # # - Connect-4 is played with 6 rows and 7 columns...