Traverse a tree recursively and return an un-muted list of all values found? Possible? - recursion

I wonder if this is even possible at all as the question suggest.
My problem is that I cannot seem to grasp how to handle the fact that a given input value can have multiple children. The problem is easily solved by using the mutable SortedSet variable as shown below. But I would really like to find out if this is a problem possible to solve with pure recursion and creation of new un-muted lists or similar. I hope my question is clear. I fear I'm ignorant to the easy conclusion that it's not possible.
As you can see bellow the if(true) will return a list but the else will return a list of list. So the code bellow is not in working state.
let someSet = new System.Collections.Generic.SortedSet<string>()
let rec children(value:string,listSoFar) =
printfn "ID: %A" value
someSet.Add(value) works fine of course.
let newList = List.append listSoFar [value]
if(not (hasChildren(value))) then
newList
else
let tmpCollection = database.GetCollection<Collection>("Collection")
let tmpQuery = Query.EQ("Field",BsonValue.Create(value))
let tmpRes = tmpCollection.Find(tmpQuery)
[ for child in tmpRes do
yield children(child.Value,newList) ]
let resultList = children("aParentStartValue",[])
//Or do i need to use someSet values?

Unless the tree is very deeply nested (in which case, this would be inefficient), you can write the code as a recursive F# sequence expression that generates elements using yield and yield!
let rec children (value:string) = seq {
// Produce the current value as the next element of the sequence
yield value
if hasChildren value then
// If it has children, then get all the children
let tmpCollection = database.GetCollection<Collection>("Collection")
let tmpQuery = Query.EQ("Field",BsonValue.Create(value))
let tmpRes = tmpCollection.Find(tmpQuery)
// For each child, generate all its sub-children recursively
// and return all such elements as part of this sequence using 'yield!'
for child in tmpRes do
yield! children child.Value }
// Using 'List.ofSeq' to fully evaluate the lazy sequence
let resultList = List.ofSeq (children "aParentStartValue")
If the tree is more deeply nested, then the situation is a bit more difficult. When iterating over all the children, you'd need to pass the list collected so far to the first children, get the results and then pass the resulting list to the next children (using something like List.fold). But the above is clean and should work in most cases.

Related

Is there a short way to extract exactly one element from a `Vec`?

I have a reference to a Vec and I want to extract a reference to exactly one element from it, and panic if there are any more elements or zero. The equivalent if I had an array would be:
let [x] = list;
but Vecs are dynamically sized, so that won't work here. I can think of one way to do it with a reference to a Vec, and a couple more that require ownership, but I'm wondering if there's a shorter and simpler way.
Unowned Option 1: Use assert! and indexing
assert_eq!(list.len(), 1);
let x = &list[0];
Owned Option 1: Use try_into()
let [x]: [i32; 1] = list.try_into().unwrap();
Owned Option 2: Use assert! and pop
assert_eq!(list.len(), 1);
let x = list.pop();
So, is there a shorter and clearer way?
You can use a slice pattern (playground):
let v = vec![1u32];
let x = match v.as_slice() {
&[x] => x,
_ => panic!("expected single element"),
};
// ... use x ...
It's not necessarily shorter, but it's clear, doesn't require ownership or mutation, and can be nicely generalized to more than one element to extract.
If instead of panicking you want to check the element count and extract x if it's the only element, you can use if let:
if let &[x] = v.as_slice() {
// ... use x ...
}

Recursively wrapping up an element

Say I have an element <x>x</x> and some empty elements (<a/>, <b/>, <c/>), and I want to wrap up the first inside the second one at a time, resulting in <c><b><a><x>x</x></a></b></c>. How do I go about this when I don't know the number of the empty elements?
I can do
xquery version "3.0";
declare function local:wrap-up($inner-element as element(), $outer-elements as element()+) as element()+ {
if (count($outer-elements) eq 3)
then element{node-name($outer-elements[3])}{element{node-name($outer-elements[2])}{element{node-name($outer-elements[1])}{$inner-element}}}
else
if (count($outer-elements) eq 2)
then element{node-name($outer-elements[2])}{element{node-name($outer-elements[1])}{$inner-element}}
else
if (count($outer-elements) eq 1)
then element{node-name($outer-elements[1])}{$inner-element}
else ($outer-elements, $inner-element)
};
let $inner-element := <x>x</x>
let $outer-elements := (<a/>, <b/>, <c/>)
return
local:wrap-up($inner-element, $outer-elements)
but is there a way to do this by recursion, not decending and parsing but ascending and constructing?
In functional programming, you usually try to work with the first element and the tail of a list, so the canonical solution would be to reverse the input before nesting the elements:
declare function local:recursive-wrap-up($elements as element()+) as element() {
let $head := head($elements)
let $tail := tail($elements)
return
element { name($head) } { (
$head/#*,
$head/node(),
if ($tail)
then local:recursive-wrap-up($tail)
else ()
) }
};
let $inner-element := <x>x</x>
let $outer-elements := (<a/>, <b/>, <c/>)
return (
local:wrap-up($inner-element, $outer-elements),
local:recursive-wrap-up(reverse(($inner-element, $outer-elements)))
)
Whether reverse(...) will actually require reversing the output or not will depend on your XQuery engine. In the end, reversing does not increase computational complexity, and might not only result in cleaner code, but even faster execution!
Similar could be achieved by turning everything upside down, but there are no functions for getting the last element and everything before this, and will possibly reduce performance when using predicates last() and position() < last(). You could use XQuery arrays, but will have to pass counters in each recursive function call.
Which solution is fastest in the end will require benchmarking using the specific XQuery engine and code.

how return a new type with an update value

