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 revisionBoth sides next revision
at:tutorial:basic [2007/04/17 16:24] – * tvcutsemat:tutorial:basic [2007/04/17 16:40] tvcutsem
Line 4: Line 4:
 ====== 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 <code>>></code> 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 =====
at/tutorial/basic.txt · Last modified: 2020/02/09 22:05 by elisag