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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:09:23+00:00 2026-05-15T16:09:23+00:00

I am new to F# and I recently discovered the function composition operator >>

  • 0

I am new to F# and I recently discovered the function composition operator >>

I understand the basic principle so that something like this is possible….

let Add1ToNum x = x +1
let Mul2ToNum y = y * 2
let FuncComp = Add1ToNum >> Mul2ToNum

However, how would one handle composition when you have several functions that have a varying number of input parameters… for instance I would like to be able to do the following…

let AddNums (x,y) = x+y
let MulNums (x,y) = x*y
let FuncComp = Add1 >> Mul2

Which obviously doesn’t work because AddNums is returning an int, and MulNums is expecting a tuple.

Is there some form of syntax that allows me to accomplish this, or if I am wanting to use Function Composition do I have to always perform some sort of intermediary function to transform the values?

Anyone suggestions on this would be much appreciated.

  • 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-15T16:09:24+00:00Added an answer on May 15, 2026 at 4:09 pm

    As Yin and codekaizen pointed out, you cannot compose the two functions to create a function that passes the input to the first one and then passes the output of this call to the second function (that is, using th >> operator). Using a diagram, you cannot do:

         +---------+    +---------+
     --->| AddNums |--->| MulNums |--->
         +---------+    +---------+
    

    One option is to change the function and specify one of the parameters, so that the functions can be composed. The example by codekaizen uses this and could be also written like this (if you used currying instead of tupled parameters):

    let AddNums x y = x + y  
    let MulNums x y = x * y  
    let FuncComp = (AddNums 1) >> (MulNums 2)
    

    Another option for composing the functions is to create a function that takes several inputs, passes two numbers to the first function and then calls the second function with the result and another number from the original inputs. Using a diagram:

     -----------------\
     --->+---------+   \+---------+
     --->| AddNums |--->| MulNums |--->
         +---------+    +---------+
    

    If you need something like that, then the best option is to write that directly, because this probably won’t be a frequently repeated pattern. Directly, this is easy (using the curried variant):

    let AddNums x y = x + y  
    let MulNums x y = x * y  
    let FuncComp x y z = AddNums z y |> (MulNums z)
    

    If you wanted to write something like that more generally (or just for curiosity), you could write something like this (using the tupled version of functions this time). The &&& operator is inspired by Arrows:

    let AddNums (x,y) = x + y 
    let MulNums (x,y) = x * y  
    
    let (&&&) f g (a, b) = (f a, g b)
    let FuncComp = (AddNums &&& id) >> MulNums
    
    // First two numbers are added, result is multiplied by the third one
    FuncComp ((9, 12), 2) // Gives '42'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.