summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2022-11-16 20:05:15 +0200
committerIgor Pashev <pashev.igor@gmail.com>2022-11-16 20:07:25 +0200
commit181758e5e739b4aedf9ea1bb256d9bde09d55caf (patch)
treea99ad674e29d246df6d7b57b28581b0828765d69
parentd39ce2868483a08ef96cecf0e1d8cd95b64042bf (diff)
downloadgcd-181758e5e739b4aedf9ea1bb256d9bde09d55caf.tar.gz
Update Prolog
So it does not crash on empty list.
-rw-r--r--gcd.pro10
1 files changed, 6 insertions, 4 deletions
diff --git a/gcd.pro b/gcd.pro
index 1d888e9..793d307 100644
--- a/gcd.pro
+++ b/gcd.pro
@@ -6,8 +6,6 @@
% # ./gcd-pro 22 33 44 121
-% 1st number, 2nd number, GCD
-
% It is true that GCD of A and 0 is A
% (The fact is GCD of A and 0 is A)
gcd2(A, 0, A).
@@ -20,14 +18,18 @@ gcdn(A, [], A).
gcdn(A, [B|Bs], G) :- gcd2(A, B, N), gcdn(N, Bs, G).
gcdn([A|As], G) :- gcdn(A, As, G).
-:- initialization(main).
-
str2int([], []).
str2int([S|St], [N|Nt]) :- number_atom(N, S), str2int(St, Nt).
+not_empty([]) :- halt.
+not_empty(_).
+
main :-
argument_list(Args),
+ not_empty(Args),
str2int(Args, Numbers),
gcdn(Numbers, G),
write(G), nl.
+:- initialization(main).
+