# Unicon Introduction

### by "Blag" - Senior Developer Evangelist ## Who uses Unicon?

• The people who developed it...
• Some enthusiastic people...

## Starting out...

### ```				```
# This is a comment
``````

## Variables

### Although you need to specify "local" for most variabes

```				```
procedure main()

local myVar

myVar := "Blag"

write(myVar)

myVar := 42

write(myVar)

end
``````

## Success and Failure

### If something fails, nothing happens.

```				```
procedure main()

local myVar

myVar := 5

if myVar > 10 then

write("Biggger")

end
``````

## Loops

### Unicon provides a couple of different loop constructions

```				```
procedure main()

local i

every i := 1 to 5 do

write(i)

while i < 10 do {

write(i)

i +:= 1

}

repeat{

write(i)

i +:= 1

if(i > 15) then break

}

end
``````

## Functions

### Function are very important...that's true for every programming language...

```				```
procedure sum(a, b)

return a + b

end

procedure main()

write(sum(2, 30))

end
``````

## Arrays

### There's no arrays...but Lists, Records and Tables...

```				```
procedure main()

local myList, i

myList := ["Blag", 42, "Unicon"]

myList |||:= ["AFI"]

every i := 1 to *myList do

write(myList[i])

end
``````

## Tables

### Tables allow us to define our own indices

```				```
procedure main()

local tab

tab := table()

tab["a"] := 1

tab["b"] := 1

tab["a"] +:= 1

write(tab["a"])

end
``````

## Strings

### We have some nice functions for string manipulation.

```				```
procedure main()

write("This is " || "Unicon")

write(repl("Blag",3))

write(map("Blag","a","A"))

write(reverse("Blag"))

write(center("Blag", 10, "*"))

end
``````

## Co-Expressions

### Captures an expression so that it can be resumed at anytime and place.

```				```
procedure main()

local bands

bands := create("AFI"|"Sex Pistols"|"DHC")

while write(@bands)

end
``````

## Fibonacci list sequence

### This will create an exec that we can call like this ./nameofexec

```				```
procedure fib(num, a, b)

local result

result := ""

if a > 0 & num > 1 then

result := result || " " || (a + b) ||

fib(num - 1, a + b, a)

else if a = 0 then

result := a || " " || b || " " ||

(a + b) || fib(num - 1, a + b, b)

return result

end
``````
```				```
procedure main()

local num

write("Enter a number: ")

write(" ")

write(fib(num, 0, 1))
end
``````

## Making an LCD Number App

### This is my favorite application of all time...

```				```
procedure main()

local leds, num, line1, line2, line3

local i, j

i := 1

j := 2

line1 := ""

line2 := ""

line3 := ""

leds := table()

leds["0"] := " _  ,| | ,|_| "

leds["1"] := "    ,|   ,|   "

leds["2"] := " _  , _| ,|_  "

leds["3"] := " _  , _| , _| "

leds["4"] := "    ,|_| ,  | "

``````
```				```
leds["5"] := " _  ,|_  , _| "

leds["6"] := " _  ,|_  ,|_| "

leds["7"] := " _  ,  | ,  | "

leds["8"] := " _  ,|_| ,|_| "

leds["9"] := " _  ,|_| , _| "

write("Enter a number: ")

while i <= *num do{

line1 ||:= leds[num[i:j]][1:4]

line2 ||:= leds[num[i:j]][6:9]

line3 ||:= leds[num[i:j]][11:14]

i +:= 1

j +:= 1

}
``````
```				```
write(line1)

write(line2)

write(line3)

end
``````

## Decimal to Romans

### This will include some nice commands...

```				```
procedure main()

local roman, x, num, result, sort_roman

result := ""

roman := table()

sort_roman := []

roman := "M"

roman := "CM"

roman := "D"

roman := "CD"

roman := "C"

roman := "XC"

roman := "L"

roman := "XL"
``````
```				```
roman := "X"

roman := "IX"

roman := "V"

roman := "I"

every x := key(roman) do

sort_roman |||:= [x]

sort_roman := sort(sort_roman)

write("Enter a number: ")

``````
```				```
while num > 0 do{

x := *sort_roman

while x > 0 do{

if num >= sort_roman[x] then{

result ||:= roman[sort_roman[x]]

num -:= sort_roman[x]

break

}

x -:= 1

}

}

write(" ")

write(result)

end
``````

## Random Names

### We will measure the runtime.

```				```

procedure main()

local names, last_names, full_names, i

randomize()

names := ["Anne","Gigi","Blag","Juergen","Marek",

"Ingo","Lars","Julia", "Danielle",

"Rocky","Julien","Uwe","Myles","Mike",

"Steven","Fanny"]

"Kowalkiewicz","Sauerzapf","Karg",

"Satsuta","Keene","Ongkowidjojo",

"Vayssiere","Kylau","Fenlon",

"Flynn","Taylor","Tan"]
``````
```				```
full_names := []

every i := 1 to 100000 do

full_names := full_names ||| [names[?16]

|| " " || last_names[?16]]

write("Time taken: ", &time, " milliseconds")

end
``````

## Counting Letters

### "This is a text file that we're going to read using Unicon"

```				```
procedure main()

local intext, outtext, letters, x, i , j, letter

letters := table()

letter := ""

i := 1

j := 2

stop("Cannot open file")

``````
```				```
every x := 1 to *outtext do{

letter := outtext[i:j]

i +:= 1

j +:= 1

if \letters[letter] then letters[letter] +:= 1

else insert(letters,letter,1)

}

every x := key(letters) do

write(x," : ",letters[x])

end
``````

## Memory Databases

### We can use files to create quick access DB applications

```				```

procedure main()

local db, dbf

db := table()

db["Blag"] := 42

db["Cheko"] := 103

dbf := open("db.dat","w")

xencode(db, dbf)

close(dbf)

dbf := open("db.dat")

db := xdecode(dbf)

close(dbf)

write(db["Blag"])

end
``````

### We can easily create threads

```				```
procedure main()

local L

L := []

every i := !10 do put (L, thread write("Hello World!, I'm a thread", i))

every wait(!L)

write("main: Done")

end
``````

## Classes

### Classes are what you find in other OO Languages

```				```
class person(name)

method setName(sName)

name := sName

end

method writeName()

write(name)

end

end

procedure main()

local Blag

Blag := person()

Blag.setName("Blag")

Blag.writeName()

end
``````