diff --git a/solution.pl b/solution.pl index 8c2342f..04753ae 100644 --- a/solution.pl +++ b/solution.pl @@ -62,13 +62,22 @@ cols(Table, Cols) :- Unifies Row, one result at a time, with each row in the given Table. If the given table does not exist, the predicate should fail. */ +row(Table, _) :- + \+ tabl(Table, _), + throw("Table doesn't exist"), + !. + +row(Table, _) :- + tabl(Table, Cols), + length(Cols, L), + \+ current_predicate(Table/L), + !. + row(Table, Row) :- - tabl(Table, Cols) -> - ( - length(Cols, L), - length(Row, L), - apply(Table, Row) - ); throw("Table doesn't exist"). + tabl(Table, Cols), + length(Cols, L), + length(Row, L), + apply(Table, Row). :- dynamic rows/1. /* @@ -81,6 +90,12 @@ rows(Table) :- throw(table_does_not_exist(Table)), !. +rows(Table) :- + tabl(Table, Cols), + length(Cols, L), + \+ current_predicate(Table/L), + !. + rows(Table) :- row(Table, Row), writeln(Row), @@ -218,6 +233,17 @@ For example: findall(Values, selec(persons,[+id,+first],[],Values), Projections) returns: Projections = [[0, "Jeffrey"], [1, "Lorena"], [2, "Joseph"], ... */ +selec(Table, _, _, _) :- + \+ tabl(Table, _), + throw("Table doesn't exist"), + !. + +selec(Table, _, _, _) :- + tabl(Table, Cols), + length(Cols, L), + \+ current_predicate(Table/L), + !. + selec(Table, Selector, Cond, Projection) :- tabl(Table, Cols), row(Table, R), @@ -231,8 +257,6 @@ selec(Table, *, Cond, Projection) :- does_match(Cond, Table, R), Projection = Cols/R. -%base case -selector(_,_,[],ColumnNames, ColumnValue, ColumnNames, ColumnValue):-!. /** * Row: A row from the table * Cols: name of the column of the table @@ -242,6 +266,9 @@ selector(_,_,[],ColumnNames, ColumnValue, ColumnNames, ColumnValue):-!. * ColumnNames: List returning the column(s) name(s) * ColumnValue: List returning the row values for each column */ + +selector(_,_,[],ColumnNames, ColumnValue, ColumnNames, ColumnValue):-!. + selector(Row, Cols, [+Selector|Selectors], AccCols, AccVals, ColumnNames, ColumnValue):- (member(Selector, Cols) -> %find the value corresponding to the column in the row