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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:50:02+00:00 2026-06-12T14:50:02+00:00

In OCaml how would I write a median function that takes 5 arguments and

  • 0

In OCaml how would I write a median function that takes 5 arguments and returns the median. For example med5 2 5 7 4 3 would return 4.

I managed to write a med3 function (returns the median of 3 arguments) using if and else statements but this would be ridiculously complex if I attempted the same technique for 5 arguments 🙁

let med3 a b c =
  if ((b<=a && c>=a) || (c<=a && b>=a)) then a 
  else if ((a<=b && c>=b) || (c<=b && a>=b)) then b else c;;

For the med5 function, I would like to be able to use the min and max functions (built in to OCaml) to discard the highest and lowest values from the set of 5 arguments. Then I could use the med3 function that I have already written to return the median of the remaining 3 arguments, but how do I discard the minimum and maximum arguments!?!?!?!?

Any help would be much appreciated 🙂

  • 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-12T14:50:04+00:00Added an answer on June 12, 2026 at 2:50 pm

    If you can use Array, then just put your 5 entries in an array, sort it, and return a[2]. If it’s also forbidden in your assignment, you can use a poor-man’s bubble sort to select the max, then the min:

    let med5 a b c d e =
      (* move the max towards 'e' *)
      let a,b = if a<=b then a,b else b,a in
      let b,c = if b<=c then b,c else c,b in
      ...
      (* then move the min towards 'd', don't forget to reverse the comparisons *)
      ...
      in med3 a b c
    
    • 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 an OCaml function which takes a URL and returns
I would like to write an OCaml module having a compile function which will
I have experience with OCaml. You had to write a stub for every function
For debugging purposes I'd like to have a function in OCaml that converts to
I would like to improve the lambda-code generated for the assert OCaml 3.12.1 construct.
In Ocaml, is there easier ways to write a graphics-based toy programs like deminer
The OCaml GC imposes a global lock that prevents mutators (threads) from running in
What OCaml libraries are out there that provide lazy list handling? I am looking
I am new to OCaml, and I am now trying to implement a function
I'm OCaml newbie and I'm trying to write a simple OCaml-like grammar, and I

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.