2021-12-06

Practice Final Thread.

Please post your practice final solutions to this thread.
Best, Chris
Please post your practice final solutions to this thread. Best, Chris

-- Practice Final Thread
Harrison Hwang, Bill Li, Christina Ng.
Question 6: Briefly explain how C struct are implemented in memory by the compiler.
The C struct is stored in memory sequentially, memory size is allocated based on the sizes of the types defined in the struct. Characters and booleans occupy one byte, and ints occupy 4 bytes, doubles occupy 8 bytes. In a 32 bit machines, memory is reserved as multiples of 4 bytes and in a 64bit machine each word will take up 8 bytes. for example Struct example { int atomic_number; double atomic_weight; _Bool metallic; } e For a 64bit system, the minimum size allocated would be 8+8+8 = 24bytes.
If the ((_packed_)) attribute is included, the lowest amount of memory would be allocated: 4+8+1 = 13 bytes
(Edited: 2021-12-06)
Harrison Hwang, Bill Li, Christina Ng. Question 6: Briefly explain how C struct are implemented in memory by the compiler. The C struct is stored in memory sequentially, memory size is allocated based on the sizes of the types defined in the struct. Characters and booleans occupy one byte, and ints occupy 4 bytes, doubles occupy 8 bytes. In a 32 bit machines, memory is reserved as multiples of 4 bytes and in a 64bit machine each word will take up 8 bytes. for example <nowiki> Struct example { int atomic_number; double atomic_weight; _Bool metallic; } e </nowiki> For a 64bit system, the minimum size allocated would be 8+8+8 = 24bytes. If the ((_packed_)) attribute is included, the lowest amount of memory would be allocated: 4+8+1 = 13 bytes

-- Practice Final Thread
 Mark Masulis
 Andre Domingo
 Raymond Lin 
 E1 --> E2 + T      E1.val := sum(E2.val, T.val)
 E1 --> E2 - T      E1.val := difference(E2.val, T.val)
 E --> T            E.val := T.val
 F --> float        F.val := float.val
 F1 --> -F2         F1.val := inverse(F2.val)
 T1 --> T2 * F      T1.val := product(T2.val, F.val)
 T --> F            T.val := F.val
(Edited: 2021-12-06)
Mark Masulis Andre Domingo Raymond Lin E1 --> E2 + T E1.val := sum(E2.val, T.val) E1 --> E2 - T E1.val := difference(E2.val, T.val) E --> T E.val := T.val F --> float F.val := float.val F1 --> -F2 F1.val := inverse(F2.val) T1 --> T2 * F T1.val := product(T2.val, F.val) T --> F T.val := F.val

-- Practice Final Thread
Nicholas Semaan and Yuanye Yang 
 
8. Differentiate applicative versus normal order evaluation. Give an example using Scheme. Give an example of defining a new macro in Scheme.
 
Applicative order evaluates subexpressions when, i.e. just before, the procedure is applied.
Normal order passes subexpressions as they are, without evaluation, and proceeds with the evaluation only when the corresponding formal parameter is actually to be itself evaluated.
 
Example:	
def double x = (plus x x)
def average x y = (divide (plus x y) 2)
Normal:	
(double (average 2 4)) =>
(plus (average 2 4) (average 2 4)) =>
(plus (divide (plus 2 4) 2) (average 2 4)) =>
(plus (divide 6 2) (average 2 4)) =>
(plus 3 (average 2 4)) =>
(plus 3 (divide (plus 2 4) 2)) =>
(plus 3 (divide 6 2)) =>
(plus 3 3) =>
6
Applicative:	
double (average 2 4) =>
double (divide (plus 2 4) 2) =>
double (divide 6 2) =>
double 3 =>
plus 3 3 =>
6 
 
Macro Example:	
If your Scheme lacks the conditional special form when, you could define when as the following macro:
 
