From bc2bfd2c14e8588ffea19f359e53df55feed1069 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Sun, 29 Nov 2020 18:54:16 -0500 Subject: Prolog solution for Part 1 and Prolog blog. --- challenge-088/adam-russell/blog1.txt | 1 + challenge-088/adam-russell/prolog/ch-1.p | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 challenge-088/adam-russell/blog1.txt create mode 100644 challenge-088/adam-russell/prolog/ch-1.p 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 -- cgit