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 6881415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:06:16+00:00 2026-05-27T05:06:16+00:00

I’m new to Prolog and my assignment requires us to implement a function as

  • 0

I’m new to Prolog and my assignment requires us to implement a function as described below:

Write a Prolog predicate zip(L1,L2,L3) that is true if the list L3 is obtained by zipping (i.e. shuffling”, or interleaving”) the elements of the lists L1 and L2. Update: the lists L1 and L2 can have different lengths. For instance, when you are done, you should get the following behavior:

?- zip([1,2],[a,b],[1,a,2,b]).
true.
?- zip([1,2],[a,b],X).
X = [1, 2, a, b] ;
X = [1, 2, a, b] ;
X = [1, a, 2, b] ;
X = [1, a, b, 2] ;
X = [a, 1, 2, b] ;
X = [a, 1, b, 2] ;
X = [a, b, 1, 2] ;
X = [a, b, 1, 2] ;
false.
?- zip([1,2],[a,b],[1,2,a,b]).
true.
?- zip(X,[a,b],[1,a,2,b]).
X = [1,2]
true.
?- zip([1,2],X,[1,a,2,b]).
X = [a,b]
true.

I’m thinking of creating a list which contains elements from L1 and L2 and then compare the list with L3. But I’m not familiar with the syntax and loops in Prolog.

  • 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-27T05:06:17+00:00Added an answer on May 27, 2026 at 5:06 am

    Actually, I have three answers to you. You probably want the third. But go through the others anyway.

    1 Different problem

    I am not that sure that you want the relation you describe. You are
    also learning OCaml and Python at the same time and in those languages zip means
    something else. It means something like:

    zip([], [], []).
    zip([], [_|_], []).
    zip([_|_], [], []).
    zip([X|Xs], [Y|Ys], [X-Y|XYs]) :-
       zip(Xs, Ys, XYs).
    
    ?- zip([1,2],[3,4],XYs).
       XYs = [1-3,2-4].
    

    Note the different convention in Prolog. While OCaml, Python but also Haskell use (X,Y) to denote a tuple, the frequent convention in Prolog is to use (X-Y). The minus sign here does not mean subtraction. It is just an uninterpreted term.

    The common way to implement this in Prolog is to use maplist. maplist requires that all lists are of same length.

    2 Interlacing

    (Edit) Another interpretation is the following. A name like interlace/3 or shuffle/3
    would be ideal here. Recently @salva has showed us a very beautiful solution to
    this. Don’t forget to +1 it! Let me only show some cool ways how you
    can use it:

    ?- shuffle([1,2],[3,4],Zs).
       Zs = [1,3,2,4].
    

    This you already know. But why do we need to give both lists [1,2]
    and [3,4] to Prolog? This isn’t a simple programming language which
    forces you to tell everything. If you are too lazy to type in a complex list or another term, just put a variable and see how Prolog figures it out. So, let’s replace the second list by a
    variable.

    ?- shuffle([1,2],Ys,Zs).
       Ys = [], Zs = [1,2]
    ;  Ys = [_A], Zs = [1,_A,2]
    ;  Ys = [_A,_B|_C], Zs = [1,_A,2,_B|_C].
    

    In this manner we ask: How do Ys and Zs have to look like such that shuffle/3 is true? In fact, there are 3 answers for Ys:

    • [] being the empty list. Zs is then [1,2]. So this is one solution.

    • [_A] being a list with exactly one element. Zs is the [1,_A,2]. The _A is a free variable. The point is that this variable occurs both within Ys and within Zs. This answer says: All terms that fit into that variable are solutions. So we have here infinitely many solutions expressed within a single answer.

    • [_A,_B|_C] meaning a list with at least two elements.

    Here is an even cooler query:

    ?- shuffle(Xs,Xs,Zs).
       Xs = [], Zs = []
    ;  Xs = [_A], Zs = [_A,_A]
    ;  Xs = [_A,_B], Zs = [_A,_A,_B,_B]
    ;  Xs = [_A,_B,_C], Zs = [_A,_A,_B,_B,_C,_C]
    ;  Xs = [_A,_B,_C,_D], Zs = [_A,_A,_B,_B,_C,_C,_D,_D]
    ;  ... .
    

    Look how there are now duplicates of the same variable in Zs!

    3 Intertwining

    Maybe this is what you actually want:

    intertwine([], [], []).
    intertwine([E|Es], Fs, [E|Gs]) :-
       intertwine(Es, Fs, Gs).
    intertwine(Es, [F|Fs], [F|Gs]) :-
       intertwine(Es, Fs, Gs).
    

    In the following query we ask about the lists that can be intertwined to give a three element list as result!

    ?- length(Zs,3), intertwine(Xs,Ys,Zs).
       Zs = [_A,_B,_C], Xs = [_A,_B,_C], Ys = []
    ;  Zs = [_A,_B,_C], Xs = [_A,_B], Ys = [_C]
    ;  Zs = [_A,_B,_C], Xs = [_A,_C], Ys = [_B]
    ;  Zs = [_A,_B,_C], Xs = [_A], Ys = [_B,_C]
    ;  Zs = [_A,_B,_C], Xs = [_B,_C], Ys = [_A]
    ;  Zs = [_A,_B,_C], Xs = [_B], Ys = [_A,_C]
    ;  Zs = [_A,_B,_C], Xs = [_C], Ys = [_A,_B]
    ;  Zs = [_A,_B,_C], Xs = [], Ys = [_A,_B,_C]
    ;  false.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
Does anyone know how can I replace this 2 symbol below from the string

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.