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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:43:24+00:00 2026-06-14T14:43:24+00:00

Define the function max2 that takes two integers as arguments and returns the largest

  • 0

Define the function max2 that takes two integers as arguments and returns the largest of them.

I did this: let max2 x y = if x < y then y else x this I belive is correct

Then define the function max_list that returns the largest of the elements in a nonempty list of integers by calling max2. For the empty list, it should abort with an error message ( raising an exception)

I did this: let list = [3;4] let max_list = if list.IsEmpty then 0 else max2 list.Item(0) list.Item(1) but this wont work if the list is more then two elements. I dont want to use any object-orientated stuff. What is the correct answer?

  • 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-14T14:43:26+00:00Added an answer on June 14, 2026 at 2:43 pm

    A simple recursive solution:

    let max2 x y = if x < y then y else x
    
    let max_list list =
      let rec loop hi list = 
         match list with 
         | h::t -> let hi = max2 h hi
                   loop hi t
         | []   -> hi
      match list with
      | h::t -> loop h t
      | []   -> invalidArg "list" "Empty list"
    

    Test in FSI:

    > max_list [3;4;5;1;2;9;0];;
    val it : int = 9
    

    For each element in the list, compare it to the previous highest (‘hi’). Pass the new highest and the rest of the list into the loop function, until the input list is empty. Then just return ‘hi’.

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

Sidebar

Related Questions

I made a user-define function for business hours calculation. This is my UDF. CREATE
When I define this function, template<class A> set<A> test(const set<A>& input) { return input;
Is this how you define a function in jQuery? $(document).ready( function () { var
So I have this function: (define (try try-block catch-block finally-block) ; Implements try/catch/finally like
In Python I can define a function as follows: def func(kw1=None,kw2=None,**kwargs): ... In this
I want to declare a function that takes in a function(the function also takes
How can I define a function that will accept my type and return its
Whenever I define a function say funct(n), then can I assign value of n
I have a dojo define function like this one define([ // some param ]),function(foo,
I am reading this tutorial on Haskell . They define function composition as the

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.