06 Abstraction
09 April 2025
11:02
int x =1 , y =2;
void f (int a, int b) {
x += a+b;
a++;
B = a/2;
}
int main () {
f (x ,y );
printf ("x:%d , y:%d\n",x ,y );
return 0;
}
Variable binding example, in constant binding the function parameters would be
like "const int a, const int b" (so the a++ and b=a/2 lines would give error)
#include <cstdio>
int x = 1, y = 2;
void f(int &ra, int &rb) {
int a = ra, b = rb;
x += a + b;
a++;
b = a/2;
printf("x:%d y:"%d a:%d b:%d\n", x, y, a, b);
printf("x:%p y:"%p a:%p b:%p\n", &x, &y, &a, &b);
ra = a;
rb = b;
}
Example of copy-in-out in C++.
int main() {
f(++x, ++y);
printf("x:%d y:%d\n", x,y);
• I took picture of example of substitution in C++ as well.
The right side of the picture is the preprocessor output obtained by "gcc -E" and then indented.
Why doesn't everyone use Lazy Evaluation?
Because it's hard to implement. Also substitution is harder to implement than copy-in, and we also need
runtime to be able to do Normal Order Evaluation or Lazy Evaluation.
Code for finding primes (not the best code though, but it's nice), which is possible through lazy evaluation.
Primes = integral a => [a]
Primes = map head (iterate Sieve [2..])
Sieve (p:xs) = [x | x<-xs, x `rem` p/=0]
Programming Language Concepts Page 1