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

  • Home
  • SEARCH
  • 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 73903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:16:28+00:00 2026-05-10T20:16:28+00:00

Inspired by this question , I wanted to try my hand at the latest

  • 0

Inspired by this question, I wanted to try my hand at the latest ponder this challenge, using F#

My approach is probably completely off course, but in the course of solving this problem, I’m trying to get a list of all the permutations of the digits 0-9.

I’m looking at solving it using a n-ary tree like so:

type Node =      | Branch of (int * Node list)     | Leaf of int 

I’m quite pleased with myself, because I’ve managed to work out how to generate the tree that I want.

My problem now is that I can’t work out how to traverse this tree and extract the ‘path’ to each leaf as an int. Thing thing that is confusing me is that I need to match on individual Nodes, but my ‘outer’ function needs to take a Node list.

My current attempt almost does the right thing, except that it returns me the sum of all the paths…

let test = Branch(3, [Branch(2, [Leaf(1)]);Branch(1, [Leaf(2)])])  let rec visitor lst acc =      let inner n =          match n with         | Leaf(h) -> acc * 10 + h         | Branch(h, t) -> visitor t (acc * 10 + h)     List.map inner lst |> List.sum  visitor [test] 0 //-> gives 633 (which is 321 + 312) 

And I’m not even sure that this is tail-recursive.

(You’re quite welcome to propose another solution for finding permutations, but I’m still interested in the solution to this particular problem)

EDIT: I’ve posted a generic permutations algorithm in F# here.

  • 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-10T20:16:28+00:00Added an answer on May 10, 2026 at 8:16 pm

    regarding your question about list traversal – you can start by writing a function that returns lists that represent the path – that’s I think easier and it will be later easy to turn it into a function that returns a number.

    This one takes a list as the first argument (path so far) and a tree and returns a list> type – that is all the possible paths from the current branch.

    let rec visitor lst tree =    match tree with   | Branch(n, sub) -> List.collect (visitor (n::lst)) sub   | Leaf(n) -> [List.rev (n::lst)]  // For example... > let tr = Branch(1, [Leaf(3); Branch(2, [Leaf(4); Leaf(5)] )]);; > visitor [] tr;; val it : int list list = [[1; 3]; [1; 2; 4]; [1; 2; 5]] 

    In the ‘Leaf’ case, we simply add the current number to the list and return the result as a list containing single list (we have to reverse it first, because we were adding numbers to the beginning). In the ‘Branch’ case, we add ‘n’ to the list and recursively call the visitor to process all the sub-nodes of the current branch. This returns a bunch of lists and we use ‘map_concat’ to turn them into a single list that contains all posble paths from the current branch.

    Now, you can rewrite this to return a list of integers:

    let rec visitor2 lst tree =    match tree with   | Branch(n, sub) -> List.collect (visitor2 (lst * 10 + n)) sub   | Leaf(n) -> [lst * 10 + n]  // For example...   > visitor2 0 tr;; val it : int list = [13; 124; 125]   

    Instead of concatenating lists, we now calculate the number.

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

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer In the sub-query vs simple (non-recursive) CTE versions, they are… May 11, 2026 at 1:50 pm
  • added an answer Whoops, misread the question, you want either an AoAoH or… May 11, 2026 at 1:50 pm
  • added an answer MD5 is the way to go. May 11, 2026 at 1:50 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.