recursion

Recursive Haskell function seemingly doesn't terminate

ぐ巨炮叔叔 提交于 2020-01-02 09:11:42
问题 To improve my Haskell skills, I'm trying to solve the Advent of Code 2018. As expected, I am already stuck on day 1, specifically on part 2: --- Part Two --- You notice that the device repeats the same frequency change list over and over. To calibrate the device, you need to find the first frequency it reaches twice. For example, using the same list of changes above, the device would loop as follows: Current frequency 0, change of +1; resulting frequency 1. Current frequency 1, change of -2;

Recursive Haskell function seemingly doesn't terminate

放肆的年华 提交于 2020-01-02 09:11:02
问题 To improve my Haskell skills, I'm trying to solve the Advent of Code 2018. As expected, I am already stuck on day 1, specifically on part 2: --- Part Two --- You notice that the device repeats the same frequency change list over and over. To calibrate the device, you need to find the first frequency it reaches twice. For example, using the same list of changes above, the device would loop as follows: Current frequency 0, change of +1; resulting frequency 1. Current frequency 1, change of -2;

How to make a tail-recusive method that can also refer to itself in a non-tail-recursive way

别来无恙 提交于 2020-01-02 08:42:09
问题 Suppose I have a mechanism for long-running computations that can suspend themselves to be resumed later: sealed trait LongRunning[+R]; case class Result[+R](result: R) extends LongRunning[R]; case class Suspend[+R](cont: () => LongRunning[R]) extends LongRunning[R]; The simplest way how to run them is @annotation.tailrec def repeat[R](body: LongRunning[R]): R = body match { case Result(r) => r case Suspend(c) => { // perhaps do some other processing here println("Continuing suspended

c# Recursive Reflection & Generic Lists setting default properties

为君一笑 提交于 2020-01-02 06:54:12
问题 I am trying to to use reflection to achieve the following: I need a method where i pass in an object and this method will recursively instantiate the object with child objects and set the properties with default values. I need the entire object instantiated going as many levels as needed. this method needs to be able to handle an object with a multiple properties that will be generic lists of other objects. Here is my sample code (I am getting a parameter count mismatch exception when i get

Recursion inside while loop, How does it work?

邮差的信 提交于 2020-01-02 06:21:40
问题 Can you please tell me how does this java code work? : public class Main { public static void main (String[] args) { Strangemethod(5); } public static void Strangemethod(int len) { while(len > 1){ System.out.println(len-1); Strangemethod(len - 1); } } } I tried to debug it and follow the code step by step but I didn't understand it. update: sorry I didn't mention that I know the result of this code but just want to know the steps of the execution.. 回答1: That'll print 4 3 2 1 1 1 1 1 1... And

Recursive Custom Configuration in c#

梦想的初衷 提交于 2020-01-02 06:21:04
问题 I am trying to create a custom configuration section that follows the following recursive structure: <monitorSettings> <monitor description="description1" /> <monitor description="description2" /> <monitor description="description3"> <monitor description="description3.1" /> <monitor description="description3.2" /> </monitor> </monitorSettings> Is this possible? I am not sure how I would lay out the configuration classes. I have the following for the monitor: public class Monitor :

Recursive Custom Configuration in c#

試著忘記壹切 提交于 2020-01-02 06:20:13
问题 I am trying to create a custom configuration section that follows the following recursive structure: <monitorSettings> <monitor description="description1" /> <monitor description="description2" /> <monitor description="description3"> <monitor description="description3.1" /> <monitor description="description3.2" /> </monitor> </monitorSettings> Is this possible? I am not sure how I would lay out the configuration classes. I have the following for the monitor: public class Monitor :

SQL select descendants of a row

我与影子孤独终老i 提交于 2020-01-02 05:50:35
问题 Suppose a tree structure is implemented in SQL like this: CREATE TABLE nodes ( id INTEGER PRIMARY KEY, parent INTEGER -- references nodes(id) ); Although cycles can be created in this representation, let's assume we never let that happen. The table will only store a collection of roots (records where parent is null) and their descendants. The goal is to, given an id of a node on the table, find all nodes that are descendants of it. A is a descendant of B if either A 's parent is B or A 's

Alternative for recursively making recursive calculations in sqlite?

☆樱花仙子☆ 提交于 2020-01-02 05:40:29
问题 I am currently working on a project for the iPhone that requires accessing a large amount of hierarchical data stored in a local sqlite database. One of the more common operations is calculating a rollup status field. Right now, I'm doing that by recursing through all the descendants of that item (which can be anywhere from 1 to n levels deep). However, this ends up requiring a LOT of sql calls. Each sqlite call on an iPhone takes around 250ms to complete, and in the end this adds up to

Writing a simple syntax parser

北慕城南 提交于 2020-01-02 05:21:07
问题 Here's what I'd like to do - in Php: given a string, have a result like this: (a()?b|c) a is a function that returns true of false. This should give b or c after calling a() (a()?(b()?d|e)|c) . Same principle. The final result should be d , e or c (a()?(b()?d|e)|(c()?f|g)) . Same principle. The final result should be d , e , f or g The problem I'm facing is that a (in my previous examples) can be an expression too, like this: ((h() ? a | i) ? (b() ? d | e) | (c() ? f | g)) I'm trying to use a