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 [2007/04/06 08:31] – * elisag | at:tutorial:basic [2007/04/17 16:51] – tvcutsem | ||
---|---|---|---|
Line 1: | Line 1: | ||
< | < | ||
- | **IN PROGRESS: FIRST DRAFT!!** | + | **This Tutorial is still under heavy construction!!** |
- | + | ||
- | - TODO: Adding Table splicing, quasi-quoting? | + | |
</ | </ | ||
====== Functional and Imperative Programming ====== | ====== Functional and Imperative Programming ====== | ||
- | This part of the tutorial | + | This part of the tutorial |
===== Variables ===== | ===== Variables ===== | ||
- | As usual, one can define, assign and refer to a variable. Variable definitions are made with the keyword **def**. Note that AmbientTalk is a dynamically typed language so, variables do not have a type but, they just contain | + | As usual, one can define, assign and refer to a variable. Variable definitions are made with the keyword **def**. Note that AmbientTalk is a dynamically typed language, so variables do not have a type but can contain |
- | In the examples we use the interactive AmbientTalk shell (iat) where the input and output prompt are represented by > and >> | + | In the examples we use the interactive AmbientTalk shell (iat) where the input and output prompt are represented by > and %%>>%% respectively. |
< | < | ||
Line 21: | Line 19: | ||
</ | </ | ||
- | Variable definitions can be combined with assignments as shown above. As in Pico, assignments uses the ": | + | Variable definitions can include an initialization expression that immediately initializes the variable. Variable assignment is performed by means of the well-known |
- | An assignment consists of one or more expressions, | ||
< | < | ||
>[x, y] := [ y, x ] | >[x, y] := [ y, x ] | ||
>> | >> | ||
</ | </ | ||
- | Reference | + | |
+ | As we will explain later, the '' | ||
+ | |||
+ | The variable name is used to refer a variable. The variable is evaluated when referenced. | ||
+ | < | ||
+ | >x | ||
+ | >>7 | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | When using the '': | ||
+ | </ | ||
===== Tables ===== | ===== Tables ===== | ||
- | Indexed tables represent | + | The //table// is AmbientTalk' |
< | < | ||
- | def t[ <size> ] { <expression> } | + | def t[ <sizeexpression> ] { <initexpression> } |
</ | </ | ||
- | This means that the <expression> will be evaluated | + | This constructs a table, |
< | < | ||
>def z := 0 | >def z := 0 | ||
Line 44: | Line 53: | ||
</ | </ | ||
- | Although there is no special constructor for definition of multidimensional tables, a table entry can contain another table. This is internally stored as a unidimensional | + | Although there is no special constructor for definition of multidimensional tables, a table entry can contain another table. This is internally stored as a one-dimensional |
< | < | ||
- | > | + | > |
>> | >> | ||
- | > | + | > |
>>[1, 2, [" | >>[1, 2, [" | ||
> | > | ||
Line 54: | Line 63: | ||
</ | </ | ||
- | As shown in the definition of the varible //vocals//, evaluating a series of comma-separated abstract grammar values between square brackets (aka a tabulation) results | + | As shown in the definition of the variable '' |
< | < | ||
Line 61: | Line 70: | ||
</ | </ | ||
- | === Table Splicing === | + | ==== Table Splicing |
- | TODO! | + | AmbientTalk provides the operator @ to splice tables into surrounding table expressions. |
+ | < | ||
+ | > | ||
+ | >>[1, 2, 3, 4] | ||
+ | >[1, @[2,[3]], [4], @[5], @[], 6] | ||
+ | >>[1, 2, [3], [4], 5, 6] | ||
+ | </ | ||
+ | |||
+ | The splicing operator can be also used for matching table elements as shown below. | ||
+ | < | ||
+ | >def [first, @rest] := [1,2,3,4] | ||
+ | >>[1, 2, 3, 4] | ||
+ | >rest | ||
+ | >>[2, 3, 4] | ||
+ | </ | ||
===== Functions ===== | ===== Functions ===== | ||
Line 112: | Line 135: | ||
This example also illustrates how a function can be made private by means of lexical scope. Variables and functions defined locally to functions are only visible in the scope of the function where there were defined. Note that local //inner// function is only visible inside the //fac// function and its nested scopes. Thus, calling // | This example also illustrates how a function can be made private by means of lexical scope. Variables and functions defined locally to functions are only visible in the scope of the function where there were defined. Note that local //inner// function is only visible inside the //fac// function and its nested scopes. Thus, calling // | ||
- | === Variable-Length Argument Functions === | + | ==== Variable-Length Argument Functions |
- | You can create functions that take an arbitrary | + | You can create functions that take an arbitrary number of arguments by means of the splicing operator **@** which splices the table containing the parameters into the argument list. |
< | < | ||
>def sum(@args){ { | >def sum(@args){ { | ||
Line 125: | Line 148: | ||
</ | </ | ||
- | When the //sum// function is called, the arguments are passed to the function | + | When the //sum// function is called, the //args// table is spliced and passed |
+ | |||
+ | Alternatively, | ||
< | < | ||
>def sum(a, b, @rest){ { | >def sum(a, b, @rest){ { | ||
Line 136: | Line 161: | ||
</ | </ | ||
- | In this example | + | In that case, the //sum// function |
+ | |||
+ | A function can also declare optional arguments as shown below. Optional arguments can be omitted in a function call. Internally, the default value provided in their definition is passed as the argument to the function. | ||
< | < | ||
>def incr( number, step := 1){ number + step} | >def incr( number, step := 1){ number + step} | ||
Line 190: | Line 217: | ||
>def square := { |x| x * x } | >def square := { |x| x * x } | ||
>>< | >>< | ||
- | >square(1,2) | + | >square(3) |
- | >>3 | + | >>9 |
</ | </ | ||
===== Keywords ===== | ===== Keywords ===== | ||
- | |||
AmbientTalk supports keyword messages. We have already seen some examples of keyword messages in the previous sections such as the foreach structure. In AmbientTalk keywords are transformed by the parser into functions in the form: | AmbientTalk supports keyword messages. We have already seen some examples of keyword messages in the previous sections such as the foreach structure. In AmbientTalk keywords are transformed by the parser into functions in the form: | ||
Line 206: | Line 232: | ||
- | The basic types in AmbientTalk are numbers, fractions, text, tables and booleans. In fact, these data types are nothing but objects and as such, they respond to a variety of native methods. This section | + | The basic types in AmbientTalk are numbers, fractions, text, tables and booleans. In fact, 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 |
==== Numerical data types ==== | ==== Numerical data types ==== | ||
Line 275: | Line 301: | ||
==== Tables ==== | ==== Tables ==== | ||
- | TODO! | + | |
+ | We have already introduce how to define tables. Let us now focus on how to manipulate them with the native methods provided by the table object. | ||
+ | < | ||
+ | > | ||
+ | >>[1, 3] | ||
+ | > | ||
+ | >>[2, 3, 4] | ||
+ | >def vowels := [" | ||
+ | >> | ||
+ | > | ||
+ | >>5 | ||
+ | > | ||
+ | >>" | ||
+ | > | ||
+ | >>" | ||
+ | > | ||
+ | >> | ||
+ | > | ||
+ | >>" | ||
+ | > | ||
+ | >> | ||
+ | </ | ||
+ | |||
+ | Tables also support some useful iterator methods as shown below. | ||
+ | |||
+ | < | ||
+ | >def sum:= 0; | ||
+ | >>0 | ||
+ | > | ||
+ | >> | ||
+ | >sum | ||
+ | >>6 | ||
+ | >def sumNnum (@args) { | ||
+ | args.inject: | ||
+ | } | ||
+ | >>< | ||
+ | > | ||
+ | >>6 | ||
+ | </ | ||
==== Booleans ==== | ==== Booleans ==== |
at/tutorial/basic.txt · Last modified: 2020/02/09 22:05 by elisag