aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2020-11-29 18:54:16 -0500
committerAdam Russell <ac.russell@live.com>2020-11-29 18:54:16 -0500
commitbc2bfd2c14e8588ffea19f359e53df55feed1069 (patch)
tree87b377c6b16fa9ba4afe0888a78195772310b7cf
parent0fb1e10f0b233caf4b8b46fd732f9d1d30b79fec (diff)
downloadperlweeklychallenge-club-bc2bfd2c14e8588ffea19f359e53df55feed1069.tar.gz
perlweeklychallenge-club-bc2bfd2c14e8588ffea19f359e53df55feed1069.tar.bz2
perlweeklychallenge-club-bc2bfd2c14e8588ffea19f359e53df55feed1069.zip
Prolog solution for Part 1 and Prolog blog.
-rw-r--r--challenge-088/adam-russell/blog1.txt1
-rw-r--r--challenge-088/adam-russell/prolog/ch-1.p28
2 files changed, 29 insertions, 0 deletions
diff --git a/challenge-088/adam-russell/blog1.txt b/challenge-088/adam-russell/blog1.txt
new file mode 100644
index 0000000000..982e0783c5
--- /dev/null
+++ b/challenge-088/adam-russell/blog1.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2020/11/29
diff --git a/challenge-088/adam-russell/prolog/ch-1.p b/challenge-088/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..ae22b0415c
--- /dev/null
+++ b/challenge-088/adam-russell/prolog/ch-1.p
@@ -0,0 +1,28 @@
+/*
+ You are given an array of positive integers @N.
+ Write a script to return an array @M where $M[i] is
+ the product of all elements of @N except the index $N[i].
+*/
+
+list_product([], 1).
+list_product([L|Ls], P) :-
+ foldl(product_, Ls, L, P).
+
+product_(A, B, P) :- P is A*B.
+
+list_products(List, Products):-
+ length(List, L0),
+ L is L0 - 1,
+ list_products(List, L, [], Products).
+list_products(_, -1, Products, Products).
+list_products(List, Index, ProductsAccum, Products):-
+ nth0(Index, List, _, Remainder),
+ list_product(Remainder, Product),
+ NewIndex is Index - 1,
+ list_products(List, NewIndex, [Product|ProductsAccum], Products).
+
+main:-
+ list_products([5, 2, 1, 4, 3], Products),
+ write(Products),
+ halt.
+ \ No newline at end of file