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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:56:50+00:00 2026-06-02T07:56:50+00:00

I have a help function in my Ocaml project that helps to append a

  • 0

I have a help function in my Ocaml project that helps to append a list to another without element duplicate.
For example, append list x: [d, e, f, g] to list y [a, b, c, d], result should be [a, b, c, d, e, f, g]

The function I wrote is like this:

    (* helper function checks if list contains element *)
let rec find e l =
    match l with
        [] -> false
        |(h::t) -> if (h = e) then true else find e t
;;

    (* helper function append l1 to l2 without duplicate *)
let rec help_append_list l1 l2 =
    match l1 with
        [] -> l2
        |(h::t) -> if (find h l2 = false) then (help_append_list t ([h]@l2)) else (help_append_list t l2)
;;

But this dosen’t look like working well when I use it, it turns out to be there’s still duplicate elements appear.

Please take a look at the above functions and give me some suggestion on how to correct them…

Thank you=)

  • 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-02T07:56:52+00:00Added an answer on June 2, 2026 at 7:56 am

    If you use Set, you only need union of two sets for the purpose.

    If l2 in help_append_list doesn’t have duplication, your function works fine.

    Suppose that x and y could have their own duplication, and the order doesn’t matter, you could use:

    let append_list x y = help_append_list x (help_append_list y [])
    

    I have some comments on your functions. First, find is the same as exists function in List module. You probably want to write it for learning purpose, so if (h = e) then true else ... should be replaced by ||:

    let rec find e = function
        | [] -> false
        | h::t -> h = e || find e t
    

    Second, [h]@l2 is an inefficient way to write h::l2:

    let rec help_append_list l1 l2 =
        match l1 with
        | [] -> l2
        | h::t -> if find h l2 then help_append_list t l2
                  else help_append_list t (h::l2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a ocaml project, in which I have a function that replace
I need help with my C code. I have a function that sets a
Please help me finding where I have made mistake? delimiter // CREATE FUNCTION `count_photos_in_gallery`(`tmp_gallery_id`
I need help with preg_match function in php. I have read a lot of
I need to convert the function path::combine(path1, path2). Please help me if you have
I have an HTML help system that I need to convert to SharePoint. The
I have problem with validation such code function show_help_tip(event, element) { var $e =
I have a simple function that will scroll down to a div in dojo.
Define a function called symcount that takes a symbol and a list and returns
I have to list several elements that are coming from a database source A

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.