Update tut1.txt
This commit is contained in:
parent
5e2d7e81a2
commit
77b51df09c
1 changed files with 12 additions and 7 deletions
15
doc/tut1.txt
15
doc/tut1.txt
|
|
@ -757,20 +757,25 @@ However, this cannot be done for mutually recursive procedures:
|
||||||
# forward declaration:
|
# forward declaration:
|
||||||
proc even(n: int): bool
|
proc even(n: int): bool
|
||||||
|
|
||||||
|
proc even(n: int): bool
|
||||||
|
|
||||||
proc odd(n: int): bool =
|
proc odd(n: int): bool =
|
||||||
if n == 0:
|
assert(n >= 0) # makes sure we don't run into negative recursion
|
||||||
true
|
if n == 0: false
|
||||||
|
else:
|
||||||
n == 1 or even(n-1)
|
n == 1 or even(n-1)
|
||||||
|
|
||||||
proc even(n: int): bool =
|
proc even(n: int): bool =
|
||||||
if n == 1:
|
assert(n >= 0) # makes sure we don't run into negative recursion
|
||||||
false
|
if n == 1: false
|
||||||
|
else:
|
||||||
n == 0 or odd(n-1)
|
n == 0 or odd(n-1)
|
||||||
|
|
||||||
Here ``odd`` depends on ``even`` and vice versa. Thus ``even`` needs to be
|
Here ``odd`` depends on ``even`` and vice versa. Thus ``even`` needs to be
|
||||||
introduced to the compiler before it is completely defined. The syntax for
|
introduced to the compiler before it is completely defined. The syntax for
|
||||||
such a forward declaration is simple: just omit the ``=`` and the
|
such a forward declaration is simple: just omit the ``=`` and the
|
||||||
procedure's body.
|
procedure's body. The ``assert`` just adds border conditions, and will be
|
||||||
|
covered later in `Modules`_ section.
|
||||||
|
|
||||||
Later versions of the language will weaken the requirements for forward
|
Later versions of the language will weaken the requirements for forward
|
||||||
declarations.
|
declarations.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue