From dc73df33b04d436b3ab44363eca3c263f893ff64 Mon Sep 17 00:00:00 2001 From: Brieuc Dubois Date: Sat, 2 Mar 2024 00:16:47 +0100 Subject: [PATCH] First answers --- solution.pl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/solution.pl b/solution.pl index 8bfba24..b2f1a11 100644 --- a/solution.pl +++ b/solution.pl @@ -3,13 +3,13 @@ Prints the names of all existing tables, one per line (use writeln/1). A table name is always an atom. */ -tables. +tables :- tabl(X, _), writeln(X). :- dynamic tables/1. /* Unify Tables with a list of the names of all existing tables. */ -tables(Tables). +tables(Tables) :- findall(X, tabl(X, _), Tables). :- dynamic create_table/2. /* @@ -20,7 +20,9 @@ If a table with the given name already exists, the predicate must throw a descriptive exception (use throw/1). All exceptions must have a descriptive error message. */ -create_table(Table, Cols). +create_table(Table, Cols) :- + (tabl(Table, _) -> throw("Table already exists")); + assertz(tabl(Table, Cols)). :- dynamic cols/2. /* @@ -186,4 +188,4 @@ query(Query, Result). /* cf. query/2 */ -query(Query). \ No newline at end of file +query(Query).