at:tutorial:basic
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
at:tutorial:basic [2009/06/05 15:32] – added tvcutsem | at:tutorial:basic [2020/02/09 21:53] – elisag | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | |||
====== Functional and Imperative Programming ====== | ====== Functional and Imperative Programming ====== | ||
Line 67: | Line 66: | ||
>>[1, [1, 2, [" | >>[1, [1, 2, [" | ||
</ | </ | ||
+ | |||
==== Table Splicing ==== | ==== Table Splicing ==== | ||
Line 85: | Line 85: | ||
>>[2, 3, 4] | >>[2, 3, 4] | ||
</ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ==== Multidimensional Tables ==== | ||
+ | |||
+ | As mentioned before, there is no special constructor for definition of multidimensional tables, a table entry can contain another table. In what follows we have a closer look to manipulations with multidimensional tables. | ||
+ | |||
+ | < | ||
+ | def a := [[1,0,0], [0,1,0], [0,0,1]]; | ||
+ | >> | ||
+ | >a[1][2] | ||
+ | >>0 | ||
+ | >a[1] | ||
+ | >>[1, 0, 0] | ||
+ | > | ||
+ | >>3 | ||
+ | >a | ||
+ | >> | ||
+ | </ | ||
+ | |||
+ | An implicit definition of the same table can be expressed as follows: | ||
+ | |||
+ | < | ||
+ | def i := 0; | ||
+ | def aux[3] {0}; | ||
+ | def b[3] { i := i + 1; aux := [0,0,0]; aux[i] :=1; aux}; | ||
+ | >> | ||
+ | </ | ||
+ | |||
+ | You can find later in this chapter a helper function for creating matrices | ||
+ | |||
+ | |||
===== Functions ===== | ===== Functions ===== | ||
Line 188: | Line 230: | ||
>> 21 | >> 21 | ||
</ | </ | ||
+ | |||
+ | |||
==== Optional Parameters ==== | ==== Optional Parameters ==== | ||
Line 201: | Line 245: | ||
</ | </ | ||
- | As is customary in languages with the above parameter passing semantics, AmbientTalk requires mandatory parameters to be defined //before// optional parameters, which should in turn be defined //before// a variable-argument parameter, if any. | + | As is customary in languages with the above optional arguments, AmbientTalk requires mandatory parameters to be defined //before// optional parameters, which should in turn be defined //before// a variable-argument parameter, if any. |
+ | |||
+ | Let us show how to use optional arguments to define an auxilary function that creates matrices: | ||
+ | |||
+ | < | ||
+ | def makeMatrix(n, | ||
+ | def [i,j] := [0,0]; | ||
+ | def makeCol(i, | ||
+ | def col[m] { j := j + 1; init(n,m) } | ||
+ | }; | ||
+ | def matrix[n] { i := i + 1; makeCol(i, | ||
+ | }; | ||
+ | def b := makeMatrix(2, | ||
+ | > | ||
+ | >> | ||
+ | > | ||
+ | >> false false false true | ||
+ | >def c := makeMatrix(3); | ||
+ | >> | ||
+ | >c[1] := [1,2,3] | ||
+ | >>[1, 2, 3] | ||
+ | >c | ||
+ | >> | ||
+ | </ | ||
===== Closures ===== | ===== Closures ===== | ||
Line 251: | Line 318: | ||
AmbientTalk borrows its block syntax from languages like Smalltalk and Self, where the role of the curly braces is played by square brackets, i.e. '' | AmbientTalk borrows its block syntax from languages like Smalltalk and Self, where the role of the curly braces is played by square brackets, i.e. '' | ||
</ | </ | ||
+ | |||
+ | |||
===== Keywords ===== | ===== Keywords ===== | ||
Line 282: | Line 351: | ||
</ | </ | ||
- | However, it is impossible | + | It is also possible |
+ | |||
+ | < | ||
+ | foo: | ||
+ | </ | ||
+ | |||
+ | <note important> | ||
+ | Be careful when nesting calls to keyworded functions: | ||
+ | </ | ||
===== Native Data Types ===== | ===== Native Data Types ===== | ||
The basic data types in AmbientTalk are numbers (i.e. integers), fractions (i.e. double precision floating point numbers), text (i.e. strings), tables (i.e. arrays) and booleans. In fact, instances of these data types are nothing but objects and as such, they respond to a variety of native methods. Objects will be the subject of the next chapter of the tutorial. This section explains the basic data types and includes some examples how to manipulate them. The complete list of methods can be found in the language reference. | The basic data types in AmbientTalk are numbers (i.e. integers), fractions (i.e. double precision floating point numbers), text (i.e. strings), tables (i.e. arrays) and booleans. In fact, instances of these data types are nothing but objects and as such, they respond to a variety of native methods. Objects will be the subject of the next chapter of the tutorial. This section explains the basic data types and includes some examples how to manipulate them. The complete list of methods can be found in the language reference. | ||
+ | |||
+ | |||
==== Numerical data types ==== | ==== Numerical data types ==== | ||
Line 319: | Line 398: | ||
Numbers also support some useful iterator methods such as: | Numbers also support some useful iterator methods such as: | ||
< | < | ||
- | >6.to: 0 step: 2 do: { |i| system.println(i) } | + | >1.to: 5 do: { |i| system.println(i)} |
+ | 1 | ||
+ | 2 | ||
+ | 3 | ||
+ | 4 | ||
+ | 5 | ||
+ | >1.to: 5 step: 2 do: { |i| system.println(i)} | ||
+ | 1 | ||
+ | 3 | ||
+ | 5 | ||
+ | > | ||
6 | 6 | ||
4 | 4 | ||
2 | 2 | ||
+ | 0 | ||
>> | >> | ||
> | > | ||
Line 429: | Line 519: | ||
===== Control Flow Constructs ===== | ===== Control Flow Constructs ===== | ||
- | Control flow constructs are defined in the lexical root of AmbientTalk. The lexical root is an object containing globally visible native methods (i.e. it is the top-level environment). We have already seen in the previous sections examples of use of the foreach and if/then structures. A list of traditional control flow structures defined in AmbientTalk is shown below: | + | Control flow constructs are defined in the "lexical root". The lexical root is an object containing globally visible native methods (i.e. it is the top-level environment). We have already seen in the previous sections examples of use of the foreach and '' |
< | < | ||
if: booleanCondition then: consequentClosure | if: booleanCondition then: consequentClosure |
at/tutorial/basic.txt · Last modified: 2020/02/09 22:05 by elisag