logo
down
shadow

Perl: Find a sum of an array recursively with tail call optimization


Perl: Find a sum of an array recursively with tail call optimization

Content Index :

Perl: Find a sum of an array recursively with tail call optimization
Tag : perl , By : Reiner
Date : January 12 2021, 07:00 PM

I wish did fix the issue. Here's a TCO version using that goto feature I mentioned in a comment:
#!/usr/bin/env perl
use warnings;
use strict;
use feature qw/say/;

sub sum {
    return undef if @_ == 0;
    return $_[0] if @_ == 1;
    splice @_, 0, 2, $_[0] + $_[1];
    goto ∑
}

say sum(1..100);
say sum(1..100_000);

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

How to find out if Prolog performs Tail Call Optimization


Tag : prolog , By : Dasharath Yadav
Date : March 29 2020, 07:55 AM
wish of those help To answer your question, I first looked for "trivial" goals that might prevent last call optimization. If found some:
     ;   (   ground(Input)
         ->  once(symbol:by_type_name(Tables, error, Index, _)),
             try_restore_input(Input, FailedInput, InputR),
             Input = [FailedInput | InputR],
             format(atom(Error), '~w', [FailedInput]),
             Token = Index-Error
         ;   once(symbol:by_type_name(Tables, eof, Index, _)),
             Token = Index-''
         )
?- listing(read_token).
read_token(parser(O, B), lexer(dfa-C, last_accept-T, chars-J), Q, A, S) :-
        (   A=[D|G],
            dfa:current(B, C, E),
            char_and_code(D, K, F),
            dfa:find_edge(B, E, F, H),
            N=G
        ->  table:item(dfa_table, B, H, I),
            dfa:accept(I, L),
            atom_concat(J, K, M),
            P=lexer(dfa-H, last_accept-L, chars-M),
            R=N,
            read_token(parser(O, B),
                       P,
                       Q,
                       R,
                       S)        % 1: looks nice!
        ;   (   T\=none
            ->  Q=T-J
            ;   ground(D)
            ->  once(symbol:by_type_name(B, error, W, _)),
                try_restore_input(D, U, V),
                D=[U|V],
                format(atom(X), '~w', [U]),
                Q=W-X    % 2: prevents LCO
            ;   once(symbol:by_type_name(B, eof, W, _)),
                Q=W-''   % 3: prevents LCO
            ),
            S=A    % 4: prevents LCO
        ).
?- vm_list(read_token).
========================================================================
read_token/5
========================================================================
   0 s_virgin
   1 i_exit
