%========================================================================== % Problem #1 %========================================================================== % These facts associate basic names and values single(one,1). single(two,2). single(three,3). single(four,4). single(five,5). single(six,6). single(seven,7). single(eight,8). single(nine,9). teens(ten,10). teens(eleven,11). teens(twelve,12). teens(thirteen,13). teens(fourteen,14). teens(fifteen,15). teens(sixteen,16). teens(seventeen,17). teens(eighteen,18). teens(nineteen,19). tens(twenty,20). tens(thirty,30). tens(forty,40). tens(fifty,50). tens(sixty,60). tens(seventy,70). tens(eighty,80). tens(ninety,90). % This rule makes use of previous facts to avoid having to explicitly % define all of the "hundreds". hundreds([Name,hundred],Val) :- single(Name,V), Val is V * 100. %========================================================================== % This predicate defines how to name numbers less than 100. There are % three special cases and a general rule: The number is a single digit, a % teen, an even multiple of 10, or it's composed of some combination of % a ten and a single. (e.g. 5, 13, 20, and 42 respectively) %========================================================================== upto99([Name],Val) :- single(Name,Val). upto99([Name],Val) :- teens(Name,Val). upto99([Name],Val) :- tens(Name,Val). upto99([Name1,Name2],Val) :- tens(Name1,V1), single(Name2,V2), Val is V1+V2. %========================================================================== % This predicate defines how to name numbers less than one thousand: % Either the number is less than 100, it's an even multiple of 100, or % it is some number of hundreds "and" a number less than 100. (i.e. 53, % 200, and 978 respectively) %========================================================================== numnames(Name,Val) :- upto99(Name,Val). numnames(Name,Val) :- hundreds(Name,Val). numnames([Name1,Name2,and|Names],Val) :- hundreds([Name1,Name2],V1), upto99(Names,V2), Val is V1+V2. %========================================================================== % Problem #2 %========================================================================== edge(a,b). edge(b,c). edge(b,d). edge(c,e). edge(d,e). edge(e,f). edge(g,h). edge(f,a). % This new edge makes the graph cyclic % X and Y are connected with path [X,Y] if there's an edge % connecting them. path_connects(X,Y,[X,Y]) :- edge(X,Y). % X and Y are connected with path [X | Path] if we can reach % a node N from X, and we can get from N to Y with path Path. path_connects(X,Y,[X|Path]) :- edge(X,N), path_connects(N,Y,Path). %========================================================================== % Problem #3 %========================================================================== /* edge(a,b). edge(a,c). edge(c,d). edge(c,e). con(X,Y) :- edge(X,Y). con(X,Y) :- con(X,Z), con(Z,Y). con(a,d) / \ / \ / \ edge(a,d) con(a,Z) con(Z,d) / \ / \ / \ / \ edge(a,Z) con(a,Z') con(Z,d) con(Z',Z) / \ con(Z,d) / \ / \ / \ / \ con(b,d) con(c,d) edge(a,Z') con(a,Z'') / \ / \ con(Z',Z) con(Z'',Z') / \ / \ con(Z,d) con(Z',Z) / \ / \ / \ con(Z,d) / \ / \ / \ / \ edge(b,d) con(b,Z) edge(c,d) con(c,Z) con(b,Z) con(c,Z') edge(a,Z'') con(a,Z''') | con(Z,d) | con(Z,d) con(Z,d) con(Z',Z) con(Z'',Z') con(Z''',Z'') #f ... #t ... ... con(Z,d) con(Z',Z) con(Z'',Z') ... con(Z,d) con(Z',Z) ... con(Z,d) ... */