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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:01:01+00:00 2026-05-30T21:01:01+00:00

I must come up with a function that evaluates an abstract syntax tree and

  • 0

I must come up with a function that evaluates an abstract syntax tree and returns the result of its evaluation.

evaluate will have type Exp -> int option

evaluate (Prod(Num 5, Diff(Num 6, Num 1)));;
val it : int option = Some 25

@John Palmer
How do i go about doing this? Can I use pattern matching? if so how would I do it in order to identify the operation that needs to be done.

Here is what i have come up with? I still dont understand the code per say.

let rec evaluate = function
 | Num n -> Some n
 | Neg e -> match evaluate e with
 |Sum (a,b) -> evaluate(a) + evaluate(b)
 |Diff(a,b) -> evaluate(a) - evaluate(b)
 | Prod (a,b) -> evaluate(a) * evaluate (b) 
 |Quot (a,b) -> evaluate(a) / evaluate(b) 
  • 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-05-30T21:01:02+00:00Added an answer on May 30, 2026 at 9:01 pm

    Here are some hints to get started

    let rec evaluate AST =
       match AST with
       |Prod(a,b) -> evaluate(a) * evaluate(b)
       |Num(a) -> a
       ....
    

    EDIT

    So I assume your data type looks like

    type AST =
    |Num of int
    |Neg of AST
    |Prod of AST * AST
    |Diff of AST * AST
    |Quot of AST * AST
    |Sum of AST * AST
    

    so you set up your AST (presumably you are parsing this from somewhere – which is a whole other question) with something like

    let ast = Prod(Num 5, Diff(Num 6, Num 1))
    

    then you can define evaluate as

    let rec evaluate arg =
        match arg with
        |Num n -> n
        |Neg tree -> - (evaluate tree)
        |Sum (a,b) -> (evaluate a) + (evaluate b)
        |Prod(a,b) -> (evaluate a) * (evaluate b)
        |Quot(a,b) -> (evaluate a) * (evaluate b)
        |Diff(a,b) -> (evaluate a) - (evaluate b)
    

    then call it and print the result with

    printfn "%i" (evaluate ast)
    

    Note that evaluate has to have type AST -> int as otherwise you will need to handle option cases – for example what is 2 * None or 2 + None

    I am not sure what you mean by

    how should i go about calling the recursive method on smaller inputs.

    but hopefully this explains everything

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

Sidebar

Related Questions

I have come across what must be a common problem. When I have an
Somebody must have come up with a solution for this by now. We are
I have come across a website that appears to use Ajax but does not
I'm trying to come up with a function that can generate random passwords, that
I have a function that I want to use to manipulate an input parameter,
I assume that public or private static targets must have reduced memory usage, due
I have come across a very odd problem in C that I have never
I have a fairly complicated function that takes several double values that represent two
I have been looking for an elegant way to write a function that takes
I must be overlooking something simple. I'm setting a variable from a query result

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.