recursion

Interleaving Elements of a Prolog list

不羁岁月 提交于 2020-01-02 22:09:13
问题 I am new to Prolog and came across this practice excercise. The question asks to define a predicate zipper([[List1,List2]], Zippered). //this is two lists within one list. This predicate should interleave elements of List1 with elements of List2. For example, zipper([[1,3,5,7], [2,4,6,8]], Zippered) -> Zippered = [1,2,3,4,5,6,7,8]. zipper([[1,3,5], [2,4,6,7,8]], Zippered) -> Zippered = [1,2,3,4,5,6,7,8]. So far I have a solution for two different list: zipper ([],[],Z). zipper([X],[],[X]).

Interleaving Elements of a Prolog list

泄露秘密 提交于 2020-01-02 22:08:31
问题 I am new to Prolog and came across this practice excercise. The question asks to define a predicate zipper([[List1,List2]], Zippered). //this is two lists within one list. This predicate should interleave elements of List1 with elements of List2. For example, zipper([[1,3,5,7], [2,4,6,8]], Zippered) -> Zippered = [1,2,3,4,5,6,7,8]. zipper([[1,3,5], [2,4,6,7,8]], Zippered) -> Zippered = [1,2,3,4,5,6,7,8]. So far I have a solution for two different list: zipper ([],[],Z). zipper([X],[],[X]).

Recursive path finding in matrix

北城余情 提交于 2020-01-02 19:37:25
问题 I need to find a path of the number 1 in a matrix[R][C] starting from matrix[0][0] until it gets to matrix[R-1][C-1] , using recursion. I can only go down or right. In most cases, I don't have a problem. My problem is only when there is nowhere to go, and I need to take a step backwards. Per example, this is the matrix I'm getting from a file: 1 0 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 1 1 0 1 1 0 1 1 1 1 0 1 0 1 1 1 The problem is when it gets to matrix[4][0] . I don't understand why it won't

Can conditionals be used to pair balance group elements?

旧时模样 提交于 2020-01-02 16:39:48
问题 TL;DR: Is there a way to specify a conditional so that an opening element MUST match its paired closing element? Example is located on regex101.com. ===== Balancing elements in regex is typically handled through recursion. This means that nested {...{...{...}...}...} can be located. Also, PCRE allows the (?(DEFINE)...) construct, which lets you define various patterns without actually starting the match. In the regular expression # Define the opening and closing elements before the recursion

Can conditionals be used to pair balance group elements?

て烟熏妆下的殇ゞ 提交于 2020-01-02 16:39:06
问题 TL;DR: Is there a way to specify a conditional so that an opening element MUST match its paired closing element? Example is located on regex101.com. ===== Balancing elements in regex is typically handled through recursion. This means that nested {...{...{...}...}...} can be located. Also, PCRE allows the (?(DEFINE)...) construct, which lets you define various patterns without actually starting the match. In the regular expression # Define the opening and closing elements before the recursion

AngularJS - Json to Tree structure

天涯浪子 提交于 2020-01-02 16:17:53
问题 I 've seen lots of answers that turn a Json response to a Ul Tree structure. The thing is that all these subjects where around a nested response pointing out the relationship between parent object and child object. I have a non nested Json response indicating the relation by reference to the objects property. The following is part of the response: {"id":"1","parent_id":null,"name":"Item-0"}, {"id":"2","parent_id":"1","name":"Item-1"}, {"id":"3","parent_id":"2","name":"Item-2"}, {"id":"4",

Prolog - Recursive call

落爺英雄遲暮 提交于 2020-01-02 15:54:36
问题 I am having trouble with recursive searching of list and creation of list of lists from the result.. The knowledge base contains team name, number of wins and zone they are in, all associated withe their team number. I am passing list of team numbers in Teams and I am searching for a matching pair with findMinMax/3 . The result I need is... List of lists of paired teams (ex. X = [[gonzaga, washington], [iowa, oklahoma], …] ) And 1 unmatched team (resulted from odd number of teams) or 0 (in

Prolog - Recursive call

本秂侑毒 提交于 2020-01-02 15:53:35
问题 I am having trouble with recursive searching of list and creation of list of lists from the result.. The knowledge base contains team name, number of wins and zone they are in, all associated withe their team number. I am passing list of team numbers in Teams and I am searching for a matching pair with findMinMax/3 . The result I need is... List of lists of paired teams (ex. X = [[gonzaga, washington], [iowa, oklahoma], …] ) And 1 unmatched team (resulted from odd number of teams) or 0 (in

Kotlin recursion stack overflow

假如想象 提交于 2020-01-02 12:41:05
问题 I have written this recursive function in Kotlin: fun recursive(target: String, population: Population, debugFun: (String) -> Unit) : Population { if (population.solution(target).toString() == target) { return population } debugFun.invoke(population.solution(target).toString()) return recursive(target, population.evolve(target), debugFun) } It will run an indeterminate amount of times (because I'm using randomness to converge on solution in evolutionary algorithm). I am frequently getting

OCaml recursive modules across compilation units

纵然是瞬间 提交于 2020-01-02 10:09:12
问题 I'm trying to split the following recursive modules into separate compilation units. Specifically, I'd like B to be in its own b.ml, to be able to reuse it with other A's. module type AT = sig type b type t = Foo of b | Bar val f : t -> b list end module type BT = sig type a type t = { aaa: a list; bo: t option } val g : t -> t list end module rec A : (AT with type b = B.t) = struct type b = B.t type t = Foo of b | Bar let f = function Foo b -> [ b ] | Bar -> [] end and B : (BT with type a =