diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-11-30 00:19:47 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-30 00:19:47 +0000 |
| commit | 81d1cc269f80aab0f0d590e3f4afeff7d5256360 (patch) | |
| tree | 336db66268638e6827505673ee192e7e76c48f76 | |
| parent | 76619b2ef1933bc93b28e8dfcaa83c903871461d (diff) | |
| parent | 4227c63624b68c731b4fdcdf973e0baabee82342 (diff) | |
| download | perlweeklychallenge-club-81d1cc269f80aab0f0d590e3f4afeff7d5256360.tar.gz perlweeklychallenge-club-81d1cc269f80aab0f0d590e3f4afeff7d5256360.tar.bz2 perlweeklychallenge-club-81d1cc269f80aab0f0d590e3f4afeff7d5256360.zip | |
Merge pull request #2879 from adamcrussell/challenge-088
Prolog solution for Part 1 and Prolog blog.
| -rw-r--r-- | challenge-088/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-088/adam-russell/prolog/ch-1.p | 27 |
2 files changed, 28 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..01922cb3f1 --- /dev/null +++ b/challenge-088/adam-russell/prolog/ch-1.p @@ -0,0 +1,27 @@ +/* + 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]. +*/ +product_a_b(A, B, P):- + P is A*B. +list_product([], 1). +list_product([H|T], P) :- + foldl(product_a_b, T, H, P). + +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 |
