recursion

Is it possible to make a recursive closure in Rust?

巧了我就是萌 提交于 2020-01-26 17:10:51
问题 This is a very simple example, but how would I do something similar to: let fact = |x: u32| { match x { 0 => 1, _ => x * fact(x - 1), } }; I know that this specific example can be easily done with iteration, but I'm wondering if it's possible to make a recursive function in Rust for more complicated things (such as traversing trees) or if I'm required to use my own stack instead. 回答1: There are a few ways to do this. You can put closures into a struct and pass this struct to the closure. You

Is it possible to make a recursive closure in Rust?

别等时光非礼了梦想. 提交于 2020-01-26 17:08:54
问题 This is a very simple example, but how would I do something similar to: let fact = |x: u32| { match x { 0 => 1, _ => x * fact(x - 1), } }; I know that this specific example can be easily done with iteration, but I'm wondering if it's possible to make a recursive function in Rust for more complicated things (such as traversing trees) or if I'm required to use my own stack instead. 回答1: There are a few ways to do this. You can put closures into a struct and pass this struct to the closure. You

Huffman Tree decoding

我怕爱的太早我们不能终老 提交于 2020-01-26 04:58:26
问题 Given a Huffman tree and a stream of bits, return a pair containing (1) the -- string of symbols encoded by the bits (according to the Huffman tree), and -- (2) a Bool indicating whether the output stream contains every bit from the -- input (that is, return False if there were any bits left over). Here is the code, it only returns the first symbol in the tree. What's the problem? data BTree a = Leaf a | Fork (BTree a) (BTree a) deriving (Show, Eq) traT :: BTree a -> BTree a -> [Bool] -> [a]

Find repeating values in List

一曲冷凌霜 提交于 2020-01-26 01:46:06
问题 I want to return true if I find a repeating value in the list let rec repeats L = match L with | [] -> false | x::xs when x = xs.Head -> true | x::xs -> repeats xs;; repeats [1;2;3;4;5] Should return false. But I get this error: System.InvalidOperationException: The input list was empty. at Microsoft.FSharp.Collections.FSharpList`1.get_Head() at FSI_0003.repeats[a](FSharpList`1 L) at <StartupCode$FSI_0004>.$FSI_0004.main@() at main@dm() Stopped due to error What should I do to fix the error?

Prolog permutation predicate using insertion of elements

China☆狼群 提交于 2020-01-25 21:46:24
问题 I am trying to write a predicate permutation/2 so that it is true if and only if both arguments are list, one a permutation of the other. To do this, I've written the two helper predicates delete/3 and insert/3 . The first is true if and only if the third argument is the second argument, both lists, with the first instance of the element in the first argument removed. The second is true if and only if the third argument equals the second argument with the first argument (element) inserted.

Prolog permutation predicate using insertion of elements

大憨熊 提交于 2020-01-25 21:46:09
问题 I am trying to write a predicate permutation/2 so that it is true if and only if both arguments are list, one a permutation of the other. To do this, I've written the two helper predicates delete/3 and insert/3 . The first is true if and only if the third argument is the second argument, both lists, with the first instance of the element in the first argument removed. The second is true if and only if the third argument equals the second argument with the first argument (element) inserted.

Recursion on a list in Scheme - avoid premature termination

无人久伴 提交于 2020-01-25 11:57:27
问题 I was doing a problem from the HTDP book where you have to create a function that finds all the permutations for the list. The book gives the main function, and the question asks for you to create the helper function that would insert an element everywhere in the list. The helper function, called insert_everywhere , is only given 2 parameters. No matter how hard I try, I can't seem to create this function using only two parameters. This is my code: (define (insert_everywhere elt lst) (cond [

Hilbert curve using turtle graphics and recursion

杀马特。学长 韩版系。学妹 提交于 2020-01-25 11:15:06
问题 I'm trying to implement an L-System generated Hilbert curve ,making use of python turtle graphics and recursion. My code seems to be working for the first two levels of recursion n=1 and n=2 but beyond that , the graphics just entangled (although I´m able to observe further modules within them), and I can´t seem to grasp what might be wrong here, do I need some intermediate steps to regenerate the Hilbert modules for deeper levels of recursion? Please see my code below , its relatively simple

BackTracking function is not working as expected

微笑、不失礼 提交于 2020-01-25 10:13:31
问题 I am trying to solve the following question using BackTracking in C but I don't know how to continue from here... The question is: Chris is planning to travel in a country with N cities. He will get help from a matrix NxN that the cell (I,J) represents the length of the road from city I to City J. The length of the road from City A to city B is not the same when compared to the road from City B to City A. The Road From the same source city back to it (directly) is 0. Chris noticed that the

Trying to print a triangle recursively in lisp

ε祈祈猫儿з 提交于 2020-01-25 08:57:07
问题 Trying to print a triangle recursively in lisp. I get an overflow but I don't know from where. Mind you I am new to Lisp programming. (defun triangle (n) (if (not (oddp n))(progn (print "This is not an odd integer") (return-from triangle n))) (if (< n 1) '()) (setf lst (cons (car(list n)) (triangle (- n 2)))) (print lst)) (triangle 7) 回答1: Let's review some parts of your code. (if (not (oddp n)) (progn (print ...) (return-from ...))) When you have an if with only one branch, think about using