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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:56:04+00:00 2026-05-10T21:56:04+00:00

I’m still working on groking the F# thing – trying to work out how

  • 0

I’m still working on groking the F# thing – trying to work out how to ‘think’ in F# rather than just translating from other languages I know.

I’ve recently been thinking about the cases where you don’t have a 1:1 map between before and after. Cases where List.map falls down.

One example of this is moving averages, where typically you will have len-n+1 results for a list of length len when averaging over n items.

For the gurus out there, is this a good way to do it (using queue pinched from Jomo Fisher)?

//Immutable queue, with added Length member type Fifo<'a> =     new()={xs=[];rxs=[]}     new(xs,rxs)={xs=xs;rxs=rxs}      val xs: 'a list;     val rxs: 'a list;      static member Empty() = new Fifo<'a>()     member q.IsEmpty = (q.xs = []) && (q.rxs = [])     member q.Enqueue(x) = Fifo(q.xs,x::q.rxs)     member q.Length() = (List.length q.xs) + (List.length q.rxs)     member q.Take() =         if q.IsEmpty then failwith 'fifo.Take: empty queue'         else match q.xs with                 | [] -> (Fifo(List.rev q.rxs,[])).Take()                 | y::ys -> (Fifo(ys, q.rxs)),y  //List module, add function to split one list into two parts (not safe if n > lst length) module List =     let splitat n lst =         let rec loop acc n lst =             if List.length acc = n then                 (List.rev acc, lst)             else                 loop (List.hd lst :: acc) n (List.tl lst)         loop [] n lst  //Return list with moving average accross len elements of lst let MovingAverage (len:int) (lst:float list) =      //ugly mean - including this in Fifo kills genericity     let qMean (q:Fifo<float>) = ((List.sum q.xs) + (List.sum q.rxs))/(float (q.Length()))      //get first part of list to initialise queue     let (init, rest) = List.splitat len lst      //initialise queue with first n items     let q = new Fifo<float>([], init)      //loop through input list, use fifo to push/pull values as they come     let rec loop (acc:float list) ls (q:Fifo<float>) =         match ls with         | [] -> List.rev acc         | h::t ->              let nq = q.Enqueue(h) //enqueue new value             let (nq, _) = nq.Take() //drop old value             loop ((qMean nq)::acc) t nq //tail recursion      loop [qMean q] rest q  //Example usage     MovingAverage 3 [1.;1.;1.;1.;1.;2.;2.;2.;2.;2.] 

(Maybe a better way would be to implement a MovingAverageQueue by inheriting from Fifo?)

  • 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. 2026-05-10T21:56:04+00:00Added an answer on May 10, 2026 at 9:56 pm

    If you don’t care too much about performance, here is a very simple solution:

    #light  let MovingAverage n s =    Seq.windowed n s    |> Seq.map Array.average  let avgs = MovingAverage 5000 (Seq.map float [|1..999999|])  for avg in avgs do     printfn '%f' avg     System.Console.ReadKey() |> ignore 

    This recomputes the average of each ‘window’ from scratch, so it is poor if the windows are large.

    In any case, check out Seq.windowed:

    http://research.microsoft.com/projects/cambridge/fsharp/manual/FSharp.Core/Microsoft.FSharp.Collections.Seq.html

    as it’s handy to have in your back pocket for such things.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I am currently running into a problem where an element is coming back from
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns 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.