If I want to change a value on a list, I will return a new list with the new value instead of changing the value on the old list.
Now I have four types. I need to update the value location in varEnd, instead of changing the value, I need to return a new type with the update value
type varEnd = {
v: ctype;
k: varkind;
l: location;
}
;;
type varStart = {
ct: ctype;
sy: sTable;
n: int;
stm: stmt list;
e: expr
}
and sEntry = Var of varEnd | Fun of varStart
and sTable = (string * sEntry) list
type environment = sTable list;;
(a function where environment is the only parameter i can use)
let allocateMem (env:environment) : environment =
I tried to use List.iter, but it changes the value directly, which type is also not mutable. I think List.fold will be a better option.
The biggest issue i have is there are four different types.
I think you're saying that you know how to change an element of a list by constructing a new list.
Now you want to do this to an environment, and an environment is a list of quite complicated things. But this doesn't make any difference, the way to change the list is the same. The only difference is that the replacement value will be a complicated thing.
I don't know what you mean when you say you have four types. I see a lot more than four types listed here. But on the other hand, an environment seems to contain things of basically two different types.
Maybe (but possibly not) you're saying you don't know a good way to change just one of the four fields of a record while leaving the others the same. This is something for which there's a good answer. Assume that x is something of type varEnd. Then you can say:
{ x with l = loc }
If, in fact, you don't know how to modify an element of a list by creating a new list, then that's the thing to figure out first. You can do it with a fold, but in fact you can also do it with List.map, which is a little simpler. You can't do it with List.iter.
Update
Assume we have a record type like this:
type r = { a: int; b: float; }
Here's a function that takes r list list and adds 1.0 to the b fields of those records whose a fields are 0.
let incr_ll rll =
let f r = if r.a = 0 then { r with b = r.b +. 1.0 } else r in
List.map (List.map f) rll
The type of this function is r list list -> r list list.

F# Recursive Tree Validation

This is a somewhat beginner question. I have been trying to validate the following type of FamilyTree. I can't find a simple way to do this. All help would be appreciated.
type BirthYear = int;;
type Tree = Person of BirthYear * Children
and Children = Tree list;;
I want to validate a given family tree such that every Person is older than their Children and furthermore check if the list of Children is sorted in order of their age (eldest first). Preferably done with a function that return a boolean. Something along the lines of this:
let rec validate (Person(x,child)) =
let vali = child |> List.forall (fun (y,_) -> y < x)
I'd do something like this:
let rec checkAges minBirth = function
| Person(b, _) :: t -> b >= minBirth && checkAges b t
| [] -> true
let rec validate (Person(b, c)) =
List.forall validate c && checkAges (b + minParentAge) c
where minParentAge is set to a reasonable minimum age to have children at.
I'd expect checkAges to be the more difficult part here: the function checks whether the first child it sees is younger than the limit it is given, then recursively checks the next child, with the current child's age as the new limit.
Note some techniques:
The function that checks child ages takes the minimum birthday as input; this is used to validate that the parent is old enough for the first child to be reasonable.
List.forall checks a predicate for all items in a list, and early-outs if a predicate is not fulfilled
function is a shorthand to create a function that does pattern matching on its parameter. Therefore, checkAges actually has two arguments.
Here's a very simple solution using a single recursive function. It's not relying on built-in functions like List.forall but I think it's very declarative and (hopefully) easy to follow.
Rule 1: Every Person is older than their Children
Rule 2: List of Children is sorted in order of their age (eldest first)
Code:
let rec isValid = function
| Person ( _ , []) -> true // Person alone without childs -> always valid
| Person (minYear, Person (year, childs) :: brothers) ->
year > minYear && // Validate Rules (either 1 or 2)
isValid (Person (year, childs)) && // Enforce Rule 1
isValid (Person (year, brothers)) // Enforce Rule 2
I personally don't feel List.forall fits well here, it helps to solve a part of the problem but not the whole, so you need to combine it with more stuff (see the other answers) and in the end you can't avoid a recursive function.
List functions are good for lists but for trees I feel recursion more natural unless your tree provides already a way to traverse it.
Here's a way to do it. Perhaps spending some time analyzing how this works will be helpful to you.
let rec check (Person(age, children)) =
match children with
| [] -> true
| Person(eldest, _)::_ ->
Seq.pairwise children |> Seq.forall ((<||) (>))
&& age > eldest
&& List.forall check children

Recursive search with Lua objects

In Lua, I have a tree relationship structure between objects where an object can have multiple children, but only one parent object, i.e.
obj---obj1---obj2---objd3---obj4---obj5---obj6
If I want to know obj6's 'distant' parents instead of just the immediate parent obj5, how can I achieve that? I just need a list of parents two or more levels above the current object, and the API I'm working with only has a obj.parent property.
Pseudo-code would also be helpful to get me in the right direction.
obj.parent -- immediate parent (obj5)
obj.parent.parent -- parent's parent (obj4)
obj.parent.parent.parent -- parent's parent's parent (obj3)
So on and so forth?
If you want to avoid trying to referencing a non-existent parent, I supposed you could do something like:
function getAncestor(obj, depth)
if not obj.parent then
return nil
elseif depth > 1 then
return getAncestor(obj.parent, depth-1)
end
return obj.parent
end
-- get parent
obj = getAncestor(obj6)
-- get great great grandparent
obj = getAncestor(obj6, 3)
Well, if your api supports .parent, can't you do something like the following? I'm rusty with Lua but this should offer a start.
local function GetAncestors(child)
local ancestors = {};
if child.parent then
local i = 0;
ancestors[0] = child.parent;
while ancestors[i].parent do
ancestors[i + 1] = ancestors[i].parent;
i = i + 1;
end
end
return ancestors;
end

Resources