Your company is just getting started on a project, and for the first time you decided to go use a functional programming code-style. However your boss is really diffident and doesn't want to use built-in functions, and requires you to implement yourself the main functions. In particular you need to write the functions: Map
, Nest
, Apply
, Range
, Fold
and Table
in a language on your choice. The boss is a really busy man, and he wants to have the programs as short as possible, so he doesn't waste time reading. He also would like not for you to use loops, therefore you will have a 10% reduction on the byte count for not using loops.
The detailed requirements of the functions are below:
Map
The Map
function takes two parameters: f
and list
where f
is a function and list
is a list of values. It should return the f
applied to each element of list
. Therefore it will work as such:
Map(f,{a,b,c})
returns
{ f(a), f(b), f(c) }
and
Map(f, {{a,b},{b,c}})
returns
{ f({a,b}), f({b,c})}
Nest
The Nest
function takes three parameters as well: f
, arg
, times
where f
is a function, arg
is its starting argument, and times
is how many times the function is applied. It should return an expression with f
applied times
times to arg
. Therefore it will work as such:
Nest(f, x, 3)
returns
f(f(f(x)))
and
Nest(f, {a,b}, 3)
returns
f(f(f({a,b})))
Apply
The Apply
function takes two parameters: f
and args
where f
is a function and args
a list. It should apply f
to the args
. Therefore:
Apply(f, {a,b,c})
returns
f(a,b,c)
Range
The Range
function takes one integerr
and outputs the integers up to that number. Therefore:
Range(5)
returns
{ 1, 2, 3, 4, 5}
Fold
The Fold
function takes three parameters f
, arg
, others
where f
is a function, arg
is simple parameter, and others
a list. It will work as such:
Fold(f, x, {a, b, c, d})
returns
f(f(f(f(x,a),b),c),d)
Table
The table functions should take a function f
, and a parameter called iterator
in the form: {iMin, iMax}
where iMin
and iMax
are integers. You should apply f
over the range specified. Therefore:
Table(f, {0, 5})
returns
{f(0), f(1), f(2), f(3), f(4), f(5)}
I've used the definition of these functions from the Mathematica functional programming page, so head there if you need any more guidance. Note that you will not need to implement all the version of the functions shown in that page, but only those written in this post.
Standard Loopholes are disallowed as usual.
In case your language does not allow functions to be passed as arguments, you need to implement this capability, and add it into your answer. However the byte-count of this operation will not be added to the total.
This is code golf so the shortest code wins. Good luck!!!
Table
works here. Is your example supposed to beTable(f, {x, 0, 5})
? I also don't get the purpose ofx
at all, since it just applies the function to the range. \$\endgroup\$