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

  • SEARCH
  • Home
  • 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 263625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:37:04+00:00 2026-05-11T22:37:04+00:00

From page 90 of Erlang Programming by Cesarini and Thomson, there is an example

  • 0

From page 90 of Erlang Programming by Cesarini and Thomson, there is an example that has no detailed discussion. I’m quite the newbie to functional programming and recursive thinking, so I’m not familiar in solving problems this way.

“For example, the following function merges two lists (of the same length) by interleaving
their values: “

merge(Xs,Ys) -> lists:reverse(mergeL(Xs,Ys,[])).

mergeL([X|Xs],Ys,Zs) ->  mergeR(Xs,Ys,[X|Zs]);
mergeL([],[],Zs) ->  Zs.

mergeR(Xs,[Y|Ys],Zs) ->  mergeL(Xs,Ys,[Y|Zs]);
mergeR([],[],Zs) ->  Zs.

How does this work? Thanks!

  • 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-11T22:37:04+00:00Added an answer on May 11, 2026 at 10:37 pm

    This function is called first:

    merge(Xs,Ys) -> lists:reverse(mergeL(Xs,Ys,[])).
    

    The empty list [] passed to mergeL is the accumulator – this is where the answer will come from. Note that the first function calls mergeL – the left merge.

    Let us pretend that this function is called as so:

    merge([1, 2, 3], [a, b, c])
    

    Two lists of the same length. This first function then calls mergeL:

    mergeL([X|Xs],Ys,Zs) ->  mergeR(Xs,Ys,[X|Zs]);
    mergeL([],[],Zs) ->  Zs.
    

    There are 2 clauses in left merge. The call to mergeL with arguments will match these clauses in top down order.

    The second of these clauses has three parameters – the first two of these are empty lists []. However the first time mergeL is called these two lists aren’t empty they are the lists Xs and Ys so the first clause matches.

    Lets break out the matches. This is the call to mergeL:

    mergeL([1, 2, 3], [a, b, c], [])

    and it matches the first clause in the following fashion:

    X = 1
    Xs = [2, 3]
    Ys = [a, b, c]
    Zs = []
    

    This is because of the special form of the list:

    [X | Xs]
    

    This means match X to the head of the list (an individual item) and make Xs the tail of the list (a list).

    We then build up the new function call. We can add the value X to the start of the list Zs the same way we pattern matched it out so we get the first mergeR call:

    mergeR([2, 3], [a, b, c], [1])

    The final argument is a one-item list caused by adding an item at the head of an empty list.

    This this zips through until the end.

    Actually the final clause of mergeL is redundant. By definition this function will exhaust in the final clause of mergeR (but I will leave that as an exercise for the reader).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

when i copied this code from page error found that there is syntax error
I'm trying to call ScriptManager.RegisterStartupScript from page A so that when page B loads
I have a class that inherits from Page, called APage. public abstract class APage:
I have several variables that I need to send from page to page... What
Using C#, I want to get a value from previous page. For example, In
In the Erlang crypto library, there is no aes_cfb_ivec function. Does it mean that
Every time there is a post from page the entire bunch of css gets
In Silverlight I noticed that the code-behind Page class inherits from UserControl: public partial
I have a select element that is in the DOM from page load. Lets
How to remove Default Enter Click from page. that is Have one save button

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.