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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:47:28+00:00 2026-06-14T13:47:28+00:00

I would like to write this function in haskell it is a union function

  • 0

I would like to write this function in haskell

it is a union function with o(m+n) complexity

int printUnion(int arr1[], int arr2[], int m, int n)
{
  int i = 0, j = 0;
  while(i < m && j < n)
  {
    if(arr1[i] < arr2[j])
      printf(" %d ", arr1[i++]);
    else if(arr2[j] < arr1[i])
      printf(" %d ", arr2[j++]);
    else
    {
      printf(" %d ", arr2[j++]);
      i++;
    }
  }

  /* Print remaining elements of the larger array */
  while(i < m)
   printf(" %d ", arr1[i++]);
  while(j < n)
   printf(" %d ", arr2[j++]);
}
  • 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-06-14T13:47:30+00:00Added an answer on June 14, 2026 at 1:47 pm
    import Control.Monad (mapM_)
    

    The difference between programming languages is not just that different programming languages are different, but also that we use different programming languages differently. e.g. C and C++ are similar programming languages, but whereas C programs tend to allocate a few large blocks of memory, C++ programs tend to allocate more but smaller blocks of memory.

    So: whereas your C function takes two arrays (and has to be passed their lengths explicitly), in Haskell we would use lists instead. (That’s not to say that Haskell programs never use arrays, just as it’s not true that C programs never use lists, just that C programs tend to use arrays and Haskell programs tend to use lists.)

    showSpaced :: Show a => a -> String
    showSpaced x = " " ++ show x ++ " "
    

    Your C function calls printf(), which both formats the output and sends it to stdout, but we’re going to those things separately. showSpaced works with all values we can pass to show.

    union :: Ord a => [a] -> [a] -> [a]
    union xs          []          = xs
    union []          ys          = ys
    union xs0@(x:xs1) ys0@(y:ys1) = case x `compare` y of
        LT -> x : union xs1 ys0
        GT -> y : union xs0 ys1
        EQ -> y : union xs1 ys1
    

    This union function requires only that the underlying elements can be compared. The two lists have to have the same type as each other. You’ll note that we’re using recursion instead of looping, and that by using lists instead of arrays we don’t have to keep track of array indices.

    We use pattern matching, both by giving three equations for union itself, and with the case expression. Pattern matching is important in Haskell: find yourself a tutorial that explains it.

    printUnion :: (Ord a, Show a) => [a] -> [a] -> IO ()
    printUnion xs ys = mapM_ (putStr . showSpaced) (union xs ys)
    

    And so in printUnion we put it all together. The list elements need to implement Ord (so we can call union) and Show (so we can call showSpaced), so they both appear in the type signature.

    As you’re coming from C, you may be worried about the efficiency of building up an intermediate list. Don’t be: the optimiser will fuse everything into a single loop.

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

Sidebar

Related Questions

I would like to write this as a user defined function: private double Score(Story
I would like to know how to write this inside my hittest if statement:
I would like to write a function that creates a plot of a set
I would like to write a cross-platform function in C++ that contains system calls.
I have a rather complicated function the I would like to write as an
In clojure, I would like to write a defn-my macro that creates a function
I would like to make qqplots through a list of dataframes. I wrote this
I would like to write an abstract base class class func_double_double_t : public unary_function<double,
I would like to write an if statement that would continue to repeat a
I would like to write a query that returns a single date value, that

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.