Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 495155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:33:59+00:00 2026-05-13T05:33:59+00:00

I’m trying to understand lists in prolog when I stumbpled over this problem: there’s

  • 0

I’m trying to understand lists in prolog when I stumbpled over this problem:

there’s a predicate mergeandmap/2 that should basically do this:

mergeandmap([[a1,...,an],...,[z1,...,zm]],[x1...xn])
            %----------list 1------------ -list 2--

List 2 consits of letters (for example [a,b,c]).
List 1 consits of several lists with size(list2) elements containing 1s and 0s
(for example: [[0,0,1],[1,0,1],[0,1,1]])

The prolog program should determine from list 1 which elements from list 2 should be printed and when.

For the above example:

([[0,0,1],[1,0,1],[0,1,1]],[a,b,c])  Result:  b c abc

Another example:

([[0,1,1],[1,0,1],[1,1,1]],[a,b,c])  Result:  bc ac abc
([[0,1],[1,0]],[a,b])  Result:  b a
([[0,1,1,1],[1,0,1,0],[1,1,1,0],[1,1,1,0]],[a,b,c,d])  Result:  bcd acd abcd a

I came up with this idea:

([[A,B,C],[D,E,F],[G,H,I]],[a,b,c])
  1. “Merge” Lists into new list: by putting all the first subelements together, all the second subelements, all third, etc, etc… -> [ADG,BEH,CFI]
  2. “Map” second list on Result from (1):

    [ADG,BEH,CFI] + [abc,abc,abc]

-> Value of uppercase letter decides wether lower case letter gets in the result.

Does anybody know how to implement this in prolog?
Any help would really be appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:33:59+00:00Added an answer on May 13, 2026 at 5:33 am

    Well this is a an implementation of what you say, done in prolog exactly to your specifications. Your “Merge” is actually a function called transpose (in the context of matrices), and this happens to be in one of the SWI-Prolog libraries!

    1 ?- [user].
    |: mergeandmap(Bss,Ls) :- transpose(Bss,BssT), write('Result: '), maptostrings(BssT,Ls), write('\n').
    |: maptostrings([],_).
    |: maptostrings([Bs|Bss],Ls) :- write(' '), zipstringbits(Bs,Ls), maptostrings(Bss,Ls).
    |: zipstringbits([],_).
    |: zipstringbits([0|Bs],[_|Ls]) :- zipstringbits(Bs,Ls).
    |: zipstringbits([1|Bs],[L|Ls]) :- write(L), zipstringbits(Bs,Ls).
    |: :- use_module(library(clpfd)).
    %    library(error) compiled into error 0.01 sec, 10,056 bytes
    %   library(apply) compiled into apply 0.02 sec, 16,256 bytes
    %   library(lists) compiled into lists 0.00 sec, 13,404 bytes
    %   library(pairs) compiled into pairs 0.01 sec, 4,772 bytes
    %  library(clpfd) compiled into clpfd 0.32 sec, 367,328 bytes
    |: 
    % user://1 compiled 0.44 sec, 369,348 bytes
    true.
    
    2 ?- mergeandmap([[0,0,1],[1,0,1],[0,1,1]],[a,b,c]).
    Result:  b c abc
    true .
    
    3 ?- mergeandmap([[0,1,1],[1,0,1],[1,1,1]],[a,b,c]).
    Result:  bc ac abc
    true .
    
    4 ?- mergeandmap([[0,1],[1,0]],[a,b]).
    Result:  b a
    true .
    
    5 ?- mergeandmap([[0,1,1,1],[1,0,1,0],[1,1,1,0],[1,1,1,0]],[a,b,c,d]).
    Result:  bcd acd abcd a
    true .
    
    6 ?- mergeandmap([[0,1],[1,0],[1,0]],[a,b,c]).
    Result:  bc a
    true .
    
    7 ?- transpose([[A,B,C],[D,E,F],[G,H,I]],X).
    X = [[A, D, G], [B, E, H], [C, F, I]].
    

    Writing your own transpose function is a little more involved, as I decided to have a stab at it myself. After a while I did manage to get to a very neat solution, which by design, is able to accept lists of lists even when they’re not rectangular! (Though it’s still not as flexible as would be desired for use in mergeandmap unfortunately). Here’s the code:

    1 ?- [user].
    |: transpose(Rs,Cs) :- emptylists(Rs),emptylists(Cs).
    |: transpose([[X|R]|Rs],[[X|C]|Cs]) :- headsandtails(Rs,C,NRs), headsandtails(Cs,R,NCs), transpose(NRs,NCs).
    |: headsandtails(Xss,[],[]) :- emptylists(Xss).
    |: headsandtails([[X|Xs]|Xss],[X|Hs],[Xs|Ts]) :- headsandtails(Xss,Hs,Ts).
    |: emptylists([]) :- !.
    |: emptylists([[]|L]) :- emptylists(L).
    |: 
    % user://1 compiled 0.07 sec, 1,208 bytes
    true.
    
    2 ?- transpose([[1,2,3,4],[5,6,7],[8,9],[10]],Result).
    Result = [[1, 5, 8, 10], [2, 6, 9], [3, 7], [4]] ;
    false.
    
    3 ?- transpose([[1,2,3,4],[5,6,7],[8,9,10,11],[10]],Result).
    false.
    
    4 ?- transpose([[1,2,3,4,5,7,8,3],[5,6,7],[8,9,10,11],[10]],Result).
    false.
    
    5 ?- transpose([[1,2,3,4,5,7,8,3],[5,6,7],[8,9,99],[10]],Result).
    Result = [[1, 5, 8, 10], [2, 6, 9], [3, 7, 99], [4], [5], [7], [8], [3]] ;
    false.
    

    Basically how this works, is it checks the top left elements of the transpose and original are the same, and checks that the top row of one matches the left column of the other, and vice versa, then checks everything else iteratively until there’s nothing left to check.

    The cut operator ! on the emptylists predicate stops the answers growing off to the right with more empty lists, and means that it can terminate on trying to transpose something which doesn’t have one (where it just keeps adding them and always fails to find an answer).

    In summary for an all in one solution this is all the code you need:

    mergeandmap(Bss,Ls) :- transpose(Bss,BssT), write('Result: '), maptostrings(BssT,Ls), write('\n').
    maptostrings([],_).
    maptostrings([Bs|Bss],Ls) :- write(' '), zipstringbits(Bs,Ls), maptostrings(Bss,Ls).
    zipstringbits([],_).
    zipstringbits([0|Bs],[_|Ls]) :- zipstringbits(Bs,Ls).
    zipstringbits([1|Bs],[L|Ls]) :- write(L), zipstringbits(Bs,Ls).
    transpose(Rs,Cs) :- emptylists(Rs),emptylists(Cs).
    transpose([[X|R]|Rs],[[X|C]|Cs]) :- headsandtails(Rs,C,NRs), headsandtails(Cs,R,NCs), transpose(NRs,NCs).
    headsandtails(Xss,[],[]) :- emptylists(Xss).
    headsandtails([[X|Xs]|Xss],[X|Hs],[Xs|Ts]) :- headsandtails(Xss,Hs,Ts).
    emptylists([]) :- !.
    emptylists([[]|L]) :- emptylists(L).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.