login  home  contents  what's new  discussion  bug reports     help  links  subscribe  changes  refresh  edit

Edit detail for FunctionalProgramming revision 2 of 2

1 2
Editor: test1
Time: 2016/08/30 18:55:29 GMT+0
Note:

added:

FriCAS language is imperatvie, but it can take best parts from functional
style.  In particular, in FriCAS functions are first class, meaning that
they can be passed as argument to over functions.  One idiom which
is frequently used is apply function to all elements of an aggregate.
This is done by 'map', for example:
\begin{axiom}
l := [1, 2, 3]
map(x +-> 2^x, l)
\end{axiom}
Note: '+->' operator produces unnamed function (closure).  FriCAS
aggregates of category HomogeneousAggregate export 'map'.  Also
several other domains provide 'map'.  Below we apply 'map'
to polynomial (the effect is to apply given function to
each coefficient of the polynomial):
\begin{axiom}
p := 3*x^2*y + 2*x +5*y + 7
map(x +-> (odd?(x) => x + 1; x - 1), p)
\end{axiom}



In pure functional programing there are no variables, there are only values. Values are immutable and logically exist "forever" from creation. Program creates new values from old ones, typically using functions. Traditional loops are of no use in pure functional programming because repeating computation will produce the same value. Instead, recursion can produce effects equivalent to traditional loops. There are practical problems with pure functional programming. First, since values exists forever eventually they will exhaust all available memory. This is resolved by noting that some values are unreachable: there is no way to reference them in further computation. Consequently memory used by unreachable values can be freed and reused for new values. This is called garbage collection. Without garbage collection functional programming would be of limited use. Second, interesting programs interact with environment, for example draw graphs on the screen. Such activities mean that program is no longer pure functional, but can be included in way compatible with functional style. Third, some computations, like graph traversal became complicated without ability to change values. So pure functional programming is frequently regarded as doing things with unnecessary restrictions. However, to deal with restrictions functional programming developed new style which in many cases leads to short and efficient programs.

FriCAS language is imperatvie, but it can take best parts from functional style. In particular, in FriCAS functions are first class, meaning that they can be passed as argument to over functions. One idiom which is frequently used is apply function to all elements of an aggregate. This is done by map, for example:

fricas
(1) -> l := [1, 2, 3]

\label{eq1}\left[ 1, \: 2, \: 3 \right](1)
Type: List(PositiveInteger?)
fricas
map(x +-> 2^x, l)

\label{eq2}\left[ 2, \: 4, \: 8 \right](2)
Type: List(PositiveInteger?)

Note: +-> operator produces unnamed function (closure). FriCAS aggregates of category HomogeneousAggregate? export map. Also several other domains provide map. Below we apply map to polynomial (the effect is to apply given function to each coefficient of the polynomial):

fricas
p := 3*x^2*y + 2*x +5*y + 7

\label{eq3}{{\left({3 \ {{x}^{2}}}+ 5 \right)}\  y}+{2 \  x}+ 7(3)
Type: Polynomial(Integer)
fricas
map(x +-> (odd?(x) => x + 1; x - 1), p)

\label{eq4}{{\left({4 \ {{x}^{2}}}+ 6 \right)}\  y}+ x + 8(4)
Type: Polynomial(Integer)