Comment on: quiz. write a function recurseList(f,x,n) that returst a list, [f(x), f(f(x)), ...], length n.
0 01 Nov 2017 12:09 u/TumsFestival in v/programmingComment 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
$xis mutated: it receives the previous call's result and becomes the next call's argument.outputs: