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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:19:48+00:00 2026-06-05T15:19:48+00:00

I am trying to port some code form java do F# that generates a

  • 0

I am trying to port some code form java do F# that generates a grid of multidimensional points around a given point. I came up with this:

let gridGenerator midpoint stepSize steps = 
    seq {
        let n = Array.length midpoint
        let direction = Array.create n -steps
        let mutable lastIndex = n-1
        while lastIndex>=0 do
            let next = midpoint |> Array.mapi (fun i x -> x+ direction.[i]*stepSize)
            while lastIndex>=0 && direction.[lastIndex]=steps do 
                direction.[lastIndex]<- (-steps)
                lastIndex<-lastIndex-1;        
            if lastIndex>=0 then
                direction.[lastIndex]<-direction.[lastIndex]+1;
                lastIndex <- n-1;
            yield next;
    }

Apart from this code being horribly imperative (I would be grateful for hints how to fix it), I am getting a compilation error:

Program.fs(18,15): error FS0407: The mutable variable ‘lastIndex’ is used in an invalid way. Mutable variables cannot be captured by closures. Consider eliminating this use of mutation or using a heap-allocated mutable reference cell via ‘ref’ and ‘!’.

How can I fix this error? How can I make it more functional?

EXAMPLE: For midpoint [|0.0, 1.0|], step size 0.5 and steps 1 I expect (in any order really)

seq{[|-0.5, 0.5|], [|-0.5, 1.0|], [|-0.5, 1.5|], [|0.0, 0.5|], [|0.0, 1.0|], [|0.0, 1.5|], [|0.5, 0.5|], [|0.5, 1.0|], [|0.5, 1.5|]}

Please also note that this will be executed many times, so performance is critical.

  • 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-05T15:19:49+00:00Added an answer on June 5, 2026 at 3:19 pm
    let gridGenerator midpoint stepSize steps =
        seq {
            let n = Array.length midpoint
            let direction = Array.create n -steps
            let lastIndex = ref (n - 1)
            while !lastIndex >= 0 do
                let next = midpoint |> Array.mapi (fun i x -> x + direction.[i] * stepSize)
                while !lastIndex >= 0 && direction.[!lastIndex] = steps do
                    direction.[!lastIndex] <- -steps
                    decr lastIndex
                if !lastIndex >= 0 then
                    direction.[!lastIndex] <- direction.[!lastIndex] + 1
                    lastIndex := n - 1
                yield next
        }
    

    ?

    ref‘s are very good for such uses, and are not considered mutable variables (because they are not).

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

Sidebar

Related Questions

I am trying to port some java code to scala. The code uses annotations
I am trying to port some code from a regular Java program into the
I'm trying to port some code to 64-bit, but it seems that the thread
I'm trying to port some Octave code to Java. I'm trying out atan2 so
I am trying to figure out how to port some .Net code that parsed
I'm trying to port some Visual C++ (VS2005) code so that it will compile
I'm trying to port some code from Play Framework Java to Play Framework Scala
I'm trying to port some Java code, which requires arithmetic and logical bit shifts,
I am trying to port some code using DevExpress XtraGrid to Wpf. What is
I'm trying to port some Windows code to Linux, ideally through platform-independent libraries (eg

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.