User Tools

Site Tools


at:tutorial:basic

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
at:tutorial:basic [2007/04/06 11:37] – * elisagat:tutorial:basic [2007/04/17 16:51] tvcutsem
Line 1: Line 1:
 <note> <note>
-**This Tutorial is still under heavy construction**+**This Tutorial is still under heavy construction!!**
 </note> </note>
 ====== Functional and Imperative Programming ====== ====== Functional and Imperative Programming ======
    
-This part of the tutorial shows AmbientTalk as a simple expression language with a minimum syntax which resembles very on Java script. This section mainly describes the basic features of the language, namely variables, functions and tables and control flow.+This part of the tutorial explains AmbientTalk as a simple expression language with a flexible syntax which resembles languages like Ruby, Python and Javascript. This section mainly describes the basic features of the language, namely variables, functionstables (i.e. arrays) and control flow primitives.
  
 ===== 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 sovariables do not have a type but, they just contain values.+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 languageso variables do not have a type but can contain any value.
  
-In the examples we use the interactive AmbientTalk shell (iat) where the input and output prompt are represented by > and >> respectively. +In the examples we use the interactive AmbientTalk shell (iat) where the input and output prompt are represented by > and %%>>%% respectively. 
  
 <code> <code>
Line 19: Line 19:
 </code> </code>
  
-Variable definitions can be combined with assignments as shown aboveAs in Pico, assignments uses the ":=" operator. Note that there must be an space between the variable and the ":=" operator in order for the parse to resolve the ambiguity between a keyword message and a assignment, e.g. ":= 1" is understood as an assignment while "a:" as a keyword. We will further elaborate on keywords in the following sections.+Variable definitions can include an initialization expression that immediately initializes the variableVariable assignment is performed by means of the well-known ":=" operator. AmbientTalk supports assignment to multiple variables as a single assignment expression. For this to work, the number of variable names on the left hand side of ":=" must match the number of expressions on the right hand side of ":=". A typical application of this is to swap the values of two variables more easily:
  
-An assignment consists of one or more expressions, providing that the number of expressions on the right hand side match the number of variables on the left hand side. This allows a permutation of variables such as: 
 <code> <code>
 >[x, y] := [ y, x ] >[x, y] := [ y, x ]
 >>[7,5] >>[7,5]
 </code> </code>
 +
 +As we will explain later, the ''[y,x]'' syntax simply denotes a literal table (a.k.a. an array).
  
 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 32: Line 33:
 >>7 >>7
 </code> </code>
 +
 +<note>
 +When using the '':='' assignment operator, beware of the following syntactic annoyance: the expression ''a := 1'' denotes an assignment to the variable ''a'', while ''a:= 1'' is misunderstood by the parser as ''a: = 1'', which is the invocation of a keyworded message named ''a:''. Keyworded message sends will be explained later on in this chapter. Hence, as a general rule, don't forget to always put a space between the variable name and the '':='' operator.
 +</note>
  
 ===== Tables ===== ===== Tables =====
  
-Indexed tables represent what other languages call arrays or listsTables are unidimensional and their indexes range from 1 to the size of the tableAs variables, one can define, assign and refer to a table. Table definition is also made  with the keyword **def** in the following form:+The //table// is AmbientTalk's native compound data type. It is akin to what other languages call //arrays//The main difference is that tables are indexed from ''1'' up to their ''length'', while arrays are indexed from ''0'' up to ''length-1''Like with variables, one can define, assign and refer to a table. Table definitions are also formed with the keyword **def** in the following format:
 <code> <code>
-def t[ <size> ] { <expression> }+def t[ <sizeexpression> ] { <initexpression> }
 </code> </code>
-This means that the <expressionwill be evaluated <sizetimes, i.e., one for each slot of the table. This allows expressions such as initializing a table of ascending numbers as shown below:+This constructs a table, the size of which is determined by ''<sizeexpression>''. The content of each slot is the result of evaluating ''<initexpression>''This means that ''<initexpression>'' is evaluated for each slot in the table! Tables of e.g. ascending numbers are easily formed: 
 <code> <code>
 >def z := 0 >def z := 0
Line 47: Line 53:
 </code> </code>
  
-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 table whose entries are other tables.+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 table whose entries are other tables.
 <code> <code>
 >def vowels := ["a", "e", "i", "o", "u"] >def vowels := ["a", "e", "i", "o", "u"]
Line 57: Line 63:
 </code> </code>
  
-As shown in the definition of the varible //vowels//evaluating a series of comma-separated data types between square brackets (aka a tabulation) results in table.+As shown in the definition of the variable ''vowels''AmbientTalk provides literal syntax to encode in-line tables. Table assignment and indexation work as usual, but recall that table indices range from ''1'' up to ''table.length''. Some more examples of literal tables:
  
 <code> <code>
Line 66: Line 72:
 ==== Table Splicing ==== ==== Table Splicing ====
  
-TODO!+AmbientTalk provides the operator @ to splice tables into surrounding table expressions.  
 +<code> 
 +>[1,@[2,3],4] 
 +>>[1, 2, 3, 4] 
 +>[1, @[2,[3]], [4], @[5], @[], 6] 
 +>>[1, 2, [3], [4], 5, 6] 
 +</code> 
 + 
 +The splicing operator can be also used for matching table elements as shown below. 
 +<code> 
 +>def [first, @rest] := [1,2,3,4] 
 +>>[1, 2, 3, 4] 
 +>rest 
 +>>[2, 3, 4] 
 +</code>
  
 ===== Functions ===== ===== Functions =====
Line 197: Line 217:
 >def square := { |x| x * x } >def square := { |x| x * x }
 >><closure:lambda> >><closure:lambda>
->square(1,2+>square(3
->>3+>>9
 </code> </code>
  
at/tutorial/basic.txt · Last modified: 2020/02/09 22:05 by elisag