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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:38:12+00:00 2026-05-29T10:38:12+00:00

So I have a function that I’ve pulled from a tutorial: let sumOfSquares nums

  • 0

So I have a function that I’ve pulled from a tutorial:

let sumOfSquares nums = 
    nums
    |> Seq.map sqr
    |> Seq.sum

I have decided that I want to test its performance. In a brute-force approach, I would do something like:

let timeFunction nums =
    let sw = new System.Diagnostics.Stopwatch()
    sw.Start()
    nums
        |> Seq.map sqr
        |> Seq.sum
    sw.Stop()
    sw.ElapsedMilliseconds

However, this struck me as a good example for how F# is supposed to treat functions as first-class citizens. So I tried to do something like the following, instead:

let timeFunction fn x =
    let sw = new System.Diagnostics.Stopwatch()
    sw.Start()
    fn x
    sw.Stop()
    sw.ElapsedMilliseconds

And then I tried to call it:

let sequenceOfNums = [for i in 1..20 -> i]
let print n = System.Console.WriteLine(n.ToString())
print "Time Trial"
print (timeFunction sumOfSquares sequenceOfNums)

However, this fails with the following error:

Type mismatch. Expecting a ‘a -> unit but given a ‘a ->
int The type ‘unit’ does not match the type ‘int’

on the line that calls the function. Do I have some sort of syntactical error here, or have I grossly misunderstood a concept?

  • 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-29T10:38:13+00:00Added an answer on May 29, 2026 at 10:38 am

    Because the result of fn isn’t being used it’s inferred to be unit. You can fix this by piping the result to the ignore function. It’s signature is 'a -> unit, so it makes the return type of fn generic (which is what you want).

    let timeFunction fn x =
        let sw = new System.Diagnostics.Stopwatch()
        sw.Start()
        fn x |> ignore
        sw.Stop()
        sw.ElapsedMilliseconds
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Java code I have function that gets file from the client in
I have a function that returns me only the fridays from a range of
I have a function that loads a SVG Dom from a file. Currently, it
I have a function that calls a lot of other functions from different objects.
I have a function that writes things from a binary tree. When I run
I have function that opens up a window, and the values from the newly
in my programm I have function that takes the image from camera and should
I have function that return list of strings from database. I want to show
I have a function that returns a comparison matrix from a given list: def
So I have function that formats a date to coerce to given enum DateType{CURRENT,

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.