Академический Документы
Профессиональный Документы
Культура Документы
Square Root 4.sqrt = 2 5.sqrt = 2.236067977 Integer Square Root 4.isqrt = 2 5.isqrt = 2 Log base 10 and Natural Log 5.ln = 1.609437912 5.log10 = 0.6989700043 Max & Min 3.min(7) = 3 3.max(7) = 7 Even & Odd 3.even? = False 3.odd? = True Prime 3.prime? = True 4.prime? = False There are some other mathematical functions available, including basic trigonometry functions as well as some basic calculus functions. Happy calculating.
Posted by Christian at 11:30 AM | Permalink | Comments (0) | TrackBacks (0)
I Object
Smallworld Magik is an object oriented language, so almost everything you deal with is an object. Some examples of objects:
Integers (1, 13, 2008) Floating point (1.0, 3.1415, 2e3, 0.57721) o Note: the e in 2e6 is 2x10^6, not 2*e^6 where e = Natural Logarithmic Base o Note: 0.57721 works while .57721 will fail Character( %c, %l, %t) o Note the % denotes a character object
There are a few special objects as well. The keywords _unset, _true, _false, and _maybe are all objects and can be treated as such. The string Its the end of the world as we know it. is actually an array of characters. Knowing this, we can treat it as a collection without having to parse the string. As objects, they can be compared with each other. This code returns the string in alphabetical order, with weight given to punctuation, and capitalization first. This is accomplished through Magiks _cf comparison which evaluates characters based on their ASCII values. Magik2> "".new_appending(_scatter sorted_collection.new_from("It's the end of the world as we know it.").as_simple_vector()) $ " '.Iaddeeeefhhiklnnooorssttttwww" Or reversed: Magik2> "".new_appending(_scatter sorted_collection.new_from("It's the end of the world as we know it.").as_simple_vector()).reversed() $ "wwwttttssrooonnlkihhfeeeeddaI.' " We create an empty string and append to it the result of the as_simple_vector method on the sorted collection we created from our string. Simplicity defined. In the next example we create a string object and assign it to tmp1. We then assign the tmp1 to tmp2. In this case tmp1 and tmp2 dont just have the same value, they are the same object.
Magik2> tmp1 << "Hello" $ "Hello" Magik2> tmp2 << tmp1 $ "Hello" This would not be true in the next case. Both tmp1 and tmp2 are assigned the same value, but they are not the same object. Magik2> tmp1 << "Hello" $ "Hello" Magik2> tmp2 << "Hello" $ "Hello" Final notes on the subject of objects:
Variable assignment doesnt copy, it references. Variable assignment is NOT strongly typed so you dont need to declare it first, just assign a variable to an object, be it a number, a string, an array, a collection, or even a running application.
Variable Assignment
Assigning variables in Smallworld Magik can be accomplished with a simple construct known as the left chevron (<<). A simple variable assignment will look like this: a << 7 This expression states that the variable a becomes 10. More complicated expressions can also be expressed in this way. b << 7 * a + 7 or c << (b-3).squared Variables may be named almost anything you want, with the following constraints:
Start with a-z, or ! Contain a-z, 0-9, _, ?, ! Unlimited length Case independent (sorry camelCase junkies)
Variables may also be assigned in serial or parallel. A series assignment might look like this: a << b << c << 7 This is the same as a << (b << (c << 1)) three << (two << (one << 1) +1) +1 Parallel variable assignment might look like this: (a,b,c) << (1,2,3) (s,c) << theta.sincos() <--this works because sincos() returns two values, sin and cos There is a somewhat classic programmer problem for swapping two variables without using a temp variable. One of the many solutions being as follows:
a << a+b b << a-b a << a-b Smallworld Magik has a more elegant solution however, using a swapping assignment. (a,b) << (b,a) Variable can be assigned inside other structures as well. A common use for this is to assign a variable during an if-then evaluation. _if (z<< (x.squared + y.squared).sqrt) <= minimum_distance _then write(The value ,z, is too small!) _endif There are other variations to the left chevron that have their uses. a +<< 1 will increment a by 1. a -<< 1 will de-increment a by 1. a *<< 2 will multiply a by 2. a /<< 2 will divide a by 2. A seldom used, but kind of interesting one is known as the boot assignment. a << b ^<< 10 This is the same as saying a << b followed by b << 10. Essentially b becomes 10 and returns its original value which is in turn assigned to a. One final note on Smallworld Magik variables is that developers tend to use longer names to describe most variables separated by an underscore (_) with the exception being when it is an iterated variable inside of a loop. The variable e is used by many to indicate an element inside of a loop, while more permanent element references would be named an_element.
Posted by Christian at 03:13 PM | Permalink | Comments (0) | TrackBacks (0)
typing magik-mode will enable Magik mode in the buffer. This allows Magik specific functions like auto complete, keyword recognition, and word coloring to work. To write our first program we will use one of the simplest constructs available, a procedure or a proc. _global hi << _proc() write(Hello World) _endproc We declared a global variable named hi, which is not case sensitive so all you java educated camelCase programmers take note. The << is Magiks assignment indicator and is read becomes by most people. _proc and _endproc are keywords and the write statement between them should look familiar. Keywords are identified by the underscore at the beginning. If you enabled magikmode earlier, pressing F12 should color them in a unique (usually blue) color. The program needs to be compiled so that the command line will recognize it. To do this, press F2 then b which is short for buffer. There are other options for compiling less than the full buffer such as F2 then m for compiling the method. If your code can be fully parsed without errors the *gis* buffer should now show the following: Magik2> Loading C:\Temp\Tua030992780 --- line 0 Defining variable hi --- line 7 True 0 Magik2> Now type in your program name at the command prompt and hopefully you will see the expected output. Magik2> hi() $ Hello World Magik2> Congratulations on writing your first Magik procedure. If you did it correctly you just declared to the world, or at least the command line, that you are now a member of the rare breed of programmers that can write Smallworld Magik under the Skills heading on their rsums.
Posted by Christian at 12:37 PM | Permalink | Comments (1) | TrackBacks (0)