From abe1051374c2213ab28a8e7814ef3cc11d044e77 Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 16 Jun 2015 15:58:21 +0300 Subject: Update tut1.txt --- doc/tut1.txt | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'doc') diff --git a/doc/tut1.txt b/doc/tut1.txt index 1fa495054..6f10e7cbb 100644 --- a/doc/tut1.txt +++ b/doc/tut1.txt @@ -758,9 +758,13 @@ However, this cannot be done for mutually recursive procedures: proc even(n: int): bool proc odd(n: int): bool = + if n == 0: + true n == 1 or even(n-1) proc even(n: int): bool = + if n == 1: + true n == 0 or odd(n-1) Here ``odd`` depends on ``even`` and vice versa. Thus ``even`` needs to be -- cgit 1.4.1-2-gfad0 From 5e2d7e81a23750661dca883adaa97745234879c4 Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 16 Jun 2015 16:00:33 +0300 Subject: Update tut1.txt --- doc/tut1.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'doc') diff --git a/doc/tut1.txt b/doc/tut1.txt index 6f10e7cbb..084de453a 100644 --- a/doc/tut1.txt +++ b/doc/tut1.txt @@ -764,7 +764,7 @@ However, this cannot be done for mutually recursive procedures: proc even(n: int): bool = if n == 1: - true + false n == 0 or odd(n-1) Here ``odd`` depends on ``even`` and vice versa. Thus ``even`` needs to be -- cgit 1.4.1-2-gfad0 From 77b51df09c6e853e4e852b8676ea0c2666b7683b Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 16 Jun 2015 16:14:23 +0300 Subject: Update tut1.txt --- doc/tut1.txt | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'doc') diff --git a/doc/tut1.txt b/doc/tut1.txt index 084de453a..500480cf0 100644 --- a/doc/tut1.txt +++ b/doc/tut1.txt @@ -757,20 +757,25 @@ However, this cannot be done for mutually recursive procedures: # forward declaration: proc even(n: int): bool - proc odd(n: int): bool = - if n == 0: - true +proc even(n: int): bool + +proc odd(n: int): bool = + assert(n >= 0) # makes sure we don't run into negative recursion + if n == 0: false + else: n == 1 or even(n-1) - proc even(n: int): bool = - if n == 1: - false +proc even(n: int): bool = + assert(n >= 0) # makes sure we don't run into negative recursion + if n == 1: false + else: n == 0 or odd(n-1) 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 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 declarations. -- cgit 1.4.1-2-gfad0