recursion

Prolog recrusive Algorithm

帅比萌擦擦* 提交于 2019-12-25 04:12:01
问题 foo(0,Y,Z) :- Z is Y. foo(X,0,Z) :- Z is X. foo(X,Y,Z) :- X>=Y, M1 is X-2, foo(M1, Y, Zx), Z is Zx + Y. foo(X,Y,Z) :- Y<X, N1 is Y-3, foo(X, N1, Zx), Z is Zx + X. So this is my program and this is what i'm trying to accomplish 𝑓𝑜𝑜(𝑥, 𝑦) = { 𝑥 𝑖𝑓 𝑦 ≤ 0 𝑦 𝑖𝑓 𝑥 ≤ 0 𝑥 + 𝑓𝑜𝑜(𝑥 − 2, 𝑦) 𝑖𝑓 𝑥 ≥ 𝑦 𝑦 + 𝑓𝑜𝑜(𝑥, 𝑦 − 3) 𝑖𝑓 𝑥 < 𝑦 } Why does my program not output anything? This is what i think i'm saying - If X = 0, foo(0,Y,Z), than return Z as Y. If Y = 0, foo(0,Y,Z), than return Z as X. if X>=Y, than do

Propositional logic in order to interpret two arguments

℡╲_俬逩灬. 提交于 2019-12-25 04:09:30
问题 I'm trying to write a function that will take in two arguments (one list and one dictionary) and return the output for instance, like so: >>>interpret(["door_open", "AND", "cat_gone"], {"door_open" : "false", "cat_gone" : "true", "cat_asleep" : "true"}) 'false' or interpret(["cat_asleep", "OR", ["NOT", "cat_gone"]], {"door_open" : "false", "cat_gone" : "true", "cat_asleep" : "true"}) 'true' .......................................................................................................

Check consecutive numbers recursively using Lisp

痞子三分冷 提交于 2019-12-25 04:06:30
问题 I'm trying to write a recursive function to check if the elements of a list are increasing consecutively. (defun test (lst) (if (null lst) 1 (if (= (car lst) (1- (test (cdr lst)))) 1 0))) (setq consecutive '(1 2 3 4)) (setq non-consecutive '(2 5 3 6)) The results are: CL-USER> (test non-consecutive) 0 CL-USER> (test consecutive) 0 (test consecutive) should return 1. How can I write this function correctly? 回答1: To check that the numbers in the sequence are consecutive , i.e., increasing with

How to rewrite this function as a recursive function?

时光毁灭记忆、已成空白 提交于 2019-12-25 04:05:12
问题 def digits(n): res = [] while n > 0: res.append(n % 10) n /= 10 return res I want to rewrite this function so it uses recursion. I'm currently lost as to what to do. Can anyone give me some direction? 回答1: Here's a possible solution: def digits(n): if n < 10: return [n] return digits(n/10) + [n%10] digits(123) > [1, 2, 3] The above solution fixes a bug in your code, you were returning the digits in reverse order. Also notice that n must be an integer greater than or equal to zero for

Where is the bug in my recursive JSON parser?

梦想的初衷 提交于 2019-12-25 03:43:26
问题 I'd like to create a recursive function to parse json-like data as below. When key is xtype, a new class will be created. In particular, when xtype = gridpanel/treepanel, all the properties have to be its constructor argument, otherwise, properties will be added after class has been created. My recursive function as below, I got an error 'too much recursion' at line 21 in ext-all.js. Please take a look, how am I able to solve this problem? codes in main program: me.recursiveParser(null, data

dynamic array key additions

放肆的年华 提交于 2019-12-25 03:39:29
问题 Here is my precode... $keys = array('a', 'b', 'c', 'd'); $number = 10; And here is my code... eval('$array[\''.implode('\'][\'',$keys).'\'] = $number;'); Using this, I get the following result... Array ( [a] => Array ( [b] => Array ( [c] => Array ( [d] => 10 ) ) ) ) Now, the problem is that this is the exact result I want, but I don't want to use eval() . As input to my code, I have a list of keys and a number . The number should be set to the value of the keys array being used to generate

How to generate all possible strings, with all ascii chars, to a certain length

柔情痞子 提交于 2019-12-25 03:34:37
问题 I've been struggling a bit today, trying to make a function, that can generate all possible strings, with all possible ascii chars, up to a certain length. So basically, first a single char, from 0-255. Then two where I get AA, then AB, then AC... etc (but using ascii char codes, to get all possible values in each spot). Does that make sense? I guess I suck at recursion, because every attempt I make, either gets way too complicated for myself to figure it out, or ends up only increasing the

Update UITextView during recursive function is running

↘锁芯ラ 提交于 2019-12-25 03:26:26
问题 I have a view controller with a UITextView loaded into the frame. I want to update the text of the UITextView every time the function calls itself. I attempt to update the UITextView on the main thread but it doesn't seem to set the text of the View UNTIL after the recursive function is done running. 'Maze.h' is the object that defines the protocol and 'MainViewController.m' is the delegate. Heres the code for the controller: 'MainViewController.m' - (void)viewDidAppear:(BOOL)animated {

Recursive loop (C#)

Deadly 提交于 2019-12-25 03:12:31
问题 Can someone please explain it to me? I wrote a function to calculate the factorial of a number like this in C#: public int factorial(int input) { if (input == 0 || input == 1) return 1; else { int temp = 1; for (int i = 1; i <= input; i++) temp = temp * i; return temp; } } But I found some C++ code (I don't really know any C++ btw) which finds a factorial using a recursive loop: int factorial(int number) { int temp; if(number <= 1) return 1; temp = number * factorial(number - 1); return temp;

PHP: Building an Adjacency List through Recursive Iteration

爱⌒轻易说出口 提交于 2019-12-25 02:59:41
问题 I'm trying to build a flattened array that preserves metadata from a pretty tricky array coming from a view in my CodeIgniter project. That metadata is things like an identifier, depth, and parent node. The data is from a query builder JavaScript library that allows a user to generate rules that will be used in business logic. I need to persist this data, and the model I've gone with to represent the tree-like nature of these rules is an adjacency list. Here's what I have, and it does work