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 10:47] – * elisag | at:tutorial:basic [2007/04/17 16:40] – tvcutsem | ||
---|---|---|---|
Line 1: | Line 1: | ||
< | < | ||
- | **IN PROGRESS: FIRST DRAFT!!** | + | **This Tutorial is still under heavy construction!!** |
- | + | ||
- | - Add 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 <code>>></ |
< | < | ||
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 ] | ||
>> | >> | ||
</ | </ | ||
+ | |||
+ | As we will explain later, the '' | ||
The variable name is used to refer a variable. The variable is evaluated when referenced. | The variable name is used to refer a variable. The variable is evaluated when referenced. | ||
Line 34: | Line 33: | ||
>>7 | >>7 | ||
</ | </ | ||
+ | |||
+ | < | ||
+ | When using the '': | ||
+ | </ | ||
===== Tables ===== | ===== Tables ===== | ||
Line 68: | Line 71: | ||
==== 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 119: | Line 136: | ||
==== 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 130: | Line 147: | ||
</ | </ | ||
- | 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 141: | Line 160: | ||
</ | </ | ||
- | 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 195: | Line 216: | ||
>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 211: | Line 231: | ||
- | 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 303: | Line 323: | ||
</ | </ | ||
- | Tables also support some useful iterator methods | + | Tables also support some useful iterator methods as shown below. |
< | < | ||
>def sum:= 0; | >def sum:= 0; |
at/tutorial/basic.txt · Last modified: 2020/02/09 22:05 by elisag