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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:33:52+00:00 2026-05-26T14:33:52+00:00

Trying to get used to F# I tried small examples and my next step

  • 0

Trying to get used to F# I tried small examples and my next step is to write a few functions for logical computations/evaluations and for now I have this structure

type Expr =
    | True
    | False
    | Not of Expr
    | And of Expr * Expr

and it should be obvious what I want to achieve: to be able to encapsulate different functions like Not and And to be used in one computation like And(And(True, Not(False)), True) but currently I do not understand (this is my first time wrinting such “complex” functional code) how to use this struct and write the proper methods as for example i can write for evaluation

let evalLogic (expr : Expr) : bool =
    match expr with
    | True  -> true
    | False -> false
    | _     -> false (* eval the expression which is not "atomic" *)

and then as a method

let And (left : Expr , right : Expr ) : Expr =
    let value = evalLogic(left) && evalLogic(right)
    match value with
    | true  -> True
    | false -> False

however I know that this is crap and not the correct way of implementing this! Can you give me a hint how to achieve the desired behavior and describe how to extend this?

  • 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-26T14:33:52+00:00Added an answer on May 26, 2026 at 2:33 pm

    When writing functional code to represent and evaluate expressions, the typical pattern is to define the type of expression as a discriminated union (just like you did!) and then write a function that takes the expression and returns the result. Your logical expressions will evaluate to boolean, so you need a function:

    evalLogic : Expr -> bool
    

    In the function, you need to write a case for every possible expression. Your sample handles False and True and the code by Bluepixy shows the rest. The general idea is that the function can recursively evaluate all sub-expressions – if you have And(e1, e2), then you can evaluate e1 and e2 to two boolean values and then combine them (using &&).

    Your And function is not needed, because the whole evaluation is all done in the evalLogic function. This is a typical way of doing things in the functional style (because the expression type is not expected to change frequently). Howeever, you could also support more binary operations using:

    type Expr = 
      | Constant of bool
      | Unary of Expr * (bool -> bool)
      | Binary of Expr * Expr * (bool -> bool -> bool)
    

    Here, the idea is that an expression is either a constant or an application of some binary or unary logical operator. For example, to represent true && not false, you could write:

    Binary(Constant(true), Unary(Constant(false), not), (&&))
    

    Now the expression tree also carries the function that should be used, so the evaluation just needs to call the function (and you can use all standard logical operators easily). For example, the case for Binary looks like this:

    let rec evalLogic e =
      // pattern matching
      |  Binary(e1, e2, op) -> op (evalLogic e1) (evalLogic e2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to get the number of available comports. Used the following code: HKEY hKey;
well iam trying to get version of IE used by user by using $.browser.version
We are trying to get the Model-View-Presenter pattern used on (virtually) all new dev
I am trying to get the likes from a company pages (coca-cola) I used
I am trying to get used to writing object oriented, but often I get
I am trying to get dynamic parameters to be used in the email publisher's
I'm trying to get a value from an array used in the Joomla Plugin
I'm trying to get ASP.NET MVC 2 single-project area registration to work. Tried with
I tried for about 3-4 hours trying to get the mysql gem to install
As a small project to get myself used to some more iPhone elements I've

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.