mutual-recursion

how to make HTML from a list in scheme, racket

北城以北 提交于 2021-02-10 06:48:33
问题 This is a very long question ... I am new and joined, so please don't attack me. Apologies for my bad communications in English. I have some defintions: An HTML(H) is one of Str Tag A Tag is (cons Sym (listof H)) I want to use mutual recursion,make the HTML into real HTML code. For example, (list 'html (list 'head (list 'title "Hi")) (list 'body (list 'h1 "Welcome") "Text")) Turns into: "<html><head><title>Hi</title></head><body><h1>Welcome</h1>Text</body></html>" This should work for any

Mutually recursive evaluator in Haskell

吃可爱长大的小学妹 提交于 2021-02-10 04:55:29
问题 Update: I've added an answer that describes my final solution (hint: the single Expr data type wasn't sufficient). I'm writing an evaluator for a little expression language, but I'm stuck on the LetRec construct. This is the language: type Var = String type Binds = [(Var, Expr)] data Expr = Var Var | Lam Var Expr | App Expr Expr | Con Int | Sub Expr Expr | If Expr Expr Expr | Let Var Expr Expr | LetRec Binds Expr deriving (Show, Eq) And this this the evaluator so far: data Value = ValInt Int

How to call two functions and use their results as arguments for each other?

泄露秘密 提交于 2020-01-17 11:17:12
问题 I have code: let join a ~with':b ~by:key = let rec a' = link a ~to':b' ~by:key and b' = link b ~to':a' ~by:(Key.opposite key) in a' and compilation result for it is: Error: This kind of expression is not allowed as right-hand side of `let rec' build complete I can rewrite it to: let join a ~with':b ~by:key = let rec a'() = link a ~to':(b'()) ~by:key and b'() = link b ~to':(a'()) ~by:(Key.opposite key) in a'() It is compilable variant, but implemented function is infinitely recursive and it is

C++ Mutually Recursive Variant Type

断了今生、忘了曾经 提交于 2020-01-14 10:48:29
问题 I am trying to represent a PDF object type in c++ using variants. A PDF object is one of the following: Boolean Integer Real String Name Stream Array<Object> Map<Object, Object> As you can see, the Object type is mutually recursive because the Array type would require a declaration of the Map type which would require a declaration of the Array type. How could I go abouts representing this type in c++? If a variant isn't the best way, what is? Here is what I have tried so far but it doesn't

Mutual Recursion Question

梦想与她 提交于 2019-12-24 05:12:30
问题 How do I change two functions that are Mutual Recursive to each other to make them into a linear recursion? Do I have to have both the methods in a single method? 回答1: You should be able to simply "inline" the implementation of the second method, into the first method. That is, public static void methA() { // snippet 1 methB(); // snippet 2 } public static void methB() { // snippet 3 methA(); // snippet 4 } becomes public static void methAB() { // snippet 1 // snippet 3 methAB(); // snippet 2

Mutual Recursion Question

▼魔方 西西 提交于 2019-12-24 05:12:14
问题 How do I change two functions that are Mutual Recursive to each other to make them into a linear recursion? Do I have to have both the methods in a single method? 回答1: You should be able to simply "inline" the implementation of the second method, into the first method. That is, public static void methA() { // snippet 1 methB(); // snippet 2 } public static void methB() { // snippet 3 methA(); // snippet 4 } becomes public static void methAB() { // snippet 1 // snippet 3 methAB(); // snippet 2

Is it possible to define types that depend on each other and are defined in separated files?

独自空忆成欢 提交于 2019-12-22 08:18:54
问题 I am trying to implement a library with extended parsing capabilities. I decided that I will use fsyacc because I knew it from the university. Unfortunately I encountered following problem. I defined a class for the head of my grammar ( Head ) and place its implementation in a single file. Then I defined parser as: ... %start head %type <Head> head ... Fsyacc generates seeparated module ( Parser ). In order to succeed it has to be compiled in following order: Head.fs Parser.fs In order to

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

北城余情 提交于 2019-12-21 04:17:15
问题 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 回答1: 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

How does python implement mutual recursion?

▼魔方 西西 提交于 2019-12-14 00:18:59
问题 Moving to python with C/Java background, I recently had to implement a mutual recursion, but something in python is bothering me: since a python program is interpreted line by line, if I have two functions one after another in the same python file: def A(n): B(n-1) # if I add A(1) here, it gives me an error def B(n): if n <= 0: return else: A(n-1) When the interpreter is reading A , B is not yet defined, however this code does not give me an error TL;DR My understanding is that, when def is

Unable to understand a mutual recursion

99封情书 提交于 2019-12-12 09:44:19
问题 I am reading Programming In Haskell, in the 8th chapter, the author gives an example of writing parsers. The full source is here: http://www.cs.nott.ac.uk/~gmh/Parsing.lhs I can't understand the following part: many permits zero or more applications of p , whereas many1 requires at least one successful application: many :: Parser a → Parser [a ] many p = many1 p +++ return [ ] many1 :: Parser a → Parser [a ] many1 p = do v ← p vs ← many p return (v : vs) How the recursive call happens at vs <