recursion

Recursively extracting JSON field values in Groovy

浪子不回头ぞ 提交于 2019-12-29 10:05:09
问题 I need to implement a method that will scan a string of JSON for a particular targetField and either return the value of that field (if it exists), or null (if it doesn't): // Ex: extractFieldValue(/{ "fizz" : "buzz" }/, 'fizz') => 'buzz' // Ex: extractFieldValue(/{ "fizz" : "buzz" }/, 'foo') => null String extractFieldValue(String json, String targetField) { // ... } This solution has to be recursive and work at any nesting-level in the (hierarchical) JSON string. Also it needs to work for

Explanation of lists:fold function

筅森魡賤 提交于 2019-12-29 09:05:28
问题 I learning more and more about Erlang language and have recently faced some problem. I read about foldl(Fun, Acc0, List) -> Acc1 function. I used learnyousomeerlang.com tutorial and there was an example (example is about Reverse Polish Notation Calculator in Erlang): %function that deletes all whitspaces and also execute rpn(L) when is_list(L) -> [Res] = lists:foldl(fun rpn/2, [], string:tokens(L," ")), Res. %function that converts string to integer or floating poitn value read(N) -> case

Explanation of lists:fold function

放肆的年华 提交于 2019-12-29 09:05:13
问题 I learning more and more about Erlang language and have recently faced some problem. I read about foldl(Fun, Acc0, List) -> Acc1 function. I used learnyousomeerlang.com tutorial and there was an example (example is about Reverse Polish Notation Calculator in Erlang): %function that deletes all whitspaces and also execute rpn(L) when is_list(L) -> [Res] = lists:foldl(fun rpn/2, [], string:tokens(L," ")), Res. %function that converts string to integer or floating poitn value read(N) -> case

How do you check if one array is a subsequence of another?

假如想象 提交于 2019-12-29 08:57:11
问题 I'm looking to explore different algorithms, both recursive and dynamic programming, that checks if one arrayA is a subsequence of arrayB. For example, arrayA = [1, 2, 3] arrayB = [5, 6, 1, 7, 2, 9, 3] thus, arrayA is indeed a subsequence of arrayB. I've tried a few different searches, but all I can seem to find is algorithms to compute the longest increasing subsequence. 回答1: Since you must match all elements of arrayA to some elements of arrayB , you never need to backtrack . In other words

Prolog: Split list at integer in list of lists

99封情书 提交于 2019-12-29 08:49:07
问题 I would like to split a list of words separated through integers into a list of lists. Sample query and expected result: ?- separatewords([h,e,l,l,o,1,o,v,e,r,3,t,h,e,r,e], X). X = [[h,e,l,l,o],[o,v,e,r],[t,h,e,r,e]]. The following things I already achieved: Splitting the list into one list before the first integer and one after the first integer: Sample query with result: ?- take1word([h,e,l,l,o,1,o,v,e,r,3,t,h,e,r,e], X, Y). X = [h,e,l,l,o], Y = [o,v,e,r,3,t,h,e,r,e]. % OK My code:

Finding the maximum element of an array recursively

这一生的挚爱 提交于 2019-12-29 08:41:47
问题 Consider this code, which computes the maximum element of an array. #include <stdio.h> int maximum(int arr[], int n) { if (n == 1) { return arr[0]; } else { int max = maximum(arr, n-1); printf("Largest element : %d\n", max); return 5; // return arr[n-1] > max ? arr[n-1] : max; } } int main() { int array[5] = {5, 23, 28, 7, 1}; printf("Maximum element of the array is: %d", maximum(array, 5)); return 0; } Why is the else block called four (4) times? 回答1: The function is recursive, thus it will

Finding the maximum element of an array recursively

人盡茶涼 提交于 2019-12-29 08:41:36
问题 Consider this code, which computes the maximum element of an array. #include <stdio.h> int maximum(int arr[], int n) { if (n == 1) { return arr[0]; } else { int max = maximum(arr, n-1); printf("Largest element : %d\n", max); return 5; // return arr[n-1] > max ? arr[n-1] : max; } } int main() { int array[5] = {5, 23, 28, 7, 1}; printf("Maximum element of the array is: %d", maximum(array, 5)); return 0; } Why is the else block called four (4) times? 回答1: The function is recursive, thus it will

Using linq to get list of web controls of certain type in a web page

瘦欲@ 提交于 2019-12-29 08:07:34
问题 Is there a way to use linq to get a list of textboxes in a web page regardless of their position in the tree hierarchy or containers. So instead of looping through the ControlCollection of each container to find the textboxes, do the same thing in linq, maybe in a single linq statement? 回答1: One technique I've seen is to create an extension method on ControlCollection that returns an IEnumerable ... something like this: public static IEnumerable<Control> FindAll(this ControlCollection

Sieve of Eratosthenes Scheme

岁酱吖の 提交于 2019-12-29 08:05:42
问题 I've been searching the web for an implementation of the Sieve of Eratosthenes in scheme and although I came up with a lot of content, none of them seemed to have made it like I need it to be done. The problem is most algorithms either use a static end or use iteration. This paired with my lack of knowledge of the language led me to ask all of you for help. I need an implementation of the Sieve that takes in one argument (number to Sieve until), uses only recursion and has a list of "cons" of

Why the Haskell sequence function can't be lazy or why recursive monadic functions can't be lazy

两盒软妹~` 提交于 2019-12-29 07:28:08
问题 With the question Listing all the contents of a directory by breadth-first order results in low efficiencyI learned that the low efficiency is due to a strange behavior of the recursive monad functions. Try sequence $ map return [1..]::[[Int]] sequence $ map return [1..]::Maybe [Int] and ghci will fall into an endless calculation. If we rewrite the sequence function in a more readable form like follows: sequence' [] = return [] sequence' (m:ms) = do {x<-m; xs<-sequence' ms; return (x:xs)} and