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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:33:40+00:00 2026-05-15T19:33:40+00:00

I’m attempting a few minor tasks in F# to help get a handle on

  • 0

I’m attempting a few minor tasks in F# to help get a handle on the language.

I would like to write a function that takes a n-dimensional list and returns a 1-dimensional list containing all the elements from each dimension.

For example, if the input was the following 3-dimensional list: [[[1;2];[3;4]];[[5;6];[7;8]]], the output would be: [1;2;3;4;5;6;7;8]

For 2-dimensions -> 1-dimension the function is pretty straightforward:

let coalesce list= List.collect(fun item -> item) list

Here is my attempt to generalize this to n-dimensions:

let rec coalesce (list, dimension) = 
    if dimension = 1 then list 
    else coalesce (List.collect(fun item -> item) list, dimension - 1)

I get the following error when I try to compile:

error FS0001: Type mismatch. Expecting a
‘a list list
but given a
‘a list
The resulting type would be infinite when unifying ”a’ and ”a list’

The issue is here:

List.collect(fun item -> item) list

There’s obviously something wrong with my thinking. What’s the proper way to write this sort of function?

  • 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-15T19:33:41+00:00Added an answer on May 15, 2026 at 7:33 pm

    This operation is not well-typed, but here’s a sample that works on IEnumerables and returns a list<obj>:

    let rec coalesce(list:System.Collections.IEnumerable, dim) =
        [
            if dim=1 then for x in list do yield x
            else
                for x in list do
                    match x with
                    | :? System.Collections.IEnumerable as s ->
                        yield! coalesce(s, dim-1)
                    | _ -> failwith "bad shape"
        ]
    printfn "%A" (coalesce([1;2], 1))
    printfn "%A" (coalesce([[1;2];[3;4]], 2))
    printfn "%A" (coalesce([[[1;2];[3;4]];[[5;6];[7]]], 3))
    

    You can also write

    let rec flatten(list:System.Collections.IEnumerable) =
        [for x in list do
            match x with
            | :? System.Collections.IEnumerable as s -> yield! flatten(s)
            | _ -> yield x
        ]
    

    which is more general, e.g.

    let weird : obj list = [[box [1;2]; box 3]; 4; [box [5;6]; box 7]]
    printfn "%A" (flatten weird)
    

    EDIT

    @Jon Harrop suggested another strategy – create a new type for nested lists:

    type NestedListElement<'T> = //'
        | L of NestedListElement<'T> list //'
        | V of 'T //'
    
    let rec flatten nlist = 
        [for x in nlist do 
            match x with 
            | L l -> yield! flatten l
            | V v -> yield v
        ] 
    
    let nested = [L[L[V 1;V 2]; V 3]; V 4; L[L[V 5;V 6]; V 7]] 
    printfn "%A" (flatten nested) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

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.