tail-recursion

scheme tail recursion

本小妞迷上赌 提交于 2019-12-10 19:10:07
问题 I am trying to create a scheme tail recursive function flatten-tl-rec that flattens a nested list of lists. (define flatten-tl-rec (lambda (xs) (letrec ([flatten-tl-rec-acc (lambda (xs acc) (cond ((empty? xs) acc) ((list? (first xs)) (flatten-tl-rec-acc (rest xs) (append (flatten-tl-rec-acc (first xs) '()) acc))) (else (flatten-tl-rec-acc (rest xs) (cons (first xs) acc)))) )]) (flatten-tl-rec-acc xs '())))) (flatten-tl-rec '(1 2 3 (4 5 6) ((7 8 9) 10 (11 (12 13))))) But I am getting (13 12 11

Doubt regarding a tail optimized code under 'gdb'

天大地大妈咪最大 提交于 2019-12-10 19:06:03
问题 Consider a tail recursive factorial implementation in C: #include <stdio.h> unsigned long long factorial(unsigned long long fact_so_far, unsigned long long count, unsigned long long max_count){ if (max_count==0 || max_count==1 || count >= max_count) return fact_so_far; else { printf("%llu %p \n", count, &factorial); return factorial(fact_so_far * count, ++count, max_count); } } int main(int argc, char **argv) { unsigned long long n; scanf("%llu", &n); printf("\n Factorial %llu \n",factorial(1

F# Recursive Functions: make list items unique

删除回忆录丶 提交于 2019-12-10 18:23:54
问题 let rec isolate (l:'a list) = match l with | [] -> [] | x::xs -> if memberof(x,xs) then remove (x,l) else isolate xs I've already created functions memberof and remove, the only problem is that when line 6 remove(x,l) executes it doesn't continue with isolate(xs) for continued search through the list. Is there a way to say, if x then f(x) and f(y) ? 回答1: As you are using F# immutable lists, the result of remove needs to be stored somewhere: let rec isolate (l:'a list) = match l with | [] -> [

How can I get this function to be tail-recursive?

被刻印的时光 ゝ 提交于 2019-12-10 17:43:04
问题 I'm still trying to implement 2-3 finger trees and I made good progress (repository). While doing some benchmarks I found out that my quite basic toList results in a StackOverflowException when the tree ist quite large. At first I saw an easy fix and made it tail-recursive. Unfortunately, it turned out that toList wasn't the culprit but viewr was: /// Return both the right-most element and the remaining tree (lazily). let rec viewr<'a> : FingerTree<'a> -> View<'a> = function | Empty -> Nil |

Tail recursive List.map

自作多情 提交于 2019-12-10 17:19:45
问题 The typical List.map function in OCaml is pretty simple, it takes a function and a list, and applies the function to each items of the list recursively. I now need to convert List.map into a tail recursive function, how can this be done? What should the accumulator accumulate? 回答1: Arguably the simplest approach is to implement map in terms of an tail-recursive auxiliary function map_aux that traverses the list while accumulating an already mapped prefix: let map f l = let rec map_aux acc =

Can a function be optimized for tail recursion even when there are more than one distinct recursive calls?

拈花ヽ惹草 提交于 2019-12-10 17:13:16
问题 As I mentioned in a recent SO question, I'm learning F# by going through the Project Euler problems. I now have a functioning answer to Problem 3 that looks like this: let rec findLargestPrimeFactor p n = if n = 1L then p else if n % p = 0L then findLargestPrimeFactor p (n/p) else findLargestPrimeFactor (p + 2L) n let result = findLargestPrimeFactor 3L 600851475143L However, since there are 2 execution paths that can lead to a different call to findLargestPrimeFactor , I'm not sure it can be

Is this tail recursion?

假装没事ソ 提交于 2019-12-10 17:08:02
问题 I have tried to find examples of tail recursion and I really don't see the difference between regular and tail. If this isn't tail recursion, can someone tell me why its not? public static long fib(long index) { // assume index >= 0 if (index == 0) // Base case return 0; else if (index == 1) // Base case return 1; else // Reduction and recursive calls return fib(index - 1) + fib(index - 2); } // end of method fib(long index) 回答1: No, the method in the question does not use a tail recursion. A

How can I prevent a tail recursive function from reversing the order of a List?

只愿长相守 提交于 2019-12-10 16:56:46
问题 I am experimenting with the functional List type and structural sharing. Since Javascript doesn't have a Tail Recursive Modulo Cons optimization, we can't just write List combinators like this, because they are not stack safe: const list = [1, [2, [3, [4, [5, []]]]]]; const take = n => ([head, tail]) => n === 0 ? [] : head === undefined ? [] : [head, take(n - 1) (tail)]; console.log( take(3) (list) // [1, [2, [3, []]]] ); Now I tried to implement take tail recursively, so that I can either

How is it possible that loop-recur throws a StackOverflowError?

南楼画角 提交于 2019-12-10 15:43:42
问题 I´ve been trying to implement a tail-recursive version of chainl1, but even with the loop-recur it throws a StackOverflowError. How is that possible and what can i do to change that? (defn atest [state] (when-not (and (= "" state) (not (= (first state) \a))) (list (first state) (. state (substring 1))))) (defn op [state] (when-not (and (= "" state) (not (= (first state) \a))) (list #(list :| %1 %2) (. state (substring 1))))) (defn chainl1-helper [x p op] (fn [state] (loop [x x state state]

What's some simple F# code that generates the .tail IL instruction?

可紊 提交于 2019-12-10 13:01:59
问题 I'd like to see the .tail IL instruction, but the simple recursive functions using tail calls that I've been writing are apparently optimized into loops. I'm actually guessing on this, as I'm not entirely sure what a loop looks like in Reflector. I definitely don't see any .tail opcodes though. I have "Generate tail calls" checked in my project's properties. I've also tried both Debug and Release builds in Reflector. The code I used is from Programming F# by Chris Smith, page 190: let