View Full Version : JavaScript and Algebra
bacterozoid
12-06-2002, 10:49 PM
Well, with my limited knowledge of JavaScript, I find a few similarities between it and Algebra. Some algebreic words and stuff seem the same. Like now in Algebra II, we are studying functions, ex: f(4) "f of four." I don't know, maybe just a coincidence or something, but maybe when whatshisname put it together he was thinking of algebra?
mordred
12-06-2002, 11:08 PM
I think that's true for most programming languages, not solely for JavaScript. And IIRC the name of a very old programming lanuage, FORTRAN, was an abbreviation that translated to english as "Formula Translator"... clearly an indication how tightly programming and math can be interwoven sometimes.
From my point of view, there are quite a lot of concepts that you find both in mathematic fields like algebra and in programming languages. Functions, variables, vectors, yadda yadda... the really interesting thing is how human readable the programming language actually is. While math equations are very exact and carry a lot of information in them, they are hard to understand for the uninitiated. A programming language should provide a workable tradeoff between both formality and readability. IMHO, the language which does this badly is (guess what) Perl, but I do admit that in the end it's the programmer himself who has to take care of readabliity.
Just my 0.02 Euro
Originally posted by bacterozoid
Like now in Algebra II, we are studying functions, ex: f(4) "f of four."
:eek: Then what did you do in Algebra I? :confused:
bacterozoid
12-06-2002, 11:56 PM
Well, in Algebra I, we learned really basic stuff: Variables on two sides, two equation solving, uh...a lot of equation stuff really...some graphing and number line...and like age problems....I could get out all my notes from last year, lol.
RadarBob
12-07-2002, 12:17 AM
The idea of the function is the same. Let's take the SINE function. Something goes in - a number with a certain value range; something comes out - the sine of that number. A function idealy does one thing and one thing only. Thus the output is only one thing also. In other words a sine function calculates the sine, NOT the sine and tangent and square root; just the sine.
What actually happens inside the function is a mystery to the users of that function. This is true in math as well as computer programming. In fact it's a desirable trait of programming. Programming functions should be written so folks who use it don't have to know how it works; nor can they change it.
The concepts of the function are indeed simple. But that simplicity belies the profound impact good function writing has in computer programming. The ideas may seem too simplistic and obvious, but you'd be suprised what programmers write and try to pass off as useful functions. By carefully sticking to the fundamental princples of function making, our computer programs tend to be better all around.
Originally posted by RadarBob
What actually happens inside the function is a mystery to the users of that function. This is true in math
All functions in math have to be well-defined. No mysteries, even in the most complex of functions.
bacterozoid
12-07-2002, 12:27 AM
Dumdeda, discussion. I agree with jkd...I don't see how a function could be undefined or mysterious in any way and still be manipulated and the sort.
Originally posted by bacterozoid
Dumdeda, discussion. I agree with jkd...I don't see how a function could be undefined or mysterious in any way and still be manipulated and the sort.
I only said in math. In programming languages you can have ambiguous functions, which may be simply be obfusticated beyond recognition.
I think RadarBob was getting at that functions should be encapsulated and not require intimate user-knowledge to use.
It is nice to know that sin(x) expands to
x - x^3 / 3! + x^5 / 5! - x^7 / 7! + ...
But do we need to know that to say sin(pi) = 0
? No, not at all.
Or do we need to know that sin(theta) is the ratio of the y-coordinate of a unit circle at that angle with the radius?
You should (as that is how sin is defined!!!), but it isn't necessary.
whammy
12-07-2002, 12:43 AM
The basics are still true though... a function returns a value. That's all it really boils down to, isn't it? :D
And yeah, algebra has a very strong correlation with programming languages, since the use of variables is a necessity.
I'm no math major, but most of the programming I deal with uses very basic algebra.
What level of math you're using (regarding programming) depends on the programming language you're using, and what the program is required to do. Some languages cannot handle extremely advanced math. (And I can't either, lol) :)
If you're working for NASA, writing programs to ensure the next space probe gets where it's supposed to go, you'd better know some SERIOUS math!!!
bacterozoid
12-07-2002, 12:43 AM
Ambiguous functions...but when you have a function, aren't they generally only interpreted one way?
Originally posted by bacterozoid
Ambiguous functions...but when you have a function, aren't they generally only interpreted one way?
In a programming language, f(5) may not always equal X. While in math, it must - for it to be a function.
int global = 1;
int f(int x) {
return x + (global++);
}
// f(5) = 6;
// f(5) = 7;
// f(5) = 8;
That be bad. References can even make things worse:
int test = 5;
int mystery(int a &, int b) {
return (a += b);
}
// mystery(test, test) = 10
// test = 10
Originally posted by whammy
And yeah, algebra has a very strong correlation with programming languages, since the use of variables is a necessity.
Variables aren't actually needed at all when dealing with a functional language or using pure functional concepts in a programming language.
whammy
12-07-2002, 12:53 AM
When you pass something to a function (at least regarding programming) doesn't it automatically become a variable? :D
This is getting way too semantic for me, since I really despise math. :)
bacterozoid
12-07-2002, 12:55 AM
Hmm, it would, wouldn't it? At least in a sense. A variable is basically something that could change, and anything that can be passed through a function, is in essence a variable, since it can be edited.
whammy
12-07-2002, 12:56 AM
That's what a function does, right? Like I said, at least regarding programming... as for the math side, I'm way outta my league lol :)
Originally posted by bacterozoid
anything that can be passed through a function, is in essence a variable, since it can be edited.
No. Look at *any* functional language, such as Lisp, Scheme, or Haskell.
The introduction to computer science course at my school does Scheme, but does not cover how to define variables because they simply aren't needed. And they do binary search trees, sorting algorithms, etc - all without this ability.
whammy
12-07-2002, 03:24 AM
I guess that explains why you are a super moderator... ;)
To me, a Lisp is when someone has a particular speech impediment.
A Scheme is another word for a plan...
And Haskell is just someone I don't know's last name...
:D
bacterozoid
12-07-2002, 03:25 AM
Lol, whammy. You make a point, I have never heard of those either.
LISP is one of the oldest computer languages still being used, after Fortran.
From what I remember reading, Yahoo! stores was actually written using LISP, as well as several AI programs.
http://www.lisp.org/table/contents.htm
Scheme is really just a better LISP with some features and improvements.
http://www.schemers.org/
And Haskell is the only functional language I have any sizable amount of experience in (though for ACSL competitions we have to evaluate LISP programs). I thought it was quite nifty, as it was my first functional language, but Scheme is probably a better one to learn. (But function currying is so cool!!!)
http://www.haskell.org/
jkd, what school is that? Do you have a bachelor's in computer theory and software? I'm working on a major in computer science myself, and it's a white-hair-generator :eek: I personally really enjoyed the analysis of algorithms, including binary search and the various sorting algorithms, etc, but the order of magnitude principles got mathematically carried away when it came to approximations.
What's your take?
krycek
12-07-2002, 01:25 PM
...oh dear, Haskell... :eek:
I absolutely hate languages like Haskell. Sure I have limited experience with them, however this is of my own choosing because, to quote Cartman; "I hate them so much, so very, very much!"
Please no-one argue about how they are necessary, because I am not disputing that (I can get by just fine without them, but obviously they must be useful to someone, lol) all I am saying is that I hate them.
Now the interesting thing is, in Haskell a quicksort can be defined in 3 lines of code, for example. There are definitely advantages to using functional languages, however I find them rather... dare I say, 'messy' compared to more 'normal' languages :D
Oh and as for the whole math thing, well of course computer languages are going to be like equations... let's just take everything back to the binary:
In machine code there are only 1's and 0's. Next step up, assembler... very simple tokens that describe a set function, just as INC, to increment a value by 1. Assembler has, very simply, commands for storing data in registers, simple manipulation, jumping to different places.
...so what has that got to do with math? Well it's what you make out of it. GOTO statements went out with the dinosaurs... instead, we use proper functional statements like WHILE etc. and also proper named functions, so we don't have to worry about the framework underneath (which still relies on GOTO principles).
So with a higher level language such as BASIC, PASCAL, C etc. we can use functions, variables, etc. which brings it very much into line with math. Now, in math, if you do 2 + 2 you KNOW it will be 4. Why? Why not still 2... or 5... or 76? Because those are the basic RULES for math. Just like a low-level language builds up the basic rules for programming, so that a higher-level language can use it's equations etc. without having to worry about exactly where the bits and bytes are going.
I find it hard to imagine a situation where a programming language would have NOTHING to do with math... in fact, surely that's impossible? After all, the only thing computers do is perform calculations. Of course, they also store data, so that they can perform calculations of some sort on it. But as the ony thing they do is math, then the way we interface with them is also going to be math based.
Now, I happen to agree to some extent with RadarBob about what he said about the user not having to know what a function does. That is true in computing AND math. Take the sine example jkd used. How many people actually KNOW what sine IS? Not many, because they don't have to. We have calculators (computers :)) that do it for us. So, we just write x = sin(y) and then work it out on our calculators... if you had to do it in your head, you couldn't, unless you knew the 'code' that makes up the sine function.
So, yes, functions are encapsulated. But they are also undefined, at the same time as being well-defined. Both definitions are correct, depending on how you look at them.
RadarBob apparently meant undefined in the way of being self-contained (encapsulated) and a mystery to the USER because the user does not need to know what is going on. However, to the COMPUTER the function has to be well-defined, just like in math. Now, in math, a function such as sine is well-defined because all mathematicians agree to use a common standard. YOu don't have different languages in math, do you?
Well, yes you do, of a sort. Math itself is universal, however there are different forms of notation (reverse Polish, for example). And, people can make their own mathematical functions, which are not a recognised standard.
Which brings us nicely back to computing, which has lots of different languages, usually because each is developed for a different purpose. Because computers deal with actual data, and peform calculations on that data, there are ALWAYS going to be variables, no matter what language you use (sorry jkd!). Using variables is a fundamental concept of computing (and math!) and, even in a functional language such as Haskell, you still use variables.
Sure, you can write a quicksort function in Haskell in 3 lines of code. You might look at it, and say, that's not using any variables (ignoring the data it is sorting, for now). But it is! The function itself is a variable, because it gets and returns a value that is not fixed, even if that is through another function (it's the same in math).
Well there we go, that's more than I intended to write, but I enjoy these little discussions :P
I don't know if jkd has first-hand experience of these languages actually in use (by that, I mean did jkd learn 'legacy' languages by using them when they were in everyday use, like I did, or at university as part of a course) however it is nice to have other people that know something about this :) I know not everyone is a computer science major (I never went to uni, but it hasn't harmed me... I have lectured at some since) but to people who are new to all this, and just starting off on javascript, I feel that there is so much you have missed... although you may not see it that way, because programming assembler on my old C64 10 years ago was horrendous! :eek: I would never go back!
So... on with the discussion...! :D
::] krycek [::
Originally posted by Beck
jkd, what school is that? Do you have a bachelor's in computer theory and software?
The Maine School of Science and Mathematics (http://www.mssm.org).
As you can see, that is a high school... so I'm still working on my high graduate "degree" ;).
And krycek, I know it sounds ridiculous, and I thought so too when I first heard it, but you don't need variables to write programs. Obviously you need to store stuff in memory, but I'm talking about the concept of variables - assigning intermediate values to memory addresses in the middle of a program.
If you don't believe me use Scheme for about a week. Like I said, the Introduction to Computer Science course here doesn't even know how to define variables in scheme, and they are able to create any data structure or algorithm we can in my AP Computer Science class (which uses C++).
RadarBob
12-08-2002, 05:02 AM
And Haskell is just someone I don't know's last name...
That would be "Eddie". Wally Cleaver's best friend.
RadarBob
12-08-2002, 05:05 AM
Interesting, jkd. So how does one reference and use the data / data structures, without calling it something - which would be a variable. Oh, OK. It's not "variable." It a constant.
Don't be technical and think of functions as variables. You can't store values to functions, while you can to variables.
krycek
12-08-2002, 06:33 PM
jkd... maybe we have different definitions of variables :D
I am simply saying that variables will always exist in programming, no matter what language you use, for one simple reason: the program only exists to FIND OUT one or more variables.
Think about it:
If, using Scheme, or Haskell, etc. you write a program - what would be the point of writing the program if the result never changed? You may as well not bother. However, it is because the result is unknown, or 'variable', that the program needs to be written.
Now, not once did I disagree with you about the specific assignment of variables, because that is not how a purely functional language such as Haskell is intended to operate. However, I did point out that you could say the functions themselves are variables.
Personally I vastly prefer using languages where I can define proper variables, however different styles of language can be greatly interesting to study, and maybe shed light on to different approaches to problems, etc.
Part of the point of Haskell-type languages is to be far more generic than most people like to code - I like to call it fuzzy logic (don't pick on that please, I know it isn't the 'true' definition :)) because the logic is not quite as 'fixed' as in a program that uses defined variables. Now, that is not to say that it isn't structured!
True, a function cannot 'store' data in the same way that a variables stores data, however there IS data moving through the function in both cases, whether held by variables or just the function itself.
Hence what I said about there always being variables in computer programs :)
Feel free to disagree, because I am enjoying this discussion! :D
::] krycek [::
Originally posted by krycek
If, using Scheme, or Haskell, etc. you write a program - what would be the point of writing the program if the result never changed? You may as well not bother. However, it is because the result is unknown, or 'variable', that the program needs to be written.
You have return values and variables - that is all that is needed.
Input from the user or a file can be a return value. There does not need to be an intermediate step of assigning it to a variable.
You are taking the concept of a variable to be too semantic. A variable is simply an identifier to which values are assigned during some intermediate step. Never once do you need to do this - if you wrote a program in a functional manner, you basically just have a queue of functions working on the return value of the one that just executed.
If you don't believe me, ask any computer science teacher who knows Scheme if you once need to ever use the (define ) statement.
krycek
12-08-2002, 10:08 PM
hmmm, not sure why you still seem to be disagreeing with me... I have read your last post very carefully more than once, and you seem to be saying exactly the same as what I said...?
Like I said, maybe we are using different definitions of variables, but I am simply following on from what was said earlier in the discussion by others, e.g. bacterozoid.
For you to say that a variable is a value that is expressly defined in a function or section of code in order to store a value is totally correct. However, that is, like you also said, a very strict definition. Perhaps I should have said 'values' instead of 'variables' before, however the difference is not really important. I am sure that you know that in some languages it is possible to set up a variable to actually BE a function, and so the line between variable and function, and simply 'value' can get a little blurred. I was not aware that in this general discussion we were supposed to stick to strict semantics; indeed I thought that the whole idea was a generic discussion ;)
Read your last post, and compare it carefully to my previous two posts, and you will hopefully see that what you said is indeed what I have been jabbering on about!
I am glad that you used the word 'identifier' in your last post, because I think that is part of the problem. To be accurate, functional languages NEVER have to use identifiers, however to say they do not have to use variables is misleading. A variable is essentially a value, except most people tend to talk strictly about the definition, restricting it to being a value identifier, so to speak (which is how most languages behave).
...oh and by the way, not only have I used Scheme, I have also used Haskell and a plethora of other languages, not all of them nice, and not many of them friendly :D
::] krycek [::
krycek
12-08-2002, 10:13 PM
oh, also it just occurred to me that this is rather funny:
Originally posted by jkd:
If you don't believe me, ask any computer science teacher who knows Scheme if you once need to ever use the (define ) statement.
...not only do I know Scheme (admittedly only a working knowledge) but I am also a computer science teacher (not at present, but I have spent more than three years teaching on and off - 90% of which has been at graduate or post-gradutate level - with my last stint ending last May)
...so maybe I know something about what I am talking about... not that we seem to really be disagreeing very much, lol! :D
::] krycek [::
RadarBob
12-09-2002, 01:26 PM
jkd;
Maybe I just insist on thinking too conventionally. Or maybe as krycek suggests we're kinda in the same ballpark - just confused on semantics.
Your term "identifier" is interesting. Some languages don't require you to explicitly define "variables" before you use them. They are defined on the fly, the moment it's used for the first time in some statement (which is a VERY bad thing for coding in real life).
What I have a hard time letting go of is the fact that, somewhere, somehow, down in the bowels of the computer; memory must be allocated so that values can be stored and modified. To access those memory locations you need some kind of reference. In higher level languages we call those references "variables." "Identifiers" is a good word here too. I view these as synonymous. Even in assembler language one assigns mnemonic names to memory locations for referencing.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.