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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:15:31+00:00 2026-06-09T08:15:31+00:00

In pure functional programming the order of execution doesn’t matter and therefore is undetermined

  • 0

In pure functional programming the order of execution doesn’t matter and therefore is undetermined (i.e. it is up to the compiler). If you have side effects the order of execution does matter. So how can it be defined in F#?

I have a function that recursively deletes all empty subfolders of a given path. In addition it deletes some files contained in them if their names are in a given list.

The algorithm is easy:

  1. Delete all files in the list.
  2. Do the recursive call for the subfolders.
  3. Delete the folder if it is empty. This must be the last step.

In addition the function returns the number of deleted elements as a tuple (numer of deleted folders, number of deleted files).

Here is my code:

let rec DeleteEmptyFolders path filenames =
    // Deletes a file or folder using the given function.
    // Returns 1 if the file could be deleted, otherwise 0.
    let Delete delete name =
        try
            delete name;
            1
        with
            | _ -> 0

    // The function result (number of deleted folders and files).
    let deletedFolders (a, _) = a
    let deletedFiles   (_, a) = a

    let accumulator a b = 
        ( deletedFolders a + deletedFolders b,
          deletedFiles a + deletedFiles b )

    // Deletes the given files and returns the number of deleted elements.
    let DeleteFiles folder names =
        names
        |> Seq.map (fun n -> Path.Combine (folder, n))
        |> Seq.map (fun n -> Delete File.Delete n)
        |> Seq.reduce (+)

    // Deletes the folder if it is empty
    // (Directory.Delete will fail if it is not empty).
    let DeleteFolder folder = Delete Directory.Delete folder

    // The recursive call
    let DeleteEmptySubFolders folder files =
        Directory.EnumerateDirectories folder
        |> Seq.map (fun p -> DeleteEmptyFolders p files)
        |> Seq.reduce (accumulator)

    // Three functions are executed: DeleteEmptySubFolders, DeleteFolder and DeleteFiles
    // But it has to be done in the correct order: DeleteFolder must be executed last.
    accumulator (DeleteEmptySubFolders path filenames) (DeleteFolder path, DeleteFiles path filenames)

I could figure out that DeleteEmptySubFolders is executed first, then DeleteFolder and DeleteFiles.
That is the order in which the functions appear in code. But I don’t think this is a rule of F#, it’s just what the compiler decided. It could be any other order.

Of course I can swap the elements in the last line of my code. The compiler would change the order of execution accordingly. But since this is not a rule of the language it would just be luck.

In another question on this topic I’ve read that values (i.e. functions without arguments) are initialized in the order in which they are declared.

let firstCall  = DeleteFiles path filenames
let secondCall = DeleteEmptySubFolders path filenames
let thirdCall  = DeleteFolder path

accumulator (secondCall) (thirdCall, firstCall)

Now the calls happen to be in the right order. But again: is this a rule of F# or just how the compiler works? (if the functions are not used the compiler might decide to not initialize the values at all)

How should I write the last line if I want to tell F# that the order of execution matters and when each call should be done? Is there a keyword or a special syntax to mark functions as not side effect free?

  • 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-06-09T08:15:34+00:00Added an answer on June 9, 2026 at 8:15 am

    F# is not a pure functional programming language: functions and values will be computed top-to-bottom, left-to-right.

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

Sidebar

Related Questions

Haskell is a pure functional language, which means Haskell functions have no side affects.
Pure functional programming languages do not allow mutable data, but some computations are more
Using pure functional languages can have a lot of benefits over using impure imperatives,
I have a pure CSS mouseover slider fully functional here http://jsfiddle.net/gU4sw/13/ . When I
I'm playing around with guile to try and get familiar with pure functional programming
I have been trying to wrap my head around functional programming for a while
How does one go about doing doubly linked lists in a pure functional language?
As Erlang is an almost pure functional programming language, I'd imagine this was possible:
I know one of the goals of pure functional programming is to eliminate mutability,
Haskell has been called a pure functional language. What does pure mean in this

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.