28.10.2022 Views

Computability complexity and Languages- Fundamentals of Theoretical Computer Science

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

3. Syntax 25

Exercises

1. Write a program in Y (using macros freely) that computes the

function f(x) = 3x.

2. Write a program in Y that solves Exercise 1 using no macros.

3. Let f(x) = 1 if x is even; f(x) = 0 if x is odd. Write a program in Y

that computes f.

4. Let f(x) = 1 if x is even; f(x) undefined if x is odd. Write a program

in Y that computes f.

5. Let f(x 1 , x 2 ) = 1 if x 1 = x 2 ; f(x 1 , x 2 ) = 0 if x 1 =I= x 2 • Without using

macros, write a program in Y that computes f.

6. Let f(x) be the greatest number n such that n 2 ~ x. Write a program

in Y that computes f.

7. Let gcd(x 1 , x 2 ) be the greatest common divisor of x 1 and x 2 • Write a

program in Y that computes gcd.

3. Syntax

We are now ready to be mercilessly precise about the language Y. Some

of the description recapitulates the preceding discussion.

The symbols

are called input variables,

zl Zz z3 ...

are called local variables, and Y is called the output variable of Y. The

symbols

AI Bl Cl Dl £1 Az Bz ...

are called labels of Y. (As already indicated, in practice the subscript 1 is

often omitted.) A statement is one of the following:

v~ V+ 1

v~ v-1

v~ v

IF V =I= 0 GOTO L

where V may be any variable and L may be any label.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!