(define-syntax when
  (lambda (test . branch)
    (list 'if test
      (cons 'begin branch)))) 
 
(Edited: 2021-12-06)
<pre> Nicholas Semaan and Yuanye Yang 8. Differentiate applicative versus normal order evaluation. Give an example using Scheme. Give an example of defining a new macro in Scheme. Applicative order evaluates subexpressions when, i.e. just before, the procedure is applied. Normal order passes subexpressions as they are, without evaluation, and proceeds with the evaluation only when the corresponding formal parameter is actually to be itself evaluated. <u>'''Example:'''</u> def double x = (plus x x) def average x y = (divide (plus x y) 2) <u>'''Normal:'''</u> (double (average 2 4)) => (plus (average 2 4) (average 2 4)) => (plus (divide (plus 2 4) 2) (average 2 4)) => (plus (divide 6 2) (average 2 4)) => (plus 3 (average 2 4)) => (plus 3 (divide (plus 2 4) 2)) => (plus 3 (divide 6 2)) => (plus 3 3) => 6 <u>'''Applicative:'''</u> double (average 2 4) => double (divide (plus 2 4) 2) => double (divide 6 2) => double 3 => plus 3 3 => 6 <u>'''Macro Example:'''</u> If your Scheme lacks the conditional special form when, you could define when as the following macro: (define-syntax when (lambda (test . branch) (list 'if test (cons 'begin branch)))) </pre>

-- Practice Final Thread
Luksawee, Alec Moore, Kyle Domen, Ameya Dighe
Pass by value: replace parameters in the body of the procedure by the corresponding argument value
(define (add x)
   lambda(y)(
      (set! x (+ x y))
)
    )
) (define add1 (add 2)) (add1 5)
Pass by reference - callee gives a direct reference to the variable and both caller and callee point to the same variable in memory.
void increment(int %count) {
     count++;
}
int main() {
     int count = 0;
     increment(count);
     printf("%d", count);
}
pass by value result- do pass by value and then take result and copy it back to location. ex. begin integer n; procedure p(k:int)
    begin
    n: n+1
    k: k+4
    print(n)
    end
main
   n = 0
   p(n)
   print(n)
   end
which the call by value result it will be 1 4
(Edited: 2021-12-06)
Luksawee, Alec Moore, Kyle Domen, Ameya Dighe Pass by value: replace parameters in the body of the procedure by the corresponding argument value (define (add x) lambda(y)( (set! x (+ x y)) ) ) ) (define add1 (add 2)) (add1 5) Pass by reference - callee gives a direct reference to the variable and both caller and callee point to the same variable in memory. void increment(int %count) { count++; } int main() { int count = 0; increment(count); printf("%d", count); } pass by value result- do pass by value and then take result and copy it back to location. ex. begin integer n; procedure p(k:int) begin n: n+1 k: k+4 print(n) end main n = 0 p(n) print(n) end which the call by value result it will be 1 4

-- Practice Final Thread
Team Members: Brandon Ong, Aaron Xiang, and Chris McCain
4. Define structured programming. Give an example of a multi-level return statement, briefly explain Scheme continuations.
Structured programming is programming that emphasizes modularization of code, structured types, descriptive variable and constant names, and extensive comments
PHP Example for multi-level returns:
function two_return(){
            return 2;
} function testReturns(){
	for($i = 0; $i < 100; $i++){
		for($j = 0; $j < 20; $j++){
			if($j == 20){
				echo "Return 1 at ";
				echo $j;
				echo "\n";
				return 1;
			}
			if($i == 50){
				echo "Return 2 at ";
				echo $i;
				echo "\n";
				two_return();
			}
		}
	}
}
        
Scheme Continuations: Use the function call-with continuation or call/cc to set a single parameter procedure to receive and manipulate an environment. After call/cc, the procedure is run in the supplied environment and once call/cc completes the execution resumes.
(Edited: 2021-12-06)
Team Members: Brandon Ong, Aaron Xiang, and Chris McCain 4. Define structured programming. Give an example of a multi-level return statement, briefly explain Scheme continuations. Structured programming is programming that emphasizes modularization of code, structured types, descriptive variable and constant names, and extensive comments PHP Example for multi-level returns: function two_return(){ return 2; } function testReturns(){ for($i = 0; $i < 100; $i++){ for($j = 0; $j < 20; $j++){ if($j == 20){ echo "Return 1 at "; echo $j; echo "\n"; return 1; } if($i == 50){ echo "Return 2 at "; echo $i; echo "\n"; two_return(); } } } } Scheme Continuations: Use the function call-with continuation or call/cc to set a single parameter procedure to receive and manipulate an environment. After call/cc, the procedure is run in the supplied environment and once call/cc completes the execution resumes.

-- Practice Final Thread
Shaoyue Liu, Thanh Le Question 3: What is short circuit evaluation? Give a C example of short circuit versus non short circuit evaluation. If we have a boolean expression, we can often determine its value without having to completely evaluate it. Short circuit evaluation: #include int main() { int a = 10; int b = -1; #does not check the second condition after the first conditon failed if (a != 10 && b == -1) { printf("Don't print this line."); } else { printf("Print this."); } return 0; } Considering the program above, if we are using non short circuit evaluation, the program would evaluate the second condition after the first condition failed.
(Edited: 2021-12-06)
<nowiki> Shaoyue Liu, Thanh Le Question 3: What is short circuit evaluation? Give a C example of short circuit versus non short circuit evaluation. If we have a boolean expression, we can often determine its value without having to completely evaluate it. Short circuit evaluation: #include <stdio.h> int main() { int a = 10; int b = -1; #does not check the second condition after the first conditon failed if (a != 10 && b == -1) { printf("Don't print this line."); } else { printf("Print this."); } return 0; } Considering the program above, if we are using non short circuit evaluation, the program would evaluate the second condition after the first condition failed. </nowiki>

-- Practice Final Thread
Sweet Zhang, Tomer Erlich
7. Explain how type inference could evaluate the type of 7*3.14 + 5. Sketch the type checking process in Ocaml.
The compiler will look for implementations of '*' that will take the types (int, float), it could be that '*' is overloaded and has an implementation of (float, float) since int can be coerced into type float by int -> (float) int, therefore *(7, 3.14) would evaluate to float. The same process would then apply to implementations of +, which would find +(float, float) therefore +(*(7,3.14), (float)5) -> float
Resource Description for IMG_4695.jpg
(Edited: 2021-12-06)
Sweet Zhang, Tomer Erlich 7. Explain how type inference could evaluate the type of 7*3.14 + 5. Sketch the type checking process in Ocaml. The compiler will look for implementations of '*' that will take the types (int, float), it could be that '*' is overloaded and has an implementation of (float, float) since int can be coerced into type float by int -> (float) int, therefore *(7, 3.14) would evaluate to float. The same process would then apply to implementations of +, which would find +(float, float) therefore +(*(7,3.14), (float)5) -> float ((resource:IMG_4695.jpg|Resource Description for IMG_4695.jpg))

-- Practice Final Thread
 Team Members: Angela Pham, Jesse Dong, Ian Chavez, Arun Murugan
 10. square_sum_up(1,1) :-!.
     square_sum_up(N,X) :- N1 is N-1,
                          square_sum_up(N1,X1),
                          X is X1+(N * N).
     diff_sum(LO,HI,X)  :- LO1 is LO-1,
                           square_sum_up(LO1,X1),
                           square_sum_up(HI,X2),
                           X is X2-X1.
(Edited: 2021-12-06)
Team Members: Angela Pham, Jesse Dong, Ian Chavez, Arun Murugan 10. square_sum_up(1,1) :-!. square_sum_up(N,X) :- N1 is N-1, square_sum_up(N1,X1), X is X1+(N * N). diff_sum(LO,HI,X) :- LO1 is LO-1, square_sum_up(LO1,X1), square_sum_up(HI,X2), X is X2-X1.

-- Practice Final Thread
 Question 2:
 Jason Hammar
 Leo Alciso
 Edward Phan
 Attribute grammars are a framework for defining semantics of programming languages in a syntax-directed fashion.
 S-attributed Grammars are grammars where the left-hand side consists of a non-terminal that is dependent on the attribute of a non-terminal on the right-hand side.
 Example: A->B > A.Val = B.Val
 
 L-attributed Grammars are grammars that have a non-terminal on the right-hand side which has its value depend on another attribute on the right hand side of the same production rule. 
 Example: A->BC > B.Val = C.Val
 A side effect is something a programming language has where it influences subsequent computation in any way other than by returning a value for use in the surrounding context.
 i.e. In some languages, making a variable on the global scope may affect the return value of the function.
(Edited: 2021-12-06)
Question 2: Jason Hammar Leo Alciso Edward Phan Attribute grammars are a framework for defining semantics of programming languages in a syntax-directed fashion. S-attributed Grammars are grammars where the left-hand side consists of a non-terminal that is dependent on the attribute of a non-terminal on the right-hand side. Example: A->B > A.Val = B.Val L-attributed Grammars are grammars that have a non-terminal on the right-hand side which has its value depend on another attribute on the right hand side of the same production rule. Example: A->BC > B.Val = C.Val A side effect is something a programming language has where it influences subsequent computation in any way other than by returning a value for use in the surrounding context. i.e. In some languages, making a variable on the global scope may affect the return value of the function.
[ Next ]
X