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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:21:01+00:00 2026-06-17T23:21:01+00:00

I have function ‘my_a’ in OCaml, which could have a very complicated return type:

  • 0

I have function ‘my_a’ in OCaml, which could have a very complicated return type:

exception Backtrack
exception Continue of (* How do I put the type of function 'my_a' here? *)

let my_a arg = try do_stuff (List.hd arg) 
               with 
               | Backtrack -> my_a (List.tl arg)
               | Continue (found_answer) ->  (try my_a (List.tl arg)
                                              with 
                                     | Backtrack -> raise Continue(found_answer)
                                     | Continue (other_answer) -> 
                       raise Continue (compare_answer(found_answer,other_answer));;
(* the caller of my_a will handle the Continue exception to catch the found value
if something was found*)

This is my problem: I’m using backtrack to find a solution. When a backtrack exception is raised by do_stuff, there was no solution going that path. However, when it raises an exception of type Continue, it means it found a solution, but, it may not be the best solution there is, that’s when I try again with a different path. If there is another exception, I want to return the answer it already had found.

The thing is, to be able to use that feature of OCaml I need to to tell it what data type Continue will be carrying. What the OCaml top level returns when i define my_a:

   'a * ('a -> ('a, 'b) symbol list list) ->
  'b list -> ('a * ('a, 'b) symbol list) list * 'b list = <fun>

Does anyone have any idea of how to do that, or a different solution to that?

  • 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-17T23:21:02+00:00Added an answer on June 17, 2026 at 11:21 pm

    You are gaining nothing by using exceptions. Here is a possible solution.

    (** There are many ways to implement backtracking in Ocaml. We show here one
        possibility. We search for an optimal solution in a search space. The
        search space is given by an [initial] state and a function [search] which
        takes a state and returns either
    
        - a solution [x] together with a number [a] describing how good [x] is
          (larger [a] means better solution), or
    
        - a list of states that need still to be searched.
    
        An example of such a problem: given a number [n], express it as a sum
        [n1 + n2 + ... + nk = n] such that the product [n1 * n2 * ... * nk] is
        as large as possible. Additionally require that [n1 <= n2 <= ... <= nk].
        The state of the search can be expressed as pair [(lst, s, m)] where
        [lst] is the list of numbers in the sum, [s] is the sum of numbers in [lst],
        and [m] is the next number we will try to add to the list. If [s = n] then
        [lst] is a solution. Otherwise, if [s + m <= n] then we branch into two states:
    
        - either we add [m] to the list, so the next state is [(m :: lst, m+s, m)], or
        - we do not add [m] to the list, and the next state is [(lst, s, m+1)].
    
        The return type of [search] is described by the following datatype:
    *)
    
    type ('a, 'b, 'c) backtrack =
      | Solution of ('a * 'b)
      | Branches of 'c list
    
    (** The main function accepts an initial state and the search function. *)
    let backtrack initial search =
      (* Auxiliary function to compare two optional solutions, and return the better one. *)
      let cmp x y =
        match x, y with
          | None, None -> None (* no solution *)
          | None, Some _ -> y  (* any solution is better than none *)
          | Some _, None -> x  (* any solution is better than none *)
          | Some (_, a), Some (_, b) ->
            if a < b then y else x
      in
      (* Auxiliary function which actually performs the search, note that it is tail-recursive.
         The argument [best] is the best (optional) solution found so far, [branches] is the
         list of branch points that still needs to be processed. *)
      let rec backtrack best branches =
        match branches with
          | [] -> best (* no more branches, return the best solution found *)
          | b :: bs ->
            (match search b with
              | Solution x ->
                let best = cmp best (Some x) in
                  backtrack best bs
              | Branches lst ->
                backtrack best (lst @ bs))
      in
        (* initiate the search with no solution in the initial state *)
        match backtrack None [initial] with
          | None -> None (* nothing was found *)
          | Some (x, _) -> Some x (* the best solution found *)
    
    (** Here is the above example encoded. *)
    let sum n =
      let search (lst, s, m) =
        if s = n then
          (* solution found, compute the product of [lst] *)
          let p = List.fold_left ( * ) 1 lst in
            Solution (lst, p)
        else
          if s + m <= n then
            (* split into two states, one that adds [m] to the list and another
               that increases [m] *)
            Branches [(m::lst, m+s, m); (lst, s, m+1)]
          else
            (* [m] is too big, no way to proceed, return empty list of branches *)           
            Branches []
      in
        backtrack ([], 0, 1) search
    ;;
    
    (** How to write 10 as a sum of numbers so that their product is as large as possible? *)
    sum 10 ;; (* returns Some [3; 3; 2; 2] *)
    

    OCaml happily informs us that the type of backtrack is

    'a -> ('a -> ('b, 'c, 'a) backtrack) -> 'b option
    

    This makes sense:

    • the first argument is the initial state, which has some type 'a
    • the second argument is the search function, which takes a state of type 'a and
      returns either a Solution (x,a) where x has type 'b and a has type 'c,
      or Branches lst where lst has type 'a list.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have function some_func_1 which will create an object of type some_type and will
I have function which return NSArray, but it's generating memory leak, since i can't
I have function like : create or replace function dedup_temp return varchar2 as TYPE
i have function which return list as given below. List<User> lstUsers = SearchUsers(searchText); which
i have function public Menu Details(int? id) { return _dataContext.Menu.Include(ChildMenu).FirstOrDefault(m => m.MenuId == id);
Inside my Controller i have function that runs after user clicks on item, which
I have function which is removing and adding class into input tag, i want
I have function which gets a Seq[_] as an argument and returns an immutable
I have function which takes in an parameter of a class called Triple, and
I have function in Javascript which works fine using prototype. The function is used

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.