How to resolve the algorithm Hash from two arrays step by step in the Prolog programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Hash from two arrays step by step in the Prolog programming language

Table of Contents

Problem Statement

Using two Arrays of equal length, create a Hash object where the elements from one array (the keys) are linked to the elements of the other (the values)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Hash from two arrays step by step in the Prolog programming language

Source code in the prolog programming language

% this one with side effect hash table creation

:-dynamic hash/2.

make_hash([],[]).
make_hash([H|Q],[H1|Q1]):-
	assert(hash(H,H1)),
	make_hash(Q,Q1).

:-make_hash([un,deux,trois],[[a,b,c],[d,e,f],[g,h,i]])


% this one without side effects 

make_hash_pure([],[],[]).
make_hash_pure([H|Q],[H1|Q1],[hash(H,H1)|R]):-
	make_hash_pure(Q,Q1,R).

:-make_hash_pure([un,deux,trois],[[a,b,c],[d,e,f],[g,h,i]],L),findall(M,(member(M,L),assert(M)),L).


  

You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Prime decomposition step by step in the PL/I programming language
You may also check:How to resolve the algorithm Character codes step by step in the Zoea programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Find limit of recursion step by step in the MUMPS programming language