----------------------------------------
clause 1 ((0x1cc4710)):
----------------------------------------
   0 h_functor(parser/2)
   2 h_firstvar(5)
   4 h_firstvar(6)
   6 h_pop
   7 h_functor(lexer/3)
   9 h_functor((-)/2)
  11 h_const(dfa)
  13 h_firstvar(7)
  15 h_pop
  16 h_functor((-)/2)
  18 h_const(last_accept)
  20 h_firstvar(8)
  22 h_pop
  23 h_rfunctor((-)/2)
  25 h_const(chars)
  27 h_firstvar(9)
  29 h_pop
  30 i_enter
  31 c_ifthenelse(26,118)
  34 b_unify_var(3)
  36 h_list_ff(10,11)
  39 b_unify_exit
  40 b_var(6)
  42 b_var(7)
  44 b_firstvar(12)
  46 i_callm(dfa,dfa:current/3)
  49 b_var(10)
  51 b_firstvar(13)
  53 b_firstvar(14)
  55 i_call(char_and_code/3)
  57 b_var(6)
  59 b_var(12)
  61 b_var(14)
  63 b_firstvar(15)
  65 i_callm(dfa,dfa:find_edge/4)
  68 b_unify_fv(16,11)
  71 c_cut(26)
  73 b_const(dfa_table)
  75 b_var(6)
  77 b_var(15)
  79 b_firstvar(17)
  81 i_callm(table,table:item/4)
  84 b_var(17)
  86 b_firstvar(18)
  88 i_callm(dfa,dfa:accept/2)
  91 b_var(9)
  93 b_var(13)
  95 b_firstvar(19)
  97 i_call(atom_concat/3)
  99 b_unify_firstvar(20)
 101 b_functor(lexer/3)
 103 b_functor((-)/2)
 105 b_const(dfa)
 107 b_argvar(15)
 109 b_pop
 110 b_functor((-)/2)
 112 b_const(last_accept)
 114 b_argvar(18)
 116 b_pop
 117 b_rfunctor((-)/2)
 119 b_const(chars)
 121 b_argvar(19)
 123 b_pop
 124 b_unify_exit
 125 b_unify_fv(21,16)
 128 b_functor(parser/2)
 130 b_argvar(5)
 132 b_argvar(6)
 134 b_pop
 135 b_var(20)
 137 b_var2
 138 b_var(21)
 140 b_var(4)
 142 i_depart(read_token/5)
 144 c_var_n(22,2)
 147 c_var_n(24,2)
 150 c_jmp(152)
 152 c_ifthenelse(27,28)
 155 b_var(8)
 157 b_const(none)
 159 i_call((\=)/2)
 161 c_cut(27)
 163 b_unify_var(2)
 165 h_functor((-)/2)
 167 h_var(8)
 169 h_var(9)
 171 h_pop
 172 b_unify_exit
 173 c_var(10)
 175 c_var_n(22,2)
 178 c_var_n(24,2)
 181 c_jmp(101)
 183 c_ifthenelse(28,65)
 186 b_firstvar(10)
 188 i_call(ground/1)
 190 c_cut(28)
 192 b_functor((:)/2)
 194 b_const(symbol)
 196 b_rfunctor(by_type_name/4)
 198 b_argvar(6)
 200 b_const(error)
 202 b_argfirstvar(22)
 204 b_void
 205 b_pop
 206 i_call(once/1)
 208 b_var(10)
 210 b_firstvar(23)
 212 b_firstvar(24)
 214 i_call(try_restore_input/3)
 216 b_unify_var(10)
 218 h_list
 219 h_var(23)
 221 h_var(24)
 223 h_pop
 224 b_unify_exit
 225 b_functor(atom/1)
 227 b_argfirstvar(25)
 229 b_pop
 230 b_const('~w')
 232 b_list
 233 b_argvar(23)
 235 b_nil
 236 b_pop
 237 i_call(format/3)
 239 b_unify_var(2)
 241 h_functor((-)/2)
 243 h_var(22)
 245 h_var(25)
 247 h_pop
 248 b_unify_exit
 249 c_jmp(33)
 251 b_functor((:)/2)
 253 b_const(symbol)
 255 b_rfunctor(by_type_name/4)
 257 b_argvar(6)
 259 b_const(eof)
 261 b_argfirstvar(22)
 263 b_void
 264 b_pop
 265 i_call(once/1)
 267 b_unify_var(2)
 269 h_functor((-)/2)
 271 h_var(22)
 273 h_const('')
 275 h_pop
 276 b_unify_exit
 277 c_var(10)
 279 c_var_n(23,2)
 282 c_var(25)
 284 b_unify_vv(4,3)
 287 c_var_n(11,2)
 290 c_var_n(13,2)
 293 c_var_n(15,2)
 296 c_var_n(17,2)
 299 c_var_n(19,2)
 302 c_var(21)
 304 i_exit

Tail Recursion (Tail Call Optimization) in Swift 4


Tag : swift , By : bjorngylling
Date : March 29 2020, 07:55 AM
hop of those help? As Martin notes, you won't get TCO in any case unless you turn on the optimizer (-O), but even in that case, there's no way to guarantee that you'll get TCO, and so you really can't rely on it. Swift is not particularly friendly to recursive algorithms. Typically you'd write this as:
func sum(n: Int) -> Int {
    return (1...n).reduce(0, +)    
}
func sum(n: Int) -> Int {
    return (1...n).reversed().reduce(0, +)
}

