# Dragon Introduction

## Comments

### Comments are very simple

```				```
//This is a comment

/*This is

a comment as well*/
``````

## Printing Output

```				```
show("Hello World") //No carriage return

showln("Hello World") //Carriage return or new line
``````

## Variables

### Variables don't need a type as the language is dynamic.

```				```
x = 5

x = "This is a string"

x = ["Hello", 5]
``````

## Conditionals

### The good IF-ELSE always prevail

```				```
select "types"

a = 3

if a > 0

showln(string(a) + " is bigger than zero")

else

showln(string(a) + " is way to small")

//3 is bigger than zero
``````

### Match is a good way to have an IF-ELSE

```				```
a = "3"

match a {

case "1": {showln("One")}

case "2": {showln("Two")}

case "3": {showln("Three")}

case _: {showln("Out of range")}

}

//Three
``````

## Loops

### We have many option when it comes to loops...

```				```
for(i = 0, i < 6, i++){

showln i

}
``````

```				```
a = [1,2,3,4,5]

for x : a {

showln x

}
``````

```				```
i = 0

while i < 5 {

showln i

i++

}
``````

```				```
i = 0

do {

showln i

i++

}while i < 5
``````

## Functions

### Return one value and can have default parameters

```				```
func say_greetings(name = "Blag"){

greet = "Hello " + name

return greet

}

show(say_greetings("Blag"))

show("\n")

show(say_greetings())

//Hello Blag

//Hello Blag
``````

## Arrays and Maps

```				```
a = [1,2,3]

show(a[0])
``````

```				```
select "std"

names = newarray(2)

names[0] = "Blag"

names[1] = "Oliver"

show(length(names))
``````

```				```
m = {"one":1, "two":2}

showln(m["one"])

n = {1:"one", 2:"two"}

show(n[1])
``````

## Classes

### We can create classes and objects

```				```
class person{

func greet(name){

showln("Hello " + name + "!")

}

}

blag = new person()

blag.greet("Oliver")

//Hello Oliver
``````

## Modules

### Most of the functions in Dragon are handled in modules

```				```
select "GUI"

win = newWindow("My Window")

win.setSize(400,300)

win.setVisible()

msg = newText(text = "Hello World")

win.add(msg)
``````

```				```
select "http"

select "json"

response = ""

s_json = ""

http("https://www.metaweather.com/api/location/

search/?query=toronto","GET",

func(v){response = v})

s_json = jsondecode(response)

showln(s_json[0]["latt_long"])
``````

```				```
select "graphic"

select "std"

window("graphics", 800, 600)

x = 150; y = 50; r = 500

for(i=0,i<5,i++){

color(rand(0,255),rand(0,255),rand(0,255))

oval(x, y, r, r)

x = x + 50

y = y + 50

r = r - 100

}

repaint()
``````

## Fibonacci list sequence

### Create a file and call it fibonacci.dgn

```				```
select "std"

select "types"

func fib(num, a, b){

result = ""

if a > 0 && num > 1

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

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

else if a == 0

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

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

return result

}
``````
```				```
show "Enter a number: "

num = readln()

showln(fib(num,0,1))
``````

## Making an LCD Number App

### Create the app and name it LED_Numbers.dgn

```				```
select "std"

select "types"

leds = {0:" _  ,| | ,|_| ",

1:"  ,| ,| ",

2:" _  , _| ,|_  ",

3:"_  ,_| ,_| ",

4:"    ,|_| ,  | ",

5:" _  ,|_  , _| ",

6:" _  ,|_  ,|_| ",

7:"_   , |  , |  ",

8:" _  ,|_| ,|_| ",

9:" _  ,|_| , _| "}
``````
```				```
show "Enter a number: "

num = readln()

digits = split(num,"")

for(i=0,i<3,i++){

for x: digits{

line = split(leds[int(x)],",")

show(line[i])

}

showln("")

}

showln("")
``````

## Decimal to Romans

### Create the app and name it decimal_to_roman.dgn

```				```
select "std"

select "types"

Roman_Table = {1000: "M", 900: "CM", 500: "D",

400: "CD", 100: "C", 90: "XC",

50: "L", 40: "XL", 10: "X",

9: "IX", 5: "V", 4: "IV", 1: "I"}

keys = sort(arrayKeys(Roman_Table))
``````
```				```
func roman_number(number){

result = ""

while number > 0{

for(i=length(keys) - 1,i>-1,i--){

if number >= keys[i]{

result = result + Roman_Table[keys[i]]

number = number - keys[i]

break

}

}

}

return result

}
``````
```				```
show "Enter a number: "

num = readln()

showln(roman_number(int(num)))
``````

## Random Names

### Create the app and name it random_names.dgn

```				```
select "std"

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

"Lars","Julia", "Danielle","Rocky","Julien",

"Uwe","Myles","Mike","Steven","Fanny"]

last_names = ["Hardy","Read","Tejada","Schmerder",

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

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

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

"Flynn","Taylor","Tan"]

full_names = newarray(1000000)
``````
```				```
for(i=0,i<1000000,i++){

full_names[i] = names[rand(from=0,to=16)] + " " +

last_names[rand(from=0,to=16)]

}
``````

## Counting Letters

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

```				```
select "std"

select "files"

select "functional"

select "types"

file = fopen("readme.txt")

line = readLine(file)

letters = {}

for(i=0,i<length(line),i++){

letter = toChar(charAt(line,i))

if arrayKeyExists(letter,letters) == 0{

letters = letters + {letter:1}

}
``````
```				```
else{

letters[letter] = letters[letter] + 1

}

}

for key, value : letters{

showln(key + " => " + value)

}
``````