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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:56:19+00:00 2026-05-10T18:56:19+00:00

I’m currently working on a small project with OCaml; a simple mathematical expression simplifier.

  • 0

I’m currently working on a small project with OCaml; a simple mathematical expression simplifier. I’m supposed to find certain patterns inside an expression, and simplify them so the number of parenthesis inside the expression decreases. So far I’ve been able to implement most rules except two, for which I’ve decided to create a recursive, pattern-matching ‘filter’ function. The two rules I need to implement are:

-Turn all expressions of the form a – (b + c) or similar into a – b – c

-Turn all expressions of the form a / (b * c) or similar into a / b / c

…which I suspect would be fairly simple, and once I’ve managed to implement one, I can implement the other easily. However, I’m having trouble with the recursive pattern-matching function. My type expression is this:

type expr =  | Var of string            (* variable *)  | Sum of expr * expr       (* sum  *)  | Diff of expr * expr      (* difference *)  | Prod of expr * expr      (* product *)  | Quot of expr * expr      (* quotient *) ;; 

And what I’m mainly having trouble on, is in the match expression. For example, I’m trying something like this:

let rec filter exp =        match exp with            | Var v -> Var v                             | Sum(e1, e2) -> Sum(e1, e2)               | Prod(e1, e2) -> Prod(e1, e2)     | Diff(e1, e2) ->         match e2 with         | Sum(e3, e4) -> filter (diffRule e2)         | Diff(e3, e4) -> filter (diffRule e2)               | _ -> filter e2              | Quot(e1, e2) ->                                 ***this line***         match e2 with           | Quot(e3, e4) -> filter (quotRule e2)                 | Prod(e3, e4) -> filter (quotRule e2)                 | _ -> filter e2 ;; 

However, it seems that the match expression on the marked line is being recognized as being part of the previous ‘inner match’ instead of the ‘principal match’, so all ‘Quot(…)’ expressions are never recognized. Is it even possible to have match expressions inside other match expressions like this? And what would be the correct way to end the inner match so I can continue matching the other possibilities?

Ignore the logic, since it’s pretty much what I came up with first, it’s just that I haven’t been able to try it since I have to deal with this ‘match’ error first, although any recommendation on how to handle the recursiveness or the logic would be welcome.

  • 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. 2026-05-10T18:56:19+00:00Added an answer on May 10, 2026 at 6:56 pm

    Quick Solution

    You just need to add parentheses, or begin/end, around the inner match:

    let rec filter exp =     match exp with     | Var v -> Var v     | Sum (e1, e2) -> Sum (e1, e2)     | Prod (e1, e2) -> Prod (e1, e2)     | Diff (e1, e2) ->             (match e2 with              | Sum (e3, e4) -> filter (diffRule e2)              | Diff (e3, e4) -> filter (diffRule e2)              | _ -> filter e2)     | Quot (e1, e2) ->             (match e2 with              | Quot (e3, e4) -> filter (quotRule e2)              | Prod (e3, e4) -> filter (quotRule e2)              | _ -> filter e2) ;; 

    Simplifications

    In your particular case there is no need for a nested match. You can just use bigger patterns. You can also eliminate the duplication in the nested rules using "|" ("or") patterns:

    let rec filter exp =     match exp with     | Var v -> Var v     | Sum (e1, e2) -> Sum (e1, e2)     | Prod (e1, e2) -> Prod (e1, e2)     | Diff (e1, (Sum (e3, e4) | Diff (e3, e4) as e2)) -> filter (diffRule e2)     | Diff (e1, e2) -> filter e2     | Quot (e1, (Quot (e3, e4) | Prod (e3, e4) as e2)) -> filter (quotRule e2)     | Quot (e1, e2) -> filter e2 ;; 

    You can make it even more readable by replacing unused pattern variables with _ (underscore). This also works for whole sub patterns such as the (e3,e4) tuple:

    let rec filter exp =     match exp with     | Var v -> Var v     | Sum (e1, e2) -> Sum (e1, e2)     | Prod (e1, e2) -> Prod (e1, e2)     | Diff (_, (Sum _ | Diff _ as e2)) -> filter (diffRule e2)     | Diff (_, e2) -> filter e2     | Quot (_, (Quot _ | Prod _ as e2)) -> filter (quotRule e2)     | Quot (_, e2) -> filter e2 ;; 

    In the same way, you can proceed simplifying. For example, the first three cases (Var, Sum, Prod) are returned unmodified, which you can express directly:

    let rec filter exp =     match exp with     | Var _ | Sum _ | Prod _ as e -> e     | Diff (_, (Sum _ | Diff _ as e2)) -> filter (diffRule e2)     | Diff (_, e2) -> filter e2     | Quot (_, (Quot _ | Prod _ as e2)) -> filter (quotRule e2)     | Quot (_, e2) -> filter e2 ;; 

    Finally, you can replace e2 by e and replace match with the function shortcut:

    let rec filter = function     | Var _ | Sum _ | Prod _ as e -> e     | Diff (_, (Sum _ | Diff _ as e)) -> filter (diffRule e)     | Diff (_, e) -> filter e     | Quot (_, (Quot _ | Prod _ as e)) -> filter (quotRule e)     | Quot (_, e) -> filter e ;; 

    OCaml’s pattern syntax is nice, isn’t it?

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

Sidebar

Ask A Question

Stats

  • Questions 172k
  • Answers 172k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer SELECT * FROM table WHERE dateField > DATE_SUB(NOW(), INTERVAL 1… May 12, 2026 at 2:34 pm
  • Editorial Team
    Editorial Team added an answer Actually pprint seems to sort the keys for you under… May 12, 2026 at 2:34 pm
  • Editorial Team
    Editorial Team added an answer Your problem may be that the execution of main thread… May 12, 2026 at 2:34 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.