How would I write a recursive function that sums an array of numbers that uses tail call optimization (TCO)?


Tag : javascript , By : Allen
Date : March 29 2020, 07:55 AM
it helps some times A TCO function needs to return a function call, which replaces the last stack item and prevents the stack to grow.
Therfore you need to store the total as well in the function as parameter and hand over this value at the ent of the recursion.
function sum(array, total = 0) {
    if (array.length === 0) {
        return total;
    } 
    return sum(array.slice(1), total + array[0]);
}

console.log(sum([1, 2, 3, 4, 5])); // 15

Tail Call Optimization in Go


Tag : go , By : Comfly
Date : March 29 2020, 07:55 AM

Tail call optimization besides tail recursion?


Tag : cpp , By : Tom Berthon
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • Why is object destructor not called when script terminates?
  • Archive tar files to a different location in Perl
  • Write asynchronously to file in perl
  • What is Perl's secret of getting small code do so much?
  • How to store output of module avail command in perl?
  • Odd "Use of uninitialized value", regular expression error
  • Perl append a substring with in a string
  • How do I collect coverage from child processes when running `cover -test`, and not otherwise? (Devel::Cover)
  • I need help regarding perl hash of array
  • Is there a faster way to get floating point bits than pack/unpack in Perl?
  • Conditions in Perl loops and performance
  • Packing an array causes "Can't locate method" error in perl
  • How do I do a case sensitive rename or move operation in perl?
  • how to convert column into row in perl
  • Comparison of Math::BigInt with scalars in Perl
  • perl mismatched tag, problem = XML::Simple module?
  • verify login+password in Perl via SAML
  • i want to search for a string in a file using perl
  • While running an email program on Windows ActivePerl I'm getting an error
  • How to read a excel file using perl?
  • Which http headers to suppress "This document is no longer available." on browser back button?
  • Is there any way to check whether a file is an empty file using PERL program?
  • Perl Date::Manip detects timezone wrong for EST and EDT
  • Why does the below Perl code show "Use of uninitialized value in concatenation (.) or string" when I try to us
  • How to print part of header and string in another file in Perl
  • Query about how to unpack a packed binary data
  • Can't open perl script "Build.PL": No such file or directory in response to perl Build.PL
  • Is there a way to host a webServer in perl which should in HTTPS
  • How do I concisely check warning categories up to the root of the hierarchy in my package?
  • Use tr with arrays
  • I'm facing problem with the email program in Perl
  • How to install Filter::Crypto module for perl under ubuntu
  • Count tandem repeats in Perl
  • Graph.pm - how to get all paths with particular length?
  • how get the last directory using perl?
  • Perl parsing email body without parts using MIME::Parser
  • Why Perl ignores spaces between a sigil and variable name?
  • Using Perl to replace empty string with space
  • Displaying 12-h time in Perl using DateTime
  • Assign temp file from cgi and read it in .sh
  • why doesn't make -C change $PWD as seen through a scripting language such as Perl?
  • Using Win32::Env perl module, environment variable not setting permanently in windows
  • How to fetch a one table from HTML source file using lwp module?
  • How to process variable dimension array in Perl
  • perl print to a file and STDOUT which is the file
  • Get array in perl from json_encode
  • How to get unicode code points in perl v5.24?
  • using an array as a reference is deprecated in perl
  • Using Perl's File::Basename to rename a file extension
  • Unable to get page via HTTPS with LWP::Simple in Perl
  • Perl DBI case insensitive methods
  • How to resolve @INC related issue in Perl
  • How to do unary and/reduction of bus to a bit in Perl?
  • What does perl -pe "s|$PASSWORD|<PASSWORD>|g"?
  • I want to replace last / by ,
  • Strange regular expression
  • What does "!" mean before a variable in perl
  • Getting the inner hash in Perl
  • perl : can't require $value from other perl file and use it in 3rd perl file
  • perl assign reference to subroutine
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com