This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
at:tutorial:basic [2007/04/05 09:57] elisag |
at:tutorial:basic [2020/02/09 22:05] elisag |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | < | ||
- | **UNDER CONSTRUCTION!!** | ||
- | </ | ||
- | ==== 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. | ||
- | |||
- | ==== 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 values. | ||
- | |||
- | In the examples we use the interactive AmbientTalk shell (iat) where the input and output prompt are represented by > and >> , respectively. | ||
- | |||
- | < | ||
- | >def x := 5 | ||
- | >>5 | ||
- | >def y := x + 2 | ||
- | >>7 | ||
- | </ | ||
- | |||
- | Variable definitions can be combined with assignments as shown above. As in Pico, assignments uses the ": | ||
- | |||
- | An assignment consists of one or more expressions, | ||
- | < | ||
- | >[x, y] := [ y, x ] | ||
- | >> | ||
- | </ | ||
- | Reference is just done by evaluating the variable. | ||
- | |||
- | ==== Tables ==== | ||
- | |||
- | As in Pico, indexed tables represent what other languages call arrays or lists. Tables are unidimensional and their indexes range from 1 to the size of the table. As variables, one can define, assign and refer to a table. Table definition is also made with the keyword **def** in the following form: | ||
- | < | ||
- | def t[ < | ||
- | </ | ||
- | This means that the < | ||
- | < | ||
- | >def z := 0 | ||
- | >>0 | ||
- | >def table[5] { z := z + 1 } | ||
- | >>[1, 2, 3, 4, 5] | ||
- | </ | ||
- | 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. | ||
- | |||
- | < | ||
- | >def vocals := [" | ||
- | >> | ||
- | > | ||
- | >>[1, 2, [" | ||
- | > | ||
- | >>" | ||
- | </ | ||
- | |||
- | As shown in the definition of the varible " | ||
- | |||
- | < | ||
- | >[ 1, table, " | ||
- | >>[1, [1, 2, [" | ||
- | </ | ||
- | |||
- | ==== Functions ==== | ||
- | |||
- | As variables and tables, functions are defined with the keyword **def** in the form of: | ||
- | < | ||
- | def functionname( < | ||
- | </ | ||
- | The argument list is just a list of local variables which are always evaluated one by one from left to right. A basic function looks like this: | ||
- | < | ||
- | >def square (x) { x*x } | ||
- | >>nil | ||
- | > | ||
- | >>25 | ||
- | </ | ||
- | This example also illustrates how functions are called. Calls to functions without parameters must also include the parenthesis. | ||
- | |||
- | Functions have access to the enclosing environment of its definition as shown in the following example. | ||
- | < | ||
- | >def counter := 0 | ||
- | >> 0 | ||
- | > def inc() { counter := counter + 1} | ||
- | >>< | ||
- | > inc() | ||
- | >>1 | ||
- | </ | ||
- | |||
- | Functions can call themselves recusively and they can also be nested in the definitions of other functions such as: | ||
- | < | ||
- | >def fac(n) { | ||
- | def inner(n, result) { | ||
- | if: (n =0) then: { result } else: { inner( n-1, n * result) | ||
- | }; | ||
- | inner(n,1) | ||
- | } | ||
- | >>< | ||
- | >fac(5) | ||
- | >>120 | ||
- | </ | ||
- | |||
- | Variables and functions defined locally to functions are only visible in the scope of the function where there were defined. | ||
- | |||
- | === Variable-Length Argument Functions === | ||
- | |||
- | You can create functions that take an arbitrary | ||
- | < | ||
- | >def sum(@args){ { | ||
- | def total := 0; | ||
- | foreach: { |el| total := total + el } in: args; | ||
- | total} | ||
- | >>< | ||
- | > | ||
- | >>6 | ||
- | </ | ||
- | |||
- | When the //sum// function is called, the arguments are passed to the function in a table called " | ||
- | < | ||
- | >def sum(a, b, @rest){ { | ||
- | def total := a + b; | ||
- | foreach: { |el| total := total + el } in: rest; | ||
- | total} | ||
- | >>< | ||
- | > | ||
- | >>6 | ||
- | </ | ||
- | |||
- | In this example the //sum// function accepts an arbitrary number of arguments as long as two arguments, //a// and //b//, are supplied. //a// and //b// are thus considered as mandatory arguments. A function can also declare optional arguments as shown below: | ||
- | < | ||
- | >def incr( number, step := 1){ number + step} | ||
- | >>< | ||
- | >incr(3) | ||
- | >>4 | ||
- | > | ||
- | >>6 | ||
- | </ | ||
- | |||
- | ==== Closures and Blocks ==== | ||
- | |||
- | The function name can also be used just to refer the function but without calling it. TODO! | ||
- | |||
- | Unlike Pico, AmbientTalk doesn' | ||
- | |||
- | < | ||
- | >def sum := 0 | ||
- | >>0 | ||
- | >sum := sum + 1 | ||
- | >>1 | ||
- | >sum := { | x, y| x + y } | ||
- | >>nil | ||
- | > | ||
- | >>3 | ||
- | </ | ||
- | |||
- | ==== Blocks ==== |