mutual-recursion

What is the standard way to optimise mutual recursion in F#/Scala?

依然范特西╮ 提交于 2019-12-03 13:02:37
These languages do not support mutually recursive functions optimization 'natively', so I guess it must be trampoline or.. heh.. rewriting as a loop) Do I miss something? UPDATE: It seems that I did lie about FSharp, but I just didn't see an example of mutual tail-calls while googling First of all, F# supports mutually recursive functions natively, because it can benefit from the tailcall instruction that's available in the .NET IL ( MSDN ). However, this is a bit tricky and may not work on some alternative implementations of .NET (e.g. Compact Frameworks), so you may sometimes need to deal

what is a mutually recursive type?

会有一股神秘感。 提交于 2019-12-02 14:21:34
问题 If in ML, an example of a recursive datatype is: datatype llist = Nil | Node of int * llist What is a mutually recursive datatype and whats an example of it, in ML? 回答1: One such example could be these stupid datatypes. datatype a = A | Ab of b and b = B | Ba of a They make no sense, but they show that it is possible to use the and keyword (just as with functions) to reference something "ahead" which is normally not possible They are mutually (as they both...) recursive (...reference each

what is a mutually recursive type?

寵の児 提交于 2019-12-02 08:11:01
If in ML, an example of a recursive datatype is: datatype llist = Nil | Node of int * llist What is a mutually recursive datatype and whats an example of it, in ML? One such example could be these stupid datatypes. datatype a = A | Ab of b and b = B | Ba of a They make no sense, but they show that it is possible to use the and keyword (just as with functions) to reference something "ahead" which is normally not possible They are mutually (as they both...) recursive (...reference each other) The standard basic example of mutually recursive data types is a tree and a forest: a forest is a list

Problem determining how to order F# types due to circular references

≡放荡痞女 提交于 2019-11-30 11:43:42
I have some types that extend a common type, and these are my models. I then have DAO types for each model type for CRUD operations. I now have a need for a function that will allow me to find an id given any model type, so I created a new type for some miscellaneous functions. The problem is that I don't know how to order these types. Currently I have models before dao, but I somehow need DAOMisc before CityDAO and CityDAO before DAOMisc , which isn't possible. The simple approach would be to put this function in each DAO, referring to just the types that can come before it, so, State comes

Problem determining how to order F# types due to circular references

試著忘記壹切 提交于 2019-11-29 17:25:56
问题 I have some types that extend a common type, and these are my models. I then have DAO types for each model type for CRUD operations. I now have a need for a function that will allow me to find an id given any model type, so I created a new type for some miscellaneous functions. The problem is that I don't know how to order these types. Currently I have models before dao, but I somehow need DAOMisc before CityDAO and CityDAO before DAOMisc , which isn't possible. The simple approach would be

Mutually recursive classes

怎甘沉沦 提交于 2019-11-27 09:17:49
How do I implement mutually recursive classes in C++? Something like: /* * Recursion.h * */ #ifndef RECURSION_H_ #define RECURSION_H_ class Class1 { Class2* Class2_ptr; public: void Class1_method() { //... (*Class2_ptr).Class2_method(); //... } }; class Class2 { Class1* Class1_ptr; public: void Class2_method() { //... (*Class1_ptr).Class1_method(); //... }; }; #endif /* RECURSION_H_ */ Forward-declare the classes (you could get away with forward-declaring only one of them, but for good form do both). Forward-declare the methods (ditto). class Class1; class Class2; class Class1 { Class2* Class2

How to have two functions that call each other C++

痴心易碎 提交于 2019-11-27 09:16:07
I have 2 functions like this that does obfuscation on if loop: void funcA(string str) { size_t f = str.find("if"); if(f!=string::npos) { funcB(str); //obfuscate if-loop } } void funcB(string str) { //obfuscate if loop funcA(body_of_if_loop); //to check if there is a nested if-loop } The problem with this would be that funcA would not be able to see funcB and vice versa if I put funcB before funcA . Would appreciate any help or advice here. What you want is forward declaration . In your case: void funcB(string str); void funcA(string str) { size_t f = str.find("if"); if(f!=string::npos) { funcB

Mutually recursive classes

余生颓废 提交于 2019-11-26 14:37:38
问题 How do I implement mutually recursive classes in C++? Something like: /* * Recursion.h * */ #ifndef RECURSION_H_ #define RECURSION_H_ class Class1 { Class2* Class2_ptr; public: void Class1_method() { //... (*Class2_ptr).Class2_method(); //... } }; class Class2 { Class1* Class1_ptr; public: void Class2_method() { //... (*Class1_ptr).Class1_method(); //... }; }; #endif /* RECURSION_H_ */ 回答1: Forward-declare the classes (you could get away with forward-declaring only one of them, but for good

How to have two functions that call each other C++

こ雲淡風輕ζ 提交于 2019-11-26 14:36:37
问题 I have 2 functions like this that does obfuscation on if loop: void funcA(string str) { size_t f = str.find("if"); if(f!=string::npos) { funcB(str); //obfuscate if-loop } } void funcB(string str) { //obfuscate if loop funcA(body_of_if_loop); //to check if there is a nested if-loop } The problem with this would be that funcA would not be able to see funcB and vice versa if I put funcB before funcA . Would appreciate any help or advice here. 回答1: What you want is forward declaration. In your

F# forward type declarations

隐身守侯 提交于 2019-11-26 08:28:36
问题 I stumbled across this problem in F#. Suppose, I want to declare two types that reference each other: type firstType = | T1 of secondType //................ type secondType = | T1 of firstType //................ How do I do that, so the compiler does not generate an error? 回答1: You use 'and': type firstType = | T1 of secondType and secondType = | T1 of firstType 回答2: I figured it. It's: type firstType = | T1 of secondType //................ and secondType = | T1 of firstType //...............