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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:28:42+00:00 2026-05-26T17:28:42+00:00

How do I retrieve the head and tail of a tuple in F#? For

  • 0
  1. How do I retrieve the head and tail of a tuple in F#?

    For example Conj (a, b), the head is Conj, tail is (a, b).

  2. I want to recursively run buildtree function on each parameters, put the head as Node‘s element, where is the map in F#?

    let rec getparams = map List.head (List.tail getparams);
    
    type Elem = Prop
    type Tree = E | T of Elem * Tree * Tree
    
    let rec buildtree vars = function
        | E = head vars
        | T = buildtree (getparams vars)
    

After updated:

open System
open Microsoft.FSharp.Reflection
// Learn more about F# at http://fsharp.net
//type Prop = {a: string; b: string}
//let Prop a b = (a, b) 
type Op = Prop
type tree = E | T of Op * tree * tree
let tree x y z = (x, y, z)

type binOp = Conj | Disj | Impl 
type expr =   
| Prop of string   
| BinOp of binOp * expr * expr 
| Conj of expr * expr
| Disj of expr * expr
| Impl of expr * expr

type Prop = {a: string}
let Prop a = (a)

//type Conj = {a : Prop; b : Prop} 
let Conj a b = (a, b)

//type Conj_Int = {a : Prop; b : Prop} 
let Conj_Int a b = Conj a b
//type Conj_Elmin1 = {a : Conj}
let Conj_Elmin1 a = fst a
//type Conj_Elmin2 = {a : Conj}
let Conj_Elmin2 a = snd a

//type Impl = {a : Prop; b : Prop} 
let Impl a b = (a b)
//type Impl_Int = {assume : Prop; b : Prop} 
let Impl_Int assume b = Impl assume b
//type Impl_Elmin = {a :string; b : Impl}
let Impl_Elmin a b = if a = fst b then snd b

type Neg = {a : Prop;}
let Neg a = (a)

//type Double_Neg_Int = {a : Prop;}
let Double_Neg_Int a = Neg(Neg(a))

//type Double_Neg_Elmin = {a : Prop}
let Double_Neg_Elmin a = fst(fst(a))

//type Disj = {a : Prop; b : Prop} 
let Disj a b = (a,b)

//type Disj_Int1 = {a : Prop; b : Prop} 
let Disj_Int1 a b = (a b)
//type Disj_Int2 = {a : Prop; b : Prop} 
let Disj_Int2 a b = (a b)

//type Disj_Elmin1 = {a : Disj}
let Disj_Elmin1 a = fst(a)
//type Disj_Elmin2 = {a : Disj}
let Disj_Elmin2 a = snd(a)

type TupleSplitter = static member splitTuple (a,b,c) = (a,(b,c)) 

let tupleToList t = if Microsoft.FSharp.Reflection.FSharpType.IsTuple(t.GetType()) then Some (Microsoft.FSharp.Reflection.FSharpValue.GetTupleFields t |> Array.toList) else None
    let operation x = List.head(List.ofSeq(FSharpValue.GetTupleFields(x)))
let parameters x = List.tail(List.ofSeq(FSharpValue.GetTupleFields(x)))


let rec map f = function | Prop _ as t -> f t | BinOp(op, a, b) -> f(BinOp(op, map f a, map f b))
(*
let rec map f = function   
| Prop _ as t -> f t   | Conj(a, b) -> f(Conj(map f a, map f b))   
| Disj(a, b) -> f(Disj(map f a, map f b))   
| Impl(a, b) -> f(Impl(map f a, map f b)) 
*)
let buildtree vars expr = map (function Prop v -> Map.find v vars | expr -> expr) expr 

let t = buildtree(Conj("a","b"))
  1. how to have two type of expression Op*Tree*Tree and Op*Tree?
  • 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-26T17:28:43+00:00Added an answer on May 26, 2026 at 5:28 pm

    A tuple is defined as (exp1,exp2, … ,expn) for example (1,”2″,’3′).
    I can’t see this pattern in your code.

    If you use (exp1 exp2) it means function application (apply exp2 as first argument to function exp1).
    The error you see on your code is because you defined Conj as a function accepting a function as first paramenter and you passed a string (“a”) instead of a function.

    If your question is how to split a tuple in head and tail you can go for the dynamic approach Tomas just explained, it will work for any n-tuple but you’ll lose type information.

    Otherwise the strong type solution is simply based on pattern matching:

    let splitTuple (a,b,c) = (a,(b,c))
    // Usage
    let (head,tail) = splitTuple (1,"2",'3')
    

    And if you want to make it work for n-tuples you’ll have to define one overload for each n:

    type TupleSplitter =
      static member splitTuple (a,b,c) = (a,(b,c))
      static member splitTuple (a,b,c,d) = (a,(b,c,d))
      static member splitTuple (a,b,c,d,e) = (a,(b,c,d,e))
    // ... more overloads, as much as you need
    
    // Usage
    let (head,tail) = TupleSplitter.splitTuple (1,"2",'3',4.0)
    // val tail : string * char * float = ("2", '3', 4.0)
    // val head : int = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I retrieve a list of ObjectId and I want to retrieve all object in
I want to retrieve the last 3 registered users in cakephp using the field
I want to retrieve all the nodes present in particular DIV element.see the below
I want to retrieve some tags from my database, they are in the form:
I want to retrieve data from restful webservice that returns xml. I'm using phonegap.
I want to retrieve TITLE attribute with the help of java's HTMLEditorKit ? this
I've put together the form below that allows a user to retrieve database records
I want to retrieve information from my Stack Overflow profile as JSON using the
i just want to retrieve the login of the selected User in the SelectOneMenu
How to retrieve link location from following script using preg_match <html><head><title>Object moved</title></head><body> <h2>Object moved

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.