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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:17:19+00:00 2026-05-15T16:17:19+00:00

I’m trying to sort each Position (which is a list) in a Position list.

  • 0

I’m trying to sort each Position (which is a list) in a Position list. Currently I’m doint like this:

type Position = Position of  list<int * Piece>

and my function:

let SortPositionList positionList : Position list = 
let rec loop list =
    match (list: Position list) with
    | [] -> []
    | hd::tl -> [List.sortBy(fun (x:(int*Piece)) -> fst x)(GetInternStruct(hd))::loop tl]
loop positionList

In my mind, this could be done by recursivelly sorting the actual head of the list and then concat it with the rest of the list, but it’s not working. The errors in this function are:

Type mismatch. Expecting a (int * Piece) list list but given a (int * Piece) list list list The type ‘int * Piece’ does not match the type ‘(int * Piece) list’, in the underlined loop tl

Type mismatch. Expecting a Position list but given a (int * Piece) list list list The type ‘Position’ does not match the type ‘(int * Piece) list list’, in the underline calling of loop, loop positionList

Hope you can help, thanks in advance

Pedro Dusso

EDIT
AList.map passing the sorting function would be a nice aproach?

SOLUTION

let SortPositionList (positionList : Position list) =
    List.map (fun (Position(position)) -> List.sort(position)) positionList

As my Position struct is a (int * Piece) lst, I’m pattern matching it in the anonimous function and sorting it.

Thanks for the answers!

  • 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-15T16:17:20+00:00Added an answer on May 15, 2026 at 4:17 pm

    In general, there are two ways of doing things in F#. You can either use recursion explicitly or you can use existing functions. In your case, you need to do two things in a nested way – you need to iterate over the outer list and sort the inner lists. The sorting can be done using List.sortBy and the iteration (projection) can be done using List.map.

    To correct your original approach (using recursion) – I simplfied it slightly (because you don’t need the loop function – you can make the function itself recursive):

    let rec sortPositionList list =  
      match list with 
      | [] -> [] 
      | hd::tl -> 
        // Sort the list of positions first - by storing this as a new value
        // using 'let', you get more readable code (and you can use IntelliSense
        // to explore the type of 'sorted' and understand what's going on)
        let sorted = List.sortBy (fun (x, _) -> x) (GetInternStruct(hd))
    
        // As Brian suggests, more idiomatic F# encoding of the line would be:
        //   let sorted = GetInternStruct(hd) |> List.sortBy (fun (x, _) -> x)
        // but both of the options would work in this case.
    
        // Note: The result shouldn't be wrapped in '[ .. ]'. The operator '::'
        // takes an element and a list and returns a new list created by 
        // prepending the element in front of the list
        sorted::(sortPositionList tl)
    

    The solution using existing functions (List.map) has been already posted by JDU. I would just add that he uses partial function application – so the parameter passed to List.map is a function. If this feels confusing, you can rewrite that using lambda function explicitly:

    let SortPositionList (positionList) = 
      List.map (fun positions -> 
        List.sortBy (fun (index, _) -> index) positions) positionList 
    

    Which could be more idiomatically written using the pipelining operator and the fst function instead of explicit lambda parameter (as Brian mentioned):

    let SortPositionList (positionList) = 
      positionList |> List.map (fun positions -> 
        positions |> List.sortBy fst)  
    

    This means exactly the same thing as the code posted by JDU, but you may find it more readable. Finally, you can write the same thing using sequence expressions (which is perhaps the most elegant option in my opinion):

    let SortPositionList (positionList) = 
      [ for positions in positionList do
          yield positions |> List.sortBy fst ]
    

    EDIT The functions as I wrote them here work with values of type (int*Point) list list and not with the type Positions list. To change this, you need to add some wrapping and unwrapping. The recursive implementation should be:

      match list with         // List is always 'Positions', so we use pattern 
      | Positions [] -> []    // matching to unwrap the underlying list in 
      | Positions (hd::tl) -> // both cases
        // Wrap the resulting list into the positions type
        let sorted = Positions(List.sortBy (fun (x, _) -> x) (GetInternStruct(hd)))
        (sorted::(sortPositionList tl))
    

    Similarly, for the List.map implementation:

    let SortPositionList (positionList) = 
      positionList |> List.map (fun (Positions positions) -> // pattern matching
        positions |> List.sortBy fst |> Positions)  // wrapping
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.