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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:31:56+00:00 2026-06-18T06:31:56+00:00

I started learning OCaml and I have a problem with a simple function. This

  • 0

I started learning OCaml and I have a problem with a simple function. This is from Developing Application with Objective OCaml

Write a general function merge which takes as argument a comparison function
and two lists sorted in this order and returns the list merged in the same order.
The comparison function will be of type ‘a -> ‘a -> bool.

Here is what I got so far

(* func : 'a -> 'a -> bool) *)
let rec merge2 listA listB func = match listA, listB with
    | list, [] | [], list -> list
    | (headA :: tailA), (headB :: tailB) -> 
        if (func headA headB) then headA :: merge2 tailA listB
        else merge2 :: merge listA tailB

and here is a error message

Characters 169-187:
        if (func headA headB) then headA :: merge2 tailA listB
                                        ^^^^^^^^^^^^^^^^^^
Error: This expression has type ('a -> 'a -> bool) -> 'a list
       but an expression was expected of type 'a list

I don’t understand wht I get this error message. Obviously a dummy function

let foo a b f = if (f a b ) then true else false

work just fine. I also tried to state types explicitly.

EDIT:

Correct code

let rec merge2 listA listB func = match listA, listB with
    | list, [] | [], list -> list
    | (headA :: tailA), (headB :: tailB) -> 
        if (func headA headB) then headA :: merge2 tailA listB func
        else headB :: merge2 listA tailB func
  • 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-18T06:31:57+00:00Added an answer on June 18, 2026 at 6:31 am

    Obviously, merge2 takes 3 arguments but you supplied only 2 arguments in the recursive calls. There are several typos in the else branch as well (merge2 should be headB while merge should become merge2).

    To avoid such mistakes, it is a good idea to move least-frequently-changed arguments upfront:

    let rec merge2 f xs ys = 
        match xs, ys with
        | zs, [] | [], zs -> zs
        | x::xs', y::ys' -> 
            if f x y then x::merge2 f xs' ys
            else y::merge2 f xs ys'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just started learning PySide and is having problem with QTimer I have this #!/usr/bin/python
I've just started learning ruby, so this question is simple. I created @subject in
Just started learning Haskell. I have an empty source file with this inside: pe
I started learning regular expression recently. I have a small problem statement now. I
Just started learning Jquery - here's my problem: I have six divs on a
I started learning Hibernate, so I decided to start with simple program. I have
Started learning algorithms. I understand how to find theta-notation from a 'regular recurrence' like
I have just started learning python version 3 and trying to create a file
I have only started learning python recently. I would still be considered a beginner.
Started learning Rails, so may this question is obsolete but yet I want to

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.