Take the 2-minute tour ×

Do any general purpose languages support, for example:

n + 2 = 3;

To ensure that possibly among other things that 'n' will now read as 1, or in other cases as a somewhat but not entirly uncertain value.

Beyond this are there any that can suport this concept for algorythmic stuff in general, for example a mixture of strings and numbers with concepts such as concatenate, substring, numerical bitwise rotate etc... not because somone hard coded it into the languege but because the languege understands about using it's knowledge of how things work (your C++ style classes, your classless scripting language like objects, functions that exist etc...) and using this knowledge to rearrange things, as is common in algebra.

share|improve this question
This is a bad feature that shouldn't be in languages. Haskell has n+k patterns but nobody likes them. –  Pubby Dec 21 '11 at 23:04
read this maby it would help: stackoverflow.com/questions/580356/… –  piotrekkr Dec 21 '11 at 23:05
Pubby, are n + k patterns only to do with addition or do they allow any such relationship. The first answer appears to say that only Prolog can do this sort of thing. –  alan2here Dec 21 '11 at 23:31
@Pubby: Haskell doesn't support n+k patterns anymore (as of 2010). –  amindfv Dec 1 '12 at 22:07
add comment

4 Answers

I guess only Prolog can do that kind of stuff (counting only well known programming languages).

share|improve this answer
Can you point me towards anything showing simulation of abstraction (for example data structures) in prolog (although ofc a question must resolve to a bool) –  alan2here Dec 21 '11 at 23:33
Logical programming languages in general can do this, I believe (Prolog being a logical programming language). Though some (a minority) may disagree, logical programming languages are not largely considered 'general purpose'. The best ways of performing some (even simple) calculations in logical languages require combinatorially implausible sub-calculations due to backtracking and so forth. That is not to say that they do not have their place, but there are instances where they are relatively inefficient. –  Zéychin Dec 22 '11 at 23:30
Thanks Zéychin. –  alan2here Jan 5 '12 at 18:52
add comment

Certainly: Algol 60 purports to support this particular case if I remember rightly (not sure .. its been a while :) However only the simple linear case, which isn't useful since it is easy enough to subtract the constant from both sides in your head.

However many modern languages pose to the compiler very much harder problems to solve in terms of their type systems. Many allow posing of typing issues which have a solution but which the compiler cannot solve, this is particular true with compilers that do type inference.

share|improve this answer
add comment

Haskell had so-called "n-plus-k" patterns, where for example you could write the factorial function as:

fac 0     =  1
fac (n+1) = (n+1) * fac n

This is now viewed as A Bad Idea (some reasons here), and was removed from the language specification (deprecated in Haskell98 and removed in Haskell2010). But! There is a more sophisticated, more general form being worked on for future versions of Haskell:

View Patterns -- see the section "N+K Patterns"

share|improve this answer
add comment

General purpose languages are called general purpose for a reason. You don't solve math problems with them.

None of GP languages I know of allow expressions on the left side of assignment. Erlang has pattern matching, but that's an entirely different thing.

share|improve this answer
The last half of my question is about just this. I'm saying algebraic like reasoning regarding arranging things, although somewhat behind the scenes in the given example could and perhaps has been done with anything, not just the sorts of types used in maths. For example objects in OO programming with the operations users, not just the languege designer have specifed. –  alan2here Dec 21 '11 at 23:36
I'm afraid I don't understand what you just said. Sorry, I'm serious. Also, the first half of my answer is just about this: you don't use algebraic approach when programming. Doing so (whether the language is suited for that or not) results in unbelievable bloat and complexity. n + x = y is a human problem. What computer needs to know is that n = y - x. If you know that, why not code it that way. If you don't, first try to understand what you want the computer to do instead of trying to make a computer solve human problems (it usually fails pretty hard). –  a sad dude Dec 21 '11 at 23:47
Are you aware of concepts such declarative programming? en.wikipedia.org/wiki/Declarative_programming "Many languages applying this style attempt to minimize or eliminate side effects by describing what the program should accomplish, rather than describing how to go about accomplishing it." Many functional languages use this approach. –  alan2here Dec 22 '11 at 1:21
I'm sure you can understand that num1.add(num2) and num1 + num2 could be equivalent but that there are many other operations that are not mathematical in the same way such as string1.substring(4, 2) or node.add_parent(p) but that the same principles can apply in regards to rearranging. –  alan2here Dec 22 '11 at 1:21
"n + x = y" is not "what a program should accomplish", but "what a problem the program must solve". Not exactly the same thing. –  a sad dude Dec 22 '11 at 2:06
show 3 more comments

Your Answer


