Fsharp / how to change type Node of (string * FsTree) list into a list where 2paths cannot be identical
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
In FSharp, I would like to do the following
given a type :
type FsTree = Node of (string * FsTree) list
I would like to define a predicate
toStringList so that :
toStringList myFsTree gives the bellow result
result :
[
["n1"];
["n2"; "sub_n2_1"];
["n2"; "sub_n2_2"];
["n3"; "sub_n3"; "sub_sub_n3_1"];
["n3"; "sub_n3"; "sub_sub_n3_2"];
["n3"; "sub_n3"; "sub_sub_n3_3"];
["n4"];
]
Where
let myFsT = Node [
("n1", Node );
("n2", Node [
("sub_n2_1", Node );
("sub_n2_2", Node )
]);
("n3", Node [
("sub_n3", Node [
("sub_sub_n3_1", Node );
("sub_sub_n3_2", Node );
("sub_sub_n3_3", Node );
])
]);
("n4", Node )
]
What I have done so far (here below) is absolutely not correct, I know that. But I'm really stuck here! Does anyone have an idea what to do?
let rec test (fst:FsTree) =
match fst with
| Node ->
| Node ((str, subFst)::restNode) ->
[[str] @ (test subFst)] @ (test restNode)
f# filesystems fscheck fsharpchart
add a comment |
In FSharp, I would like to do the following
given a type :
type FsTree = Node of (string * FsTree) list
I would like to define a predicate
toStringList so that :
toStringList myFsTree gives the bellow result
result :
[
["n1"];
["n2"; "sub_n2_1"];
["n2"; "sub_n2_2"];
["n3"; "sub_n3"; "sub_sub_n3_1"];
["n3"; "sub_n3"; "sub_sub_n3_2"];
["n3"; "sub_n3"; "sub_sub_n3_3"];
["n4"];
]
Where
let myFsT = Node [
("n1", Node );
("n2", Node [
("sub_n2_1", Node );
("sub_n2_2", Node )
]);
("n3", Node [
("sub_n3", Node [
("sub_sub_n3_1", Node );
("sub_sub_n3_2", Node );
("sub_sub_n3_3", Node );
])
]);
("n4", Node )
]
What I have done so far (here below) is absolutely not correct, I know that. But I'm really stuck here! Does anyone have an idea what to do?
let rec test (fst:FsTree) =
match fst with
| Node ->
| Node ((str, subFst)::restNode) ->
[[str] @ (test subFst)] @ (test restNode)
f# filesystems fscheck fsharpchart
add a comment |
In FSharp, I would like to do the following
given a type :
type FsTree = Node of (string * FsTree) list
I would like to define a predicate
toStringList so that :
toStringList myFsTree gives the bellow result
result :
[
["n1"];
["n2"; "sub_n2_1"];
["n2"; "sub_n2_2"];
["n3"; "sub_n3"; "sub_sub_n3_1"];
["n3"; "sub_n3"; "sub_sub_n3_2"];
["n3"; "sub_n3"; "sub_sub_n3_3"];
["n4"];
]
Where
let myFsT = Node [
("n1", Node );
("n2", Node [
("sub_n2_1", Node );
("sub_n2_2", Node )
]);
("n3", Node [
("sub_n3", Node [
("sub_sub_n3_1", Node );
("sub_sub_n3_2", Node );
("sub_sub_n3_3", Node );
])
]);
("n4", Node )
]
What I have done so far (here below) is absolutely not correct, I know that. But I'm really stuck here! Does anyone have an idea what to do?
let rec test (fst:FsTree) =
match fst with
| Node ->
| Node ((str, subFst)::restNode) ->
[[str] @ (test subFst)] @ (test restNode)
f# filesystems fscheck fsharpchart
In FSharp, I would like to do the following
given a type :
type FsTree = Node of (string * FsTree) list
I would like to define a predicate
toStringList so that :
toStringList myFsTree gives the bellow result
result :
[
["n1"];
["n2"; "sub_n2_1"];
["n2"; "sub_n2_2"];
["n3"; "sub_n3"; "sub_sub_n3_1"];
["n3"; "sub_n3"; "sub_sub_n3_2"];
["n3"; "sub_n3"; "sub_sub_n3_3"];
["n4"];
]
Where
let myFsT = Node [
("n1", Node );
("n2", Node [
("sub_n2_1", Node );
("sub_n2_2", Node )
]);
("n3", Node [
("sub_n3", Node [
("sub_sub_n3_1", Node );
("sub_sub_n3_2", Node );
("sub_sub_n3_3", Node );
])
]);
("n4", Node )
]
What I have done so far (here below) is absolutely not correct, I know that. But I'm really stuck here! Does anyone have an idea what to do?
let rec test (fst:FsTree) =
match fst with
| Node ->
| Node ((str, subFst)::restNode) ->
[[str] @ (test subFst)] @ (test restNode)
f# filesystems fscheck fsharpchart
f# filesystems fscheck fsharpchart
asked Nov 29 '18 at 3:52
teddy tresor mabulayteddy tresor mabulay
576
576
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This is a tricky one because it requires 2 mutually recursive functions one for Node and one for the list inside Node.
let rec processNode prepend node =
let rec processList prepend listOfNodes =
match listOfNodes with
| ->
| (str, subNode) :: restList ->
let restList = processList prepend restList
let newPrepend = List.append prepend [ str ]
match processNode newPrepend subNode with
| -> [ newPrepend ]
| lst -> lst
@ restList
match node with Node listOfNodes -> processList prepend listOfNodes
processNode myFsT
|> List.iter print
You need one recursive function to go over the elements in the list: processList
and another one to go over the subnodes in the list: processNode.
The confusion arises because all processNode does is get the list from Node and then call processList so it is easy to think of them as if they could be just one function.
OTOH, processList is double recursive. It calls itself to go over the elements of the list and it calls processNode to go deeper into the subtree.
There is also an accumulator parameter that needs to be passed which is prepend which carries the path.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531589%2ffsharp-how-to-change-type-node-of-string-fstree-list-into-a-list-where-2pa%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a tricky one because it requires 2 mutually recursive functions one for Node and one for the list inside Node.
let rec processNode prepend node =
let rec processList prepend listOfNodes =
match listOfNodes with
| ->
| (str, subNode) :: restList ->
let restList = processList prepend restList
let newPrepend = List.append prepend [ str ]
match processNode newPrepend subNode with
| -> [ newPrepend ]
| lst -> lst
@ restList
match node with Node listOfNodes -> processList prepend listOfNodes
processNode myFsT
|> List.iter print
You need one recursive function to go over the elements in the list: processList
and another one to go over the subnodes in the list: processNode.
The confusion arises because all processNode does is get the list from Node and then call processList so it is easy to think of them as if they could be just one function.
OTOH, processList is double recursive. It calls itself to go over the elements of the list and it calls processNode to go deeper into the subtree.
There is also an accumulator parameter that needs to be passed which is prepend which carries the path.
add a comment |
This is a tricky one because it requires 2 mutually recursive functions one for Node and one for the list inside Node.
let rec processNode prepend node =
let rec processList prepend listOfNodes =
match listOfNodes with
| ->
| (str, subNode) :: restList ->
let restList = processList prepend restList
let newPrepend = List.append prepend [ str ]
match processNode newPrepend subNode with
| -> [ newPrepend ]
| lst -> lst
@ restList
match node with Node listOfNodes -> processList prepend listOfNodes
processNode myFsT
|> List.iter print
You need one recursive function to go over the elements in the list: processList
and another one to go over the subnodes in the list: processNode.
The confusion arises because all processNode does is get the list from Node and then call processList so it is easy to think of them as if they could be just one function.
OTOH, processList is double recursive. It calls itself to go over the elements of the list and it calls processNode to go deeper into the subtree.
There is also an accumulator parameter that needs to be passed which is prepend which carries the path.
add a comment |
This is a tricky one because it requires 2 mutually recursive functions one for Node and one for the list inside Node.
let rec processNode prepend node =
let rec processList prepend listOfNodes =
match listOfNodes with
| ->
| (str, subNode) :: restList ->
let restList = processList prepend restList
let newPrepend = List.append prepend [ str ]
match processNode newPrepend subNode with
| -> [ newPrepend ]
| lst -> lst
@ restList
match node with Node listOfNodes -> processList prepend listOfNodes
processNode myFsT
|> List.iter print
You need one recursive function to go over the elements in the list: processList
and another one to go over the subnodes in the list: processNode.
The confusion arises because all processNode does is get the list from Node and then call processList so it is easy to think of them as if they could be just one function.
OTOH, processList is double recursive. It calls itself to go over the elements of the list and it calls processNode to go deeper into the subtree.
There is also an accumulator parameter that needs to be passed which is prepend which carries the path.
This is a tricky one because it requires 2 mutually recursive functions one for Node and one for the list inside Node.
let rec processNode prepend node =
let rec processList prepend listOfNodes =
match listOfNodes with
| ->
| (str, subNode) :: restList ->
let restList = processList prepend restList
let newPrepend = List.append prepend [ str ]
match processNode newPrepend subNode with
| -> [ newPrepend ]
| lst -> lst
@ restList
match node with Node listOfNodes -> processList prepend listOfNodes
processNode myFsT
|> List.iter print
You need one recursive function to go over the elements in the list: processList
and another one to go over the subnodes in the list: processNode.
The confusion arises because all processNode does is get the list from Node and then call processList so it is easy to think of them as if they could be just one function.
OTOH, processList is double recursive. It calls itself to go over the elements of the list and it calls processNode to go deeper into the subtree.
There is also an accumulator parameter that needs to be passed which is prepend which carries the path.
answered Nov 29 '18 at 5:34
AMieresAMieres
3,6601611
3,6601611
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531589%2ffsharp-how-to-change-type-node-of-string-fstree-list-into-a-list-where-2pa%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown