Question
Write a Prolog source file take.pro as follows. Be sure to follow the Coding Standards. Your file should define the predicate take/3. Predicate take should
Write a Prolog source file take.pro as follows. Be sure to follow the Coding Standards.
Your file should define the predicate take/3.
Predicate take should take arguments as follows: take(+n, +x, ?e).
The idea is that n is a nonnegative integer, x is a list, and e is a list consisting of the first n items in x, or all of x, if x has fewer than nitems.
If n is a nonnegative integer, and x and e are both lists, then take succeeds if e is the correct list.
If n is a nonnegative integer, x is a list and e is an unbound variable, then take succeeeds, with e set to the proper list.
Your code does not need to do any type checking or other error checking.
Here is a sample Gprolog session using take.
[Interactive Prolog]
| ?- [take]. compiling /home/ggc/PROLOG/take.pro for byte code... /home/ggc/PROLOG/take.pro compiled, 14 lines read - 570 bytes written, 11 ms yes | ?- take(2,[0,1,2],[0,1]). yes | ?- take(6,[7,9,8,4],[7,9,8,4]). yes | ?- take(5,[0,1,2],[0,1]). no | ?- take(2,"abc","ab"). yes | ?- take(0,[1,2],[]). yes | ?- take(4,[100,3,200,4,600,5,900,1000000],E). E = [100,3,200,4] yes
Your file may define any other symbols you wish.
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