algorithm - Recursion with accumulators that are not reversed - is it possible? -


I have recently been playing with Haskel with a reasonable amount, and I can search for n I came with this function th prime:

  nthPrime 1 = 2 nthPrime 2 = 3 nthPrime n = aux [2, 3] 3 5 n where aux is known to be the first suspect suspect In the case of current currentPrime = foldl (\ ln -> & gt; l & amp; amp; amp; suspicious` mod`n / = 0) the first known (current IsPrime, soughtNth == currentNth) (True, True) - & Gt; Suspicious (right, wrong) - & gt; Aux (Suspect: known light) (current no + 1) (questionable +2) Demand mother _ - & gt; My question is, there is no reversal of a consolidated parameter (in this case  knownprom ) which is not reversed. (As  (suspected: knownprimes) ) is passed on 

I have tried to use the known light ++ [suspicion] But it also seems incapable.

I hope that if I can pass the known prime in order, then I can shortcut some priority checks.

In Haskell, if you are using an accumulator to create a list, At the end of it, it often happens that it is better to leave the accumulator and instead creates the list lazy as the result of your calculation.

If you search for such thinking for primes, and take full advantage of laziness, then you end up with a famous technique of an infinite of all primes If we can use this technique to make the list, we can retrieve your code as much as possible,

  allPrimes = [2, 3] ++ aux 5 Where aux suspect = move currentIsPrime = foldl (\ ln - & gt; l & amp; amp; suspicious` mod` n / = 0) while taking the true $ (\ n - & gt; n * n & lt; = Questionable) all prime In sector terms currentIsPrime true - & gt; Suspect: aux (suspicion + 2) false - & gt; Aux (suspicion + 2) nthPrime n = allPrimes !! (N-1)  

I have now removed unnecessary parameters and have replaced the code by making it into a composite production, and using its own result as the source of the main divider In order to do (it is called "knot"). Apart from this, the only change here is to add a le-leasing check: Since we are testing the separator from the list it is defined in the context of itself, and is infinite to boot, we need to know Where to stop the stop to check the separators, so that we do not really re-invent.

In addition, this code has a incompetence:

  foldl (\ ln - & gt; l & amp; suspicious` mod` n / = 0) True  

This is not a good way to check whether there is no separator, because a list is in writing , after dividing it once will not stop , even if and & nbsp; Amp; And is shortcutting (as soon as its first debate is found to be False )

foldr (\ nr - & gt; suspicious `mod` n / = ) 0 & amp; Amp; R) Use the predefined function all :

  all (\ N - & gt;) right  

or better ; Suspicious `mod`n / = 0)

Usage of my comments

It seems that if you all and it's a little bit Reactor:

  allPrimes :: [integer] allprimes = 2: 3: aux 5 where aux suspect | CurrentIsPrime = Suspicious: next pages | Otherwise = the next generation, where the current ISPRME = all (\ n - & gt; suspected `modi 'n / = 0) $ ($ n = & gt; n * n & lt; = doubtful) allPrimes nextPrimes = aux (Suspicion + 2) nthPrime :: int - & gt; Integer nthPrime n = allPrimes !! (N-1)  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -