Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Solve the question please, solve the lab assessment PREDICATES nondeterm badcount1(ulong) nondeterm badcount2(ulong) nondeterm badcount3(ulong) nondeterm negative(ulong) CLAUSES badcount1(Number):- nl,write(Number), Next= Number+1, badcount1(Next), nl. badcount2(Number):-
Solve the question please, solve the lab assessment
PREDICATES nondeterm badcount1(ulong) nondeterm badcount2(ulong) nondeterm badcount3(ulong) nondeterm negative(ulong) CLAUSES badcount1(Number):- nl,write(Number), Next= Number+1, badcount1(Next), nl. badcount2(Number):- nl, write(Number),
Next= Number+1, badcount2(Next). badcount2 (Number):- Number =0, write("The Number is Non Negative"),nl. negative(X):- X
. factoris Versins of the Repetition and recursion: Repetition can be expressed in PROLOG in procedures and data structures. Two kinds of repetition exist in PROLOG : Backtracking : search for multiple solutions Recursion : a procedure calls itself When a sub goal fails, PROLOG returns to the most Normal recursion: recent subgoal that has an Tail recursion: fast and less memory untried alternative. Takes a lot of Using the fail predicate as the memory and time Compiled into end of repeated subgoals we iterative loops in enforce PROLOG to repeat M/C language executing different alternatives of some subgoals. Recursive procedure: To find the factorial of a PREDICATES number N : FACT(N) factorial (unsigned, real) CLAUSES IF N=1 then FCT(N)=1 factorial (1,1):-!. Else factorial (N, FactN):- FACT(N)=N* FACT(N-1) M=N-1, Using PROLOG, the factorial (M, FactM), factorial is implemented FactN=N*FactM. with two Goal as shown factorial (5,F). here. . F=120 Solution 1 2 File Edit Project Options Window Help %99 OORSG GEF 33:1 Insert Indent Modified PREDICATES factorial (unsigned, real) [Inactive C:\VIP\BIN\WIN\32\Obj\goal$000.exe) CLAUSES F=120 factorial (1,1):-!. 1 Solution factorial (N, FactN) :- M=N-1, factorial (M, FactM), FactN=N*FactM. GOAL factorial (5,F). Recursive rule: Highway map modeling and recursive rule a 4 b 2 5 6 5 3 g To represent the shown map we use the predicate link ( node, node, distance) To find a path from s to d get it from mentioned facts : path (S,D, TDist):-link (S, D, TDist). Or find a node x which has a link from Sto X and a path from X to D as follows: Total distance path (S,D, TDist):- link (S,X,DS1),path (X,D,DS2), TDist=DS1+DS2. d link(a,b,4). link (a.c,2). link (b,g,5). link (c.g,6). link (c,d,5). link (d.g,3). 3 DOMAINS The complete path distance finder program node=symbol distance= integer PREDICATES nondeterm link (node, node, distance) nondeterm path ( node, node, distance) CLAUSES link(a,b,4). link (a,c,2). link (b,g,5). Facts that model the link (C ,9,6). link (c,d,5). Recursive link (d,g,3). rule path (S,D, TDist):-link (S, TDist). path (S,D, TDist):- link (S, X, TD1 ),path (X,D, TD2), TDist=TD1+TD2. GOAL TotalDistance=9 path (a, g, TotalDistance). output TotalDistance=8 TotalDistance=10 3 Solutions ad map File Edit Project Options Window Help 99 QORG GEF 16:1 Insert Indent DOMAINS node=symbol distance-integer PREDICATES nondeterm link (node, node, distance) nondetern path (node, node, distance) Inactive C:\VIP\BIN\WIN32\Obj goal000.exe) TotalDistance 9 TotalDistance=8 TotalDistance=10 3 Solutions CLAUSES link (a, b, 4). link (a,c,2). link (b,9,5). link (c,9,6). link(c,d, 5). link(d, 9,3). path (S, D, TDist) :- link (S, D, TDist). path (5.0, TDist) :- link (5.x,101), path (X,D,TD2), TDist=TD1+TD2. GOAL path (a, g, TotalDistance). 4 Note: Requirements for tail recursion include the following: The call is the very last sub goal of the clause. There are no backtrack points earlier in the clause. No untried alternates in an earlier predicate call. Occurs when a procedure calls itself as the last step. Occurs in PROLOG ,when 1. The call is the very last subgoal in a clause. 2. No backtracking points exist before this call. 3. The recursive predicate does not return a value. 4. Only one version of this predicate exists. Example: count (N):- (2) No alternative solutions exist write(N), ni, here no backtracking points NewN=N+1, (3)N is bound to a constant value and no other free variable no return value *count (NewN). GOAL (1) Last call no need to save state of the previous count(0). call stack is free no extra memory is needed small memory and fast due to no need to push state You can use a cut(!) before the Write numbers from 0 to infinity ( will get unexpected values last call to be due to overflow) sure of (2) Procedure: PREDICATES count (ulong) CLAUSES count(Number):- nl, write(Number), Next = Number + 1, Count(Next). GOAL 5 Count(0) File Edit Project Options Window Help OORG BEF 11:1 Insert Indent Modified PREDICATES count (ulong) CLAUSES count (Number):- ni, write (Number), Next = Number + 1, Count (Next). C:\P\BIN\WIN\32\Obj\goal$000.exe 59204 59205 59206 59207 59208 59209 59210 59211 59212 59213 59214 59215 59216 59217 GOAL Count(0). How not to do tail recursion: PREDICATES nondeterm badcountl(ulong) nondeterm badcount2(ulong) nondeterm badcount3(ulong) nondeterm negative(ulong) CLAUSES badcount1 (Number):- nl.write(Number), Next= Number+1, badcount1(Next), nl. badcount2(Number):- nl, write(Number), 6 Next= Number+1, badcount2(Next). badcount2 (Number):- Number=0, write("The Number is Non Negative"), nl. negative(X):- X=0, write("The Number is Non Negative"), nl. negative (X):- X=0, write("The Number is Non Negative"), nl. negative(X):- X=0, write("The Number is Non Negative"), nl. negative (X):- XStep 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