OO vs FP | imperative vs FP [closed] - functional-programming

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I often come across comparisons between object-oriented and functional programming, and between imperative and functional programming. "Object-oriented" and "imperative" are two distinct terms which, in my understanding, have nothing to do with each other. Still, they are both compared to functional programming. What is the relationship between object-oriented and imperative programming that motivates such comparisons?

The reason is simply, that mainstream OO-programming (i.e. Java, C#) is a sub-category of imperative programming.
Here is a rough chart:
progamming
/ \
declarative imperative
/ \ / \
functional \ OO procedural
logic
(prolog)
In declarative programming, you say what it is you want to compute. Say we want to compute the length of a linked list:
The length of an empty list is 0
The length of a nonempty list is one more than the length of the tail of that list.
In imperative programming, you basically manipulate memory in a certain order:
int length = 0; // put starting value in memory
while (!list.isEmpty()) {
length++; // update memory
list = list.next(); // update list pointer for next iteration
}
// result is in memory location associated with length
So, the proper comparisions would be:
imperative vs. declarative
OO vs functional

Related

A summary of different symbols in programming [closed]

Closed. This question is not about programming or software development. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed yesterday.
Improve this question
I'm preparing for competitive programming, I keep running into different symbols in problems that I'm expected to know.
(example image of such symbols)
Can someone make a summary of all of these different symbols and explain what they mean? Thanks in advance.
The big Sigma stands for "sum". The part you've circled in red means:
a_l + a_(l+1) + ... + a_(r-1)
It's a sum of r-l elements. And then you're dividing this sum by r-l, so you end up with the average of those r-l elements.
A sum like this can usually be implemented with a for-loop:
s = 0;
for (i = l; i <= r-1; i++)
{
s += a[i];
}
But note that in many cases the sum can be computed directly without a slow loop. For instance if the a_i follow an arithmetic progression or geometric progression then you can calculate the sum directly without actually summing all the terms one by one.
The arrow presumably means "tends to", giving information about a limit of this quantity. However, normally we never use this arrow alone; there should be some context to tell which limit we're talking about. For instance, "3x+4 tends to 13", alone, makes no sense. What would make sense would be "3x+4 tends to 13 when x tends to 3".

What would be a best practice to program mathematical calculations? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm programming some financial software needing to program not very complicated mathematical formulas.
After writing the code, it is not readable anymore - i.e - you can't easily discern what was the original formula.
What would be a good way to program mathematical formulas so they could be easily read later on?
For example, programming a calculation for a loan with a fixed interest rate:
(TotalValue*monthlyInterest*Math.pow((1+monthlyInterest),totalPayments))/(Math.pow((1+monthlyInterest),totalPayments) - 1)
Though using meaningful variables, the formula is not readable. But if you will look at this formula written in a classical mathematical notation on a page - you will easily know what's going on (really basic math).
How would you even take this formula and write it in a readable way.
Clarification
I'm not talking about any specific language. This should be the same for any high-level language.
The example uses Javascript.
You could also add local variables that are abbreviations of the long name. As they are local, their definition will be right above the formula. This could look like
TV = TotalValue;
mi = monthlyInterest;
N = totalPayments;
MR = ( TV*mi*Math.pow( 1+mi, N ) ) / ( Math.pow( 1+mi, N ) - 1)
MonthlyRate = MR;
where you then see that you can simplify the formula to
MR = (TV*mi) / ( 1 - Math.pow( 1+mi , -N) - 1)
As far as i know, it is not possible to convert Excel formulas to equations
But for clarity, you could recreate the Equation in the Equation Editor and put it next to the cell. This way it is immediately obvious what you are doing.
Another way would be to create the Mathematical Equation and add a macro. This way you could calculate your values in VBA.

Using R, what are the equivalents of methods in other languages [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
In languages such as Java and Eiffel, there is a divide between functions (returning a value) and methods, groups of transformative statements. So a method would be something like:
public main blah NAMEOFMETHOD(x) {
STATEMENT/COMMAND
STATEMENT/COMMAND
STATEMENT/COMMAND
}
R has functions as basic to the language, but there is no obvious way of making a method (I think). Do I just put groups of commands in a class? (I programmed years ago with Eiffel and Java, but am now using R for statistics and am extending my work with some programming - or is it coding you call it now?!)
Old guy relearning. Please be gentle.
R is a functional programming language. It's important to understand that there is technically no real difference between a function (with possible side-effects such as generating a plot) and a method.
The only difference is the absence of a "return value".
A Java method with signature
void doSomething()
(wich misses a return) can be implemented in R as
doSomething <- function(){
# Do something here
return(invisible(NULL))
}
where NULL is the return value ("nothing") and invisible instructs R not to automatically print this result to the console.
return(invisible(NULL)) is equivalent to the shorter statement return(invisible()) and, if it's the last line of code in your function, as it should be, also equivalent to invisible() because R auto-returns the result of the last computation in the function body.

Why is code writen in functional programming language shorter than code writen in other programming paradigm languages? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Many people say that code writen in functional programming language is shorter than code writen in other programming paradigm languages.But why?
One big reason is the greater use of higher-order functions. A simple example is map. The following are equivalent:
# Procedural psuedocode
result = []
for value in other_list:
add somefunc(value) to result
# Functional pseudocode
let result = map(somefunc, other_list)
The higher-order function map abstracts away the code that iterates over the first list and sequentially adds the function return value to the new list. Other higher-order functions can be used to implement other techniques that are commonly spelled out (whether by necessity or not) in more detail in other paradigms.
Another less commonly seen higher-order function is composition. In many languages, you write code like
def new_function(x):
return func2(func1(x))
value = new_function(5)
or
lambda x: func2(func1(x))
In a functional style, you would write (or have) a compose function to handle the details, instead of having to write each composed function individually.
def compose(f, g):
def _(x):
return f(g(x))
value = compose(func2, func1)(x)
Some languages, like Haskell, even provide a composition operator:
value = (func2 . func1)(x)

examples to compare tradtional math notations vs APL/J notations [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I am reading a review to compare Mathematica to APL/J. One question raised in the article seems very interesting to me:
Is Mathematica really the way to go to express our creative thoughts –
viz back to a 17th century notation designed for parchment instead of
forward to a twentieth-century one designed for computers?
Can one share examples of Iverson's notation vs traditional math notation to demonstrate the edge of APL/J on expressing and solving math problems? This would be greatly helpful for new comers.
One example: Alternating series.
Alternating sum is very common in mathematics. But it is cumbersome to put the sign before each term:
in APL and J, because of the order of operations, it is
-/a
I recommend reading Iverson's paper Notation as a Tool of Thought, kindly provided by the J folks. It deals precisely with this issue.
In it you'll find many Math proofs derived using APL instead of the classical notation, along with accompanying commentary. Here's a redacted example, proving Gauss's formula for the arithmetic series:
+/⍳n
+/⌽⍳n ⍝ as + is associative and commutative
((+/⍳n)+(+/⌽⍳n))÷2 ⍝ as x=(x+x)÷2
(+/(⍳n)+(⌽⍳n))÷2 ⍝ as + is associative and commutative
(+/(n/n+1))÷2 ⍝ summing each respective x∊⍳n and y∊⌽⍳n, y=n+1-x → (x+y)=n+1
(n×n+1)÷2 ⍝ per definition of × (times)
Other articles by Iverson, Hui and friends are also illuminating. Again, the J folks provide a notable library.

Resources