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 fricas (1) -> l := [1,
Type: List(PositiveInteger?)
fricas map(x +-> 2^x,
Type: List(PositiveInteger?)
Note: fricas p := 3*x^2*y + 2*x +5*y + 7
Type: Polynomial(Integer)
fricas map(x +-> (odd?(x) => x + 1; x - 1),
Type: Polynomial(Integer)
|