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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:44:00+00:00 2026-05-25T00:44:00+00:00

I’m currently learning OCaml, and it’s giving me some problems. I’m trying to implement

  • 0

I’m currently learning OCaml, and it’s giving me some problems. I’m trying to implement a merge_sort function, but it keeps giving me an error on the 5th line of the given code. I’m just utterly confused as to why its giving me the error, if anyone could enlighten me, I’d greatly appreciate it. I’m not even really sure if I’m setting up the pattern matching (match statements) correctly, so if you could take look, that’d be really helpful.

let rec merge_sorted (l1:int list) (l2:int list) : int list =
let end_list = [] in
begin match l1, l2 with 
    | [], [] -> end_list
    | h1::t1, h2::t2 -> if h1 < h2 then merge_sorted t1 t2 (h1 :: end_list) else merge_sorted t1 t2 h2::end_list, h1::end_list
end

The error I’m getting is:

Error: This function is applied to too many arguments,
maybe you forgot a `;’
merge_sorted: int list -> int list -> int list

On the part where it says “if h1 < h2 then merge_sorted t1 t2…”

I was also wondering if there are any places to learn OCaml syntax? I’ve been trying to use Jason Hickey’s book, but there are things that it doesn’t go very in depth in (such as this multiple/parallel pattern matching). I’ve only mainly coded in Java, so coding in OCaml has been a bit of a frustrating new experience for me.

  • 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-25T00:44:00+00:00Added an answer on May 25, 2026 at 12:44 am

    You’ve declared merge_sorted as a function of type int list -> int list -> int list, or a function that takes two int lists as parameters and returns another int list.

    The problem you’re encountering is because you’re invoking merge_sorted with three int list arguments:

    h1::t1, h2::t2 -> if h1 < h2 then merge_sorted t1 t2 (h1 :: end_list) else merge_sorted t1 t2 h2::end_list, h1::end_list
    

    specifically, merge_sorted t1 t2 (h1 :: end_list). You’ve made no provision for the result list in your function definition.

    You’ll want to modify your function definition, maybe like so:

    let rec merge_sorted l1 l2 results =
      (* code code code *)
    

    Regarding your second question, a decent set of tutorials can be found here.

    EDIT

    In response to your comment-

    First, the :: operator serves two purposes. One, it creates a new list with a new head and an existing tail – 1 :: [2] yields [1; 2]. Second, it decomposes existing lists during pattern matches – match [1; 2] with x :: xs will bind x to 1 and y to [2].

    That point being addressed, there are a few things in your approach that work fine in Java but will not work at all in OCaml. First, you’re re-declaring end_list as an empty list in each recursive invocation of your function.

    Second, because you have your list concatenation on the same line as your recursive call to merge_sorted, the compiler will (has no choice but to) think that you’re specifying a third function call. What you probably meant is:

    h1::t1, h2::t2 -> if h1 < h2 then
                          merge_sorted t1 t2
                          h1 :: end_list
                      else
                          merge_sorted t1 t2
                          h2 :: end_list
                          h1 :: end_list
    

    BUT! This isn’t your solution, read on.

    Third and far more importantly, OCaml don’t work this way. By default, OCaml lists (and all variables, actually) are immutable – that is, once you’ve bound a value to one, you can’t change it. When you say (h1 :: end_list), you’re not changing end-list; what you’re actually doing is creating a new list that has h1 as its head and end_list as its tail. Because you have this all on the same line, the compiler thinks that you’re doing the equivalent of merge_sorted(t1, t2, new List(h1, end_list)) in Java-land where you’ve defined merge_sorted(List t1, List t2).

    Because you can’t modify your list in-place, functional languages of this stripe have different idioms for list processing. In this case, most functional programmers would have defined their function to take an additional accumulator argument. For example:

    let rec map f data acc =
        match data with
        | []      -> List.rev acc
        | x :: xs -> map f xs ((f x) :: acc);;
    
    map (fun x -> x + 1) [1; 2; 3;] [];;  (* yields [2; 3; 4] *)
    

    This is the canonical map function, which takes a list and a function, applies each item in the list to that function, and returns a new list containing the results. As you can see, the results of applying each item in data to f are stored in acc and the result is passed to the next call, only to be returned when data is empty. In this way we can achieve a series of list modifications without needing to mutate a variable.

    If you don’t like the extra argument in your function signature, you can hide it inside of a nested function like so:

    let map f data =
        let rec loop d acc =
            match d with
            | []      -> List.rev acc
            | x :: xs -> loop xs ((f x) :: acc)
        in
            loop data []
    

    I hope this makes some sense to you – transitioning from imperative to functional idioms can be truly mind-bending, and immutability can seem a cruel and arbitrary limitation to those new to it – I promise that the benefits and the beauty of functional programming will manifest themselves to you if you stick with it!

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into

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.