1

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.

alan2here
  • 3,223
  • 6
  • 37
  • 62
  • 4
    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: http://stackoverflow.com/questions/580356/abstract-algebra-and-programming – 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 01 '12 at 22:07

4 Answers4

4

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

Cartesius00
  • 23,584
  • 43
  • 124
  • 195
  • 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
1

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"

amindfv
  • 8,438
  • 5
  • 36
  • 58
1

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.

Yttrill
  • 4,725
  • 1
  • 20
  • 29
0

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.

a sad dude
  • 2,775
  • 17
  • 20
  • 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
  • 1
    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 01: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 01: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 02:06
  • But ok, let's try it. The construct you propose is basically an equation and equations have problems. The most obvious one is having multiple solutions (or lack thereof). Let's take your proposed example: string1.substring(4, 2) = "XY". You might want this to mean "set this substring to XY". However, what it means instead is "string1 equals any string that has this substring". Now what? Unless you believe in magic, the only way to achieve the desired result is to explicitly define "assignment to substring()", which is no better than explicitly defining another function, so why bother? – a sad dude Dec 22 '11 at 02:07
  • And if you really meant to get an [infinite] set of solutions, there are predicates and loops for that. – a sad dude Dec 22 '11 at 02:10
  • Okay, reread your question, you really meant that. Lawl. Okay, here's the deal: propose a real-world problem involving some objects with some methods, and a step by step solution for it (as this hypothetical machine should do). I'll be first to run around and praise you. – a sad dude Dec 22 '11 at 02:29