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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:53:17+00:00 2026-05-25T22:53:17+00:00

I have a function where I want to transform a list of floats into

  • 0

I have a function where I want to transform a list of floats into another one, where for each element I want to have x percent of element i spill over into element i + 1

example:

let p3 = [0.1; 0.2; 0.4; 0.2; 0.1]

then p3_s should be:

[0.05; 0.15; 0.3; 0.3; 0.2]

To do this I took half of each element and added it to the next element.

  • 0.1 became 0.05 because it gave 0.05 to the next, there is no previous element
  • 0.2 became 0.15 because it gave 0.1 to the next and got 0.05 from the
    first
  • etc
  • and finally 0.1 became 0.2 because it .01 from the
    previous. There is no next element.

Now I came up with this which works but only for list of size 5:

// create list
let p3 = [0.1; 0.2; 0.4; 0.2; 0.1]

let shiftList orgList shift =    

    // chop list up in tuples of what stays and what moves
    let ms = orgList |> List.map (fun p-> (p * shift, p * (1.0-shift))) 

    // map new list 
    ms |> List.mapi (fun i (move, stay) -> 
        match i with 
        | 0 -> stay
        | 4 -> stay + fst ms.[i-1] + move // note hardcoded 4
        | _ -> stay + fst ms.[i-1])

// get shifted list
shiftList p3 0.5

Now for the questions:

1) How do I make it match on any length list? Now I hardcoded the 4 in the match expression but I’d like to be able to accept any lenght list.

I tried this:

let shiftList orgList shift =    

    // chop list up in tuples of what stays and what moves
    let ms = orgList |> List.map (fun p-> (p * shift, p * (1.0-shift))) 

    // find length 
    let last = orgList.Length - 1

    // map new list 
    ms |> List.mapi (fun i (move, stay) -> 
        match i with 
        | 0     -> stay
        | last  -> stay + fst ms.[i-1] + move 
        | _     -> stay + fst ms.[i-1]) // now this one will never be matched

But this will not treat last as the number 4, instead it becomes a variable for i even though last is already declared above.

So how could I match on a variable, so that I can treat the last elmement differently? Finding the first one is easy because it’s at 0.

2) How would you do this? I’m still pretty fresh to F# there are many things I don’t know about yet. Guess the general case here is: how do I map a different function to the first and last element of a list, and a general one to the others?

Thanks in advance,

Gert-Jan

  • 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-25T22:53:18+00:00Added an answer on May 25, 2026 at 10:53 pm

    You want to do:

    let shiftList orgList shift =    
    
        // chop list up in tuples of what stays and what moves
        let ms = orgList |> List.map (fun p-> (p * shift, p * (1.0-shift))) 
    
        // find length 
        let last = orgList.Length - 1
    
        // map new list 
        ms |> List.mapi (fun i (move, stay) -> 
            match i with 
            | 0     -> stay
            | last' when last' = last -> stay + fst ms.[i-1] + move 
            | _     -> stay + fst ms.[i-1]) // now this one will never be matched
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one function ParseHtmlTable(string htmlContent) . Now in that function I want to
I have one r function i want to run it automatically(scheduling) after every predefined
I have a textfile that I am I want to make into a list.
I have IQueryable list of objects of type T which I want to transform
I have a three dimensional array. I want to right a function to transform
using .NET 3.5. I have a function I want to make multithreaded calls to.
I have a function that I want to execute on keyup for 2 different
I have a function that i want to pass as a parameter function myFun(){
I have a function that I want to pass an argument, market, to the
I have a function that I want to run when a specific URL path

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.