Question

PROLOG: Write a prolog predicate that counts occurrences of 'x', 'y', or 'z' in a list....

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 Y is the count.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Prolog : predicate that count occurnce of 'x','y' or 'z' in a list.
?- count_occurrences([X], Occ).
Occ = [X] where { X= 'x','y','z'}

Bagof: this is a built-in predicate.

Syntax: bagof(true,member(X,List),Xs)

?- bagof(true,member(X,[x,y,a,b,c,2]),Xs).
X = x,
Xs = [true, true, true] ;
X = y,
Xs = [true, true, true] ;
X = a,
Xs = [false] ;
X = b,
Xs = [false] ;
X = c,
Xs = [false] ;

X = 2,

Xs = [false] ;

there are two methods to check occurrence in the list.

1.occurnce method - check occurrence in the list and return the value

2. bagof method - return true if the element contains in the list otherwise return false.

if X={'x','y' or 'z'} in the list then return true otherwise return false.

Add a comment
Know the answer?
Add Answer to:
PROLOG: Write a prolog predicate that counts occurrences of 'x', 'y', or 'z' in a list....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Prolog, define the isUnion predicate so that isUnion(X,Y,Z) says that the union of X and...

    In Prolog, define the isUnion predicate so that isUnion(X,Y,Z) says that the union of X and Y is Z. Do not use the predefined list predicates. Your predicate may choose a fixed order for Z. If you query isUnion([1,2],[3],Z) it should find a binding for Z, but it need not succeed on both isUnion([1],[2],[1,2]) and isUnion([1],[2],[2,1]). Your predicate need not work well when X or Y are unbound variables.

  • The predicate minim(IntList,Min) is true if Min is the minimum of the integers in a given...

    The predicate minim(IntList,Min) is true if Min is the minimum of the integers in a given non-empty list IntList. For example, a query ?- minim([29,1,8,167], X). Returns the answer X = 1, a query ?- minim([12,123,456,12,78,999,123,12],X), returns the answer X = 12, but a query ?- minim([99,2,17,155],17), must return the answer no. Write a recursive program that implements this predicate using any arithmetical operators, but you cannot use any auxiliary predicates. (Must use PROLOG)

  • Write the function deep_contains. It takes as input a number and a list. That list might...

    Write the function deep_contains. It takes as input a number and a list. That list might contain lists as elements, and those lists might contain lists, etc. The deep_contains' function should return a Boolean indicating whether the number is contained inputted list, or a sublist of it, or a sublist of that, and so forth. For example: deep_contains (3, (1,3,5]) returns True deep_contains(3, 11, [3,5]]) returns True deep_contains (3, 1, [[3,4],5),6]) returns True deep_contains (2, (1,3,5]) returns False This function...

  • skeleton: import java.util.*; public class Point3D{ private int x,y,z; Point3D(int a, int b, int c){ x = a; y = b; z = c; } public int x(){ return x; } public int y(){ return y; } public in...

    skeleton: import java.util.*; public class Point3D{ private int x,y,z; Point3D(int a, int b, int c){ x = a; y = b; z = c; } public int x(){ return x; } public int y(){ return y; } public int z(){ return z; } public String toString(){ return "("+x+","+y+","+z+")"; } } Question1Test.java import java.util.*; public class Question1Test { public static void main(String[] args){ MyHashList<Point3D> lst = new MyHashList<Point3D>(1000); } } a) A class Point3D is given. Re-write this class so that...

  • In Lisp 2. Code the function (replaceIn list possibleList repValue) which constructs a new list. It...

    In Lisp 2. Code the function (replaceIn list possibleList repValue) which constructs a new list. It examines list for occurrences of any of the atoms from the possibleList. Those are replaced with repValue. This only examines the top-level items in list. Example: > (replaceIn '(P A T T E R) '(T R) 'S) (P A S S E S) 3.   Code the function (insertAfter list atm insValue) which constructs a new list by inserting the specified insValue into the list...

  • Write a program that can: 1. Insert twenty random numbers into a linked list. The numbers...

    Write a program that can: 1. Insert twenty random numbers into a linked list. The numbers should be within a range (E.g., 1 to 7). The user should be prompted to enter the minimum number and maximum number of the range. Each number should be inserted at the end of the list. Section 7.8 of the textbook covers the random number generator. Examples of how to use the random number generator are in Fig 7.6 and 7.7. Here is a...

  • Character Count Write a program that reads a file and counts the number of occurrences of...

    Character Count Write a program that reads a file and counts the number of occurrences of each character in the file (case sensitive). The file name is "char.txt". You should use a dictionary to hold the number of occurrences of each character, and print out that dictionary. For example, if the file contains only six characters, "tactic", your dictionary should contain the following key-value pairs (order can be different): {'a': 1, 'c': 2, 'i': 1, 't': 2} Sample Input: Follow...

  • Write a function decompressed(count_tuples) that takes a list of counts of '0's and '1's as defined...

    Write a function decompressed(count_tuples) that takes a list of counts of '0's and '1's as defined above and returns the expanded (un-compressed) string. You may assume that the first value of count_tuples has a count that is greater than or equal to zero and all other counts are greater than zero. Comments to show how to figure this out would be greatly appreciated A data file in which particular characters often occur multiple times in a row can be compressed...

  • Suppose three random variables X, Y, Z have a joint distribution PX,Y,Z(x,y,z)=PX(x)PZ∣X(z∣x)PY∣Z(y∣z). Then X and Y...

    Suppose three random variables X, Y, Z have a joint distribution PX,Y,Z(x,y,z)=PX(x)PZ∣X(z∣x)PY∣Z(y∣z). Then X and Y are independent given Z? True or False Suppose random variables X and Y are independent given Z , then the joint distribution must be of the form PX,Y,Z(x,y,z)=h(x,z)g(y,z), where h,g are some functions? True or false

  • PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the...

    PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the result of integer division (i.e., 8 / 3 = 2). Please comment on every line of code. EXISITNG CODE: int Exponentiation(int X, int Y) { if (Y == 0) // base case return 1; else // recursive case return X * Exponentiation(X, Y - 1); //make recursive call } int Multiply(int X, int Y){ if(Y == 0){ return 0; } else{ return X +...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT