aPToP / LaPToP

Examples

Snippets from interpreter demos (Lang.Demo). Highlighted and editable โ€” try \impliedby \equiv \times \cdot \prime. Open in Interpreter to Run.

sumTo

Sum 1..n with a while-loop. Sample: n=10.

i:= 0. s:= 0. while i โ‰  n do i:= i+1. s:= s+i od

forLoop

Same sum with for i:= 1;..n+1.

s:= 0. for i:= 1;..n+1 do s:= s + i od

gcd

Euclid as a recursive procedure. Sample: x=12, y=18.

Gcd(a, b) โ‡ if b = 0 then g:= a else Gcd(b, a mod b) fi Gcd(x, y)

listSum

Refinements summing a list L.

A โ‡ s:= 0. n:= 0. B B โ‡ if n = #L then ok else s:= s + L n. n:= n+1. B fi

swap

Simultaneous assignment.

x, y:= y, x

exitLoop

do โ€ฆ exit when โ€ฆ od.

do exit when n โ‰ค x. x:= x+1 od