What’s the explanation for Exercise 1.6 in SICP?
new-if is a function. When a function is called, what’s the first thing that Scheme does with the argument list? It evaluates all the arguments.
new-if is a function. When a function is called, what’s the first thing that Scheme does with the argument list? It evaluates all the arguments.
There are a few ways to do this. You can put closures into a struct and pass this struct to the closure. You can even define structs inline in a function: fn main() { struct Fact<‘s> { f: &’s dyn Fn(&Fact, u32) -> u32 } let fact = Fact { f: &|fact, x| if x … Read more
The only way I can think of to do this amounts to giving the function a name: fact = lambda x: 1 if x == 0 else x * fact(x-1) or alternately, for earlier versions of python: fact = lambda x: x == 0 and 1 or x * fact(x-1) Update: using the ideas from … Read more
Yes, they can. Starting with C++23 you can use the explicit this parameter: auto factorial = [](this auto self, int i) { return (i == 1) ? 1 : i * self(i – 1); }; With previous C++ standards, you can store the lambda in a variable and reference that variable (although you cannot declare … Read more
You could use this overload of Directory.GetFiles which searches subdirectories for you, for example: string[] files = Directory.GetFiles(sDir, “*.xml”, SearchOption.AllDirectories); Only one extension can be searched for like that, but you could use something like: var extensions = new List<string> { “.txt”, “.xml” }; string[] files = Directory.GetFiles(sDir, “*.*”, SearchOption.AllDirectories) .Where(f => extensions.IndexOf(Path.GetExtension(f)) >= 0).ToArray(); … Read more
This is expected. This is what happens for an instance of B. class A { void recursive(int i) { // <– 3. this gets called System.out.println(“A.recursive(” + i + “)”); if (i > 0) { recursive(i – 1); // <– 4. this calls the overriden “recursive” method in class B, going back to 1. } … Read more
Extremely loosely speaking, a catamorphism is just a slight generalization of fold, and an anamorphism is a slight generalization of unfold. (And a hylomorphism is just an unfold followed by a fold.). They’re presented in a more rigorous form usually, to make the connection to category theory clearer. The denser form lets us distinguish data … Read more
larsmans answered your first question For your second question, can you simply look before you leap to avoid recursion? def makeList(self, aNode=None): if aNode is None: aNode = self.root treeaslist = [aNode.data] if aNode.lChild: treeaslist.extend(self.makeList(aNode.lChild)) if aNode.rChild: treeaslist.extend(self.makeList(aNode.rChild)) return treeaslist
If you want to extract the files to the respective folder you can try this find . -name “*.zip” | while read filename; do unzip -o -d “`dirname “$filename”`” “$filename”; done; A multi-processed version for systems that can handle high I/O: find . -name “*.zip” | xargs -P 5 -I fileName sh -c ‘unzip -o … Read more
Some very simple, generic tree building: function buildTree(array $elements, $parentId = 0) { $branch = array(); foreach ($elements as $element) { if ($element[‘parent_id’] == $parentId) { $children = buildTree($elements, $element[‘id’]); if ($children) { $element[‘children’] = $children; } $branch[] = $element; } } return $branch; } $tree = buildTree($rows); The algorithm is pretty simple: Take the … Read more