~tim/lydia

ca90e36dbb06afbfbed08c92bb07bf71deeb880a — Tim Morgan 7 years ago ac49101
Move examples to examples directory.
M README.md => README.md +1 -1
@@ 33,7 33,7 @@ The syntax is very terse, as you can see below:
    (f2 2 3) + (f1) # 12
    2 ^ 3           # 8

    # see example.lid for other available functions
    # see the examples directory for more

There's a few things to note:


D example.lid => example.lid +0 -78
@@ 1,78 0,0 @@
# assignment
x = 10

# print to stdout
out 8 * 2
out x

# functions
foo = { 2 * 3 }
bar = [x y] { z = x * x
              z + y }

# calling functions
out (foo)
out (bar 3 9)
out 9

# recursive functions: factorial
fact = [x] { if x == 1,
                x,
                { x * (fact x - 1) } }
out (fact 20)

# recursive functions: factorial (tail-call optimized)
fact2 = [x] { f = [x a] { if x == 1,
                             a,
                             { (f x - 1, a * x) } }
              f x 1 }
out (fact2 20)

# recursive functions: fibonacci sequence
fib = [n] { if n < 2,
               n,
               { (fib n - 1) + (fib n - 2) } }

# map over a list
fibs = map [1 2 3 4 5 6 7 8 9 10 11 12],
           [n] { fib n }
out (join fibs ", ")

# iterative functions: fibonacci sequence
fib = [x] { curr = 0
            succ = 1
            for (1 .. x),
                [n] { s = curr + succ
                      curr = succ
                      succ = s }
            curr }

# map over a list
fibs = map [1 2 3 4 5 6 7 8 9 10 11 12],
           [n] { fib n }
out (join fibs ", ")

# filter out a list
fibs-over-5 = filter fibs [n] { n > 5 }
out (join fibs-over-5 ", ")

# for loop
list = [{ out "a" } { out "b" } { out "c" }]
for list [x] { (x) }

# while loop
n = 10
while { n > 0 },
      { out n
        n = n - 1 }

# if/elsif/else
if [x == 1 { out "one" }
    x == 2 { out "two" }
    x == 3 { out "three" }
    { out "something else" }]

# script arguments
out "this script: " -script
out "this file: " -filename
out "args: " (join -args, " ")

A examples/args.lid => examples/args.lid +4 -0
@@ 0,0 1,4 @@
# script arguments
out "this script: " -script
out "this file: " -filename
out "args: " (join -args, " ")

A examples/fact.lid => examples/fact.lid +7 -0
@@ 0,0 1,7 @@
# recursive version of the factorial function
# NOT tail-call optimized

fact = [x] { if x == 1,
                x,
                { x * (fact x - 1) } }
out (fact 20)

A examples/fib.iter.lid => examples/fib.iter.lid +13 -0
@@ 0,0 1,13 @@
# iterative version of the fibonacci sequence

fib = [x] { curr = 0
            succ = 1
            for (1 .. x),
                [n] { s = curr + succ
                      curr = succ
                      succ = s }
            curr }

fibs = map [1 2 3 4 5 6 7 8 9 10 11 12] fib

out (join fibs ", ")

A examples/fib.recur.lid => examples/fib.recur.lid +10 -0
@@ 0,0 1,10 @@
# recursive version of the fibonacci sequence
# NOT tail-call optimized

fib = [n] { if n < 2,
               n,
               { (fib n - 1) + (fib n - 2) } }

fibs = map [1 2 3 4 5 6 7 8 9 10 11 12] fib

out (join fibs ", ")

A examples/fib.tail.lid => examples/fib.tail.lid +12 -0
@@ 0,0 1,12 @@
# recursive version of the fibonacci sequence
# tail-call optimized

fib = [x] { f = [i c n] { if i == 0,
                             c,
                             { f (i - 1) n (c + n) } }
            f x 0 1 }


fibs = map [1 2 3 4 5 6 7 8 9 10 11 12] fib

out (join fibs ", ")

A examples/if.lid => examples/if.lid +14 -0
@@ 0,0 1,14 @@
x = 4

# simple if
if x == 4 { out "four" }

# if/else
if x == 4 { out "four" } { out "not four" }

# if/elsif/else
# to do multiple conditions, wrap all the args in a list
if [x == 1 { out "one" }
    x == 2 { out "two" }
    x == 3 { out "three" }
    { out "something else" }]

A examples/lists.lid => examples/lists.lid +5 -0
@@ 0,0 1,5 @@
fibs = [1 1 2 3 5 8 13 21 34 55 89 144]

# filter out a list
fibs-over-5 = filter fibs [n] { n > 5 }
out fibs-over-5

A examples/while.lid => examples/while.lid +6 -0
@@ 0,0 1,6 @@
# while loop

n = 10
while { n > 0 },
      { out n
        n -= 1 }