u/TumsFestival - 2 Archived Voat Posts in v/programming
u/TumsFestival
  • home
  • search

u/TumsFestival

0 posts · 2 comments · 2 total

Active in: v/programming (2)

  • ‹‹‹
  • ‹‹
  • ‹
  • 1
  • ›
  • ››
  • ›››
Comment on: quiz. write a function recurseList(f,x,n) that returst a list, [f(x), f(f(x)), ...], length n.

$x is mutated: it receives the previous call's result and becomes the next call's argument.

say join ', ', recurse_list sub ($x) { $x * 2 }, 1, 5;

outputs:

2, 4, 8, 16, 32
0 01 Nov 2017 12:09 u/TumsFestival in v/programming
Comment on: quiz. write a function recurseList(f,x,n) that returst a list, [f(x), f(f(x)), ...], length n.

Perl 5:

sub recurse_list ($f, $x, $n) { 
    map { $x = $f->($x) } 1 .. $n
}

example:

#! /usr/bin/perl
sub recurse_list ($f, $x, $n) { 
    map { $x = $f->($x) } 1 .. $n
}
say join ', ', map $_->[1], 
    recurse_list
        sub ($x) {
            [ $x->[1], $x->[0] + $x->[1] ]
        },
        [1, 1], 5;

outputs:

2, 3, 5, 8, 13
0 01 Nov 2017 06:03 u/TumsFestival in v/programming
  • ‹‹‹
  • ‹‹
  • ‹
  • 1
  • ›
  • ››
  • ›››

archive has 9,592 posts and 65,719 comments. source code.