2961

What will the program print if the language uses static scoping rules? What will it print if it uses dynamic scoping rules?
H2. Like most languages, C does not specify which order operands to operators such as + will be evaluated. Thus the statement x = exp1 + exp2; might be evaluated by first evaluating exp1, then exp2, then adding the results. But equally likely, it might evaluate exp2 first.
Devise a C assignment that will assign either 1 or 2 to x depending on whether the left or right operand of + is evaluated first.
Task: Write the following functions in scheme. Assume that n is a non-negative integer, L is a list, and x is any object. You don’t need to check that the input matches the description.
Prolog – Recursive List Processing
Tasks 1-3: In lectures we used append to check/create palindromes. Use append to check/create prefixes and suffixes and reversed lists. How many prefixes/suffixes does a list have – does your program find them all? Could you rewrite these predicates so as to avoid using append? Reverse using append takes o(N?) operations when it should only take o(N) operations to reverse a list of length N.
Document Preview:

H1. The following program was written in a fictional C-like language featuring subprogram nesting: void main() { int x = 1; int y = 2; void p() { print(x+y); } void q() { int x = 3; void r() { int y = 4; p(); } r(); } q(); } What will the program print if the language uses static scoping rules? What will it print if it uses dynamic scoping rules? H2. Like most languages, C does not specify which order operands to operators such as + will be evaluated. Thus the statement x = exp1 + exp2; might be evaluated by first evaluating exp1, then exp2, then adding the results. But equally likely, it might evaluate exp2 first. Devise a C assignment that will assign either 1 or 2 to x depending on whether the left or right operand of + is evaluated first. Task: Write the following functions in scheme. Assume that n is a non-negative integer, L is a list, and x is any object. You don’t need to check that the input matches the description. (listof n x) returns a list consisting of n elements, each of which is x. Thus => (listof 3 ‘hello) (hello hello hello) => (listof 0 42) () => (listof 2 (listof 2 2)) ((2 2) (2 2)) (nth n L) returns the nth element of L, where element 0 is the first, 1 is the second, and so on. If there are not enough elements in L, the function should return (). Thus => (nth 2 ‘(a b c d)) c => (nth 10 ‘(a b d d)) () => (nth 0 ‘(0)) 0 (rotate-left L) returns L rotated one place to the left. Thus => (rotate-left ‘(a b c)) (b c a) => (rotate-left ‘(a)) (a) => (rotate-left ‘()) () Prolog – Recursive List Processing Tasks 1-3: In lectures we used append to check/create palindromes. Use append to check/create prefixes and suffixes and reversed lists. How many prefixes/suffixes does a list have – does your program find them all? Could you rewrite these predicates so as to avoid…

Attachments:

H1.docx