1. Introduction to Programming Concepts

"There is no royal road to geometry."
"Just follow the yellow brick road."
- Euclid's reply to Ptolemy, Euclid (c. 300 BC)
- The Wonderful Wizard of Oz, L. Frank Baum (1856–1919)

1. Calculator

2. Variables

declare
V = 9999 * 9999
{Browse V*V}

Exercise 1

Exercise 2

3. Functions

Factorial

declare
fun {Fact N}
    if N==0 then
        1
    else
        N*{Fact N-1}
    end
end

Combinations

declare
fun {Comb N R}
    {Fact N} div ({Fact R}*{Fact N-R})
end

4. Lists

declare
H = 5
T = [6 7 8]
{Browse H|T}
declare
L = [5 6 7 8]
{Browse L.1}
{Browse L.2}
5
[6 7 8]

Pattern matching

declare
L = [5 6 7 8]
case L of H|T then
    {Browse H} {Browse T}
end

5. Functions over lists

   1
  1 1
 1 2 1
1 3 3 1

Pascal's triangle in Oz:

declare Pascal AddList ShiftLeft ShiftRight

fun {Pascal N}
    if N==1 then [1]
    else
        {AddList {ShiftLeft {Pascal N-1}}
                 {ShiftRight {Pascal N-1}}}
    end
end

fun {ShiftLeft L}
    case L of H|T then
        H|{ShiftLeft T}
    else [0] end
end

fun {ShiftRight L} 0|L end

fun {AddList L1 L2}
    case L1 of H1|T1 then
        case L2 of H2|T2 then
            H1+H2|{AddList T1 T2}
        end
    else nil
    end
end

6. Complexity

fun {Pascal N}
    if N==1 then [1]
    else
        {AddList {ShiftLeft {Pascal N-1}}
                 {ShiftRight {Pascal N-1}}}
    end
end
fun {FastPascal N}
    if N==1 then [1]
    else L in
        L = {FastPascal N-1}
        {AddList {ShiftLeft L} {ShiftRight L}}
    end
end

7. Lazy evaluation

fun lazy {Ints N}
    N | {Ints N+1}
end

Exercise 3

fun {SumList L}
    case L of X|L1 then X+{SumList L1}
    else 0 end
end

Lazy calculation of Pascal's triangle

fun lazy {PascalList Row}
    Row | { PascalList
                {AddList {ShiftLeft Row}
                         {ShiftRight Row}} }
end
declare
L = {PascalList [1]}

Exercise 4

fun {PascalList2 N Row}
    if N==1 then
        [Row]
    else
        Row | {PascalList2 N-1
                   {AddList {ShiftLeft Row}
                            {ShiftRight Row}}}
    end
end

8. Higher-order programming

fun {GenericPascal Op N}
    if N==1 then [1]
    else L in
        L = {GenericPascal Op N-1}
        {OpList Op {ShiftLeft L} {ShiftRight L}}
    end
end

fun {OpList Op L1 L2}
    case L1 of H1|T1 then
        case L2 of H2|T2 then
            {Op H1 H2}|{OpList Op T1 T2}
        end
    else nil end
end

Exercise 5

Assignment 1

9. Concurrency

thread P in
    P = {Pascal 25}
    {Browse P}
end
{Browse 99*99}

10. Dataflow

declare X
thread
   {Delay 5000}
   X=99
end
{Browse start} {Browse X*X}

11. State

A memory cell

declare
C = {NewCell 0}
C := @C + 1
{Browse @C}

Assignment 2

local X in
    X = 23
    local X in
        X = 44
    end
    {Browse X}
end
local X in
    X = {NewCell 23}
    X := 44
    {Browse @X}
end

Adding memory to a function

declare
C = {NewCell 0}
fun {Add A B}
   C := @C + 1
   A + B
end

Exercise 6

Assignment 3

{Browse {Accumulate 5}}
{Browse {Accumulate 100}}
{Browse {Accumulate 45}}
declare
fun {Accumulate N}
    Acc in
        Acc = {NewCell 0}
        Acc := @Acc + N
        @Acc
end

12. Objects

Exercise 7

declare
local C in
    %% your implementation of the object %%
end

13. Classes

declare
fun {NewCounter}
    C Bump Read in
        C = {NewCell 0}
        fun {Bump}
            C := @C + 1
            @C
        end
        fun {Read}
            @C
        end
        counter(bump:Bump read:Read)
end
declare
Ctr1 = {NewCounter}
Ctr2 = {NewCounter}

{Browse {Ctr1.bump}}
{Browse {Ctr1.bump}}
{Browse {Ctr2.read}}

14. Nondeterminism and time

declare
C = {NewCell 0}
thread
    C := 1
end
thread
    C := 2
end
declare
C = {NewCell 0}
thread I in
    I = @C
    C := I + 1
end
thread J in
    J = @C
    C := J + 1
end

15. Atomicity

declare
C = {NewCell 0}
L = {NewLock}
thread
    lock L then I in
        I = @C
        C := I + 1
    end
end
thread
    lock L then J in
        J = @C
        C := J + 1
    end
end

< ^ >