LatteScript does not currently have a tutorial, but the following code samples should be helpful for learning LatteScript if you already know how to program.
procedure insert node, item if node = nothing pass else if item < node@1 if node@2 = nothing node@2 := [item, nothing, nothing] else insert node@2, item else if node@3 = nothing node@3 := [item, nothing, nothing] else insert node@3, item procedure walk node if node = nothing pass else walk node@2 print node@1 walk node@3 procedure show node if node = nothing pass else write "(" write node@1 if node@2 = nothing pass else write " L:" show node@2 if node@3 = nothing pass else write " R:" show node@3 write ")" root := ["l", nothing, nothing] items := "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" for item in items insert root, item walk root print "" print "Tree:" print root print "TREE:" show root
procedure insert node, item if node = nothing pass else if item < node@1 if node@2 = nothing node@2 := [item, nothing, nothing] else insert node@2, item else if node@3 = nothing node@3 := [item, nothing, nothing] else insert node@3, item procedure walk node if node = nothing pass else walk node@2 print node@1 walk node@3 procedure show node if node = nothing pass else write "(" write node@1 if node@2 = nothing pass else write " L:" show node@2 if node@3 = nothing pass else write " R:" show node@3 write ")" item := read root := ["m", nothing, nothing] until item = "quit" insert root, item item := read walk root print "" print "Tree:" print root print "TREE:" show root
procedure insert node, item if node = nothing pass else if item < node@1 if node@2 = nothing node@2 := [item, nothing, nothing] else insert node@2, item else if node@3 = nothing node@3 := [item, nothing, nothing] else insert node@3, item procedure walk node if node = nothing pass else walk node@2 print node@1 walk node@3 root := [30, nothing, nothing] items := [4, 78, 2, 3, 56, -1, 3] for item in items insert root, item walk root print "" print "Tree:" print root
procedure insert node, item if node = nothing pass else if item < node@1 if node@2 = nothing node@2 := [item, nothing, nothing] else insert node@2, item else if node@3 = nothing node@3 := [item, nothing, nothing] else insert node@3, item procedure walk node if node = nothing pass else walk node@2 print node@1 walk node@3 procedure show node if node = nothing pass else write "(" write node@1 if node@2 = nothing pass else write " L:" show node@2 if node@3 = nothing pass else write " R:" show node@3 write ")" root := ["j", nothing, nothing] items := ["a", "z", "x", "c", "b"] for item in items insert root, item walk root print "" print "Tree:" print root print "TREE:" show root
procedure bubble_sort xs swapped := true while swapped swapped := false for i from 2 to #xs if xs@(i - 1) > xs@i swap xs, i - 1, i swapped := true procedure swap xs, i, j temp := xs@i xs@i := xs@j xs@j := temp xs := [3, 4, 5, 1, 2] bubble_sort xs print xs
procedure p x if x > 200 print x + "" print "p " ~ x q x + 1 procedure q x print "q " ~ x p x + 1 for n from 1 to 10 print n p 1
print 3/4 print (3 div 4) print 5/4 print 5 div 4 print 5 mod 4 print 720 mod 360 print -1 mod 5 print 0.5 mod 0.75
words := ["foobar", "buzz bar", "barz bar"] for word in words for char in word write char ~ " " char := read write "\n"
procedure p x print "p " ~ x q x + 1 procedure q x print "q " ~ x p x + 1 p 1
h := 30 k := 20 r := 15 for y from 1 to k div 4 + r for x from 1 to h + 2*r if ((x - h)^2 + ((2*y) - k)^2) > r^2 write "." else write "#" print ""
print not(1 > 4) print not(false) print not(true) print not(4)
for i from 1 to 4 print "#" ~ i ~ ": " ~ read
lines := 40 cols := 70 bg := "." pixel := "#" point := [cols div 2, lines div 2] steps := 4000 ; Returns the midpoint of p1 and p2 function midpoint(p1, p2) x1 := p1@1 y1 := p1@2 x2 := p2@1 y2 := p2@2 xm := (x1 + x2) div 2 ym := (y1 + y2) div 2 return [xm, ym] vertices := [] append vertices, [cols div 2, 1] append vertices, [1, lines] append vertices, [cols, lines] print "Initializing screen, please wait..." print "" screen := [] for m from 1 to lines line := [] for n from 1 to cols append line, bg append screen, line ; Printing during this loop makes it slower, ; but otherwise it might seem like LatteScript froze. for n from 1 to steps vertex := vertices@random(1, 3) point := midpoint(vertex, point) screen@(point@2)@(point@1) := pixel print "Step #" ~ n ~ " of " ~ steps clear for line in screen for char in line write char print ""
size := 30 for m from 1 to 2 * size by 2 q := (2 * size - m) div 2 for n from 1 to q write " " for n from 1 to m write "*" print ""