Falcon Introduction

by "Blag" - Senior Developer Evangelist

Return to Geeky Thursday

What is Falcon?


Falcon is an open source, multi-paradigm programming language.


It supports procedural, object-oriented, prototype-based, functional, tabular and message passing.


It first appeared 12 years ago.


How to install Falcon?


It should be available already on Linux based systems...


Otherwise you can grab it from here Falcon Releases

Who uses Falcon?


  • Default scripting language for now dissapeared AuroraUX Operating System.
  • Kross -> Scripting framework that provides full Python, Ruby and KDE JavaScript scripting support
  • BlastWave -> Web Interface with Falcon
  • Appcelerator -> Falcon Module for Titanium
  • XChat -> Plugin for XChat

Starting out...


We can call Falcon as an interactive shell...


Basic Concepts


One line comments are // and multiple lines comments are /* and */.


Precedence must be enforced by parenthesis


				
printl(2*3+5)

11


printl(2*(3+5))

16
				

Basic Concepts


Printing on the screen is easy...


				
text = "This is Falcon!"

: "This is Falcon!"


print(text) 



This is Falcon!
				

Variables can have multi-assignments...


				
a, b, c = 1, 2, 3

: 3


print(a,b,c)


123
				

Data Structures


Arrays

				
array = ["Blag", 37, "d-shop"]

: [ "Blag", 37, "d-shop"]



array = "Blag", 37, "d-shop"

: [ "Blag", 37, "d-shop"]



array = [["Blag"], [37], ["d-shop"]]

: [ [ "Blag"], [37], ["d-shop"]]
				

				
array = ["Blag", 37, "d-shop"]

: [ "Blag", 37, "d-shop"]



array += "Silicon Valley"  //Add element to array

: [ "Blag", 37, "d-shop", "Silicon Valley"]



array -= 37  //Removes element from array

: [ "Blag", "d-shop", "Silicon Valley"]



printl(array[0])  //First element from array


Blag


printl(array[-1])  //Last element from array


Silicon Valley
				

				
array = [1:6]  //Creates an array range

: [1:6]




for i in array

   printl(i)
	
end



1

2

3

4

5
				

				
array = arrayBuffer(3)  //Pre-allocates an array

: [ Nil, Nil, Nil]


array[0] = 1

array[1] = 2

array[3] = 3
				

This pre-allocation saves memory and performance...


Dictionary

				
dict = ["A" => 1, "B" => 2]

: ["A" => 1, "B" => 2]



printl(dict["B"])


2



dict += ["C" => 3]  //Add a new element

: ["A" => 1, "B" => 2, "C" => 3 ]



dic += ["B" => 5]  //Removes an element

: ["A" => 1, "B" => 5, "C" => 3 ]
				

Lists

				
lst = List("a","b","c")

: List(){ Sequence = Class Sequence, }



printl(lst.len()) //Length of the list


3


printl(lst.front()) //Gets the last element


a


printl(lst.back())  //Gets the first element


c

				
				
lst.pushFront("d") //At the beginning of the list



lst.push("e") //At the end of the list



for i in lst

   printl(l)

end



d

a

b

c

e
				

Functions

Functions must always return a value...


				
function Hello(name)

    return ("Hello " + name)

end



printl(Hello("Blag"))



Hello Blag

				

Function Closures

A function inside a function. The value of the innerfunction is pass to the main function

				
function Hello(name)

    greet = function(message)
    
        return "Hello " + name + " " + message
        
    end
    
    return greet
    
end



GreetMe = Hello("Blag")

printl(GreetMe("welcome to Falcon!"))



Hello Blag welcome to Falcon!
				

Functional Programming

Falcon support Functional Programming with some useful functions

				
Values = map( { x => x * x }, [1, 2, 3] )



for val in Values

    printl(val)

end



1

4

9
				
				
function GreaterTen( x )


	if x < 10: return oob(nil)

	return x

end



Values = filter(GreaterTen, [1,2,3,11,12,13,4,5,6])



for val in Values

	printl(val)

end


11

12

13
				

Fibonacci List


Finally...we're going to make our first application...


So grab your favorite text editor and get ready...


Name your file "Fibonacci.fal"


				
function fib(num,a,b)

    result = ""
	
    if a > 0 and num > 1
	
        result = result + (a+b) + " " + 
        
                 fib(num-1,a+b,a)
		
    elif a == 0
	
        result = "" + a + " " + b + " " + 
         
                 (a+b) + " " + fib(num-1,a+b,b)
		
    end
	
    return result
	
end
				
				
print("Enter a number: ")

num = int( input() )

print(fib(num,0,1))

printl("")
				

Open the Terminal and go to your source code folder...

falcon "Name_of_File.fal"


When we run it we're going to see...

Making an LED Number App


This is one of my favorite codes of all time...


Name your file "LED_Numbers.fal"


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

         "1" => ["  ","| ","| "],
		 
         "2" => [" _  "," _| ","|_  "],
		 
         "3" => ["_  ","_| ","_| "],
		 
         "4" => ["    ","|_| ","  | "],
		 
         "5" => [" _  ","|_  "," _| "],
		 
         "6" => [" _  ","|_  ","|_| "],
		 
         "7" => ["_   "," |  "," |  "],
		 
         "8" => [" _  ","|_| ","|_| "],
		 
         "9" => [" _  ","|_| "," _| "]]
				
				
print("Enter a number: ")

number = input()

len = number.len()

for i in [0:3]

	for j in [0:len]

		print(leds[number[j]][i])

	end

	printl("")

end
				

When we run it we're going to see...


Random Names


This App will generate 100,000 random names using two 16 elements arrays


We will measure the runtime


Name your file "Random_Names.fal"


				
randomSeed( seconds() )



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 = ""

full_names_array = []



for i in [0:100000]

	item_names = randomPick([0,1,2,3,4,5,6,7,8,9,
	
	                         10,11,12,13,14,15])

	item_last_names = randomPick([0,1,2,3,4,5,6,7,
	
	                              8,9,10,11,12,13,
	                              
	                              14,15])

	full_names = names[item_names] + " " + 
	
	             last_names[item_last_names]

	full_names_array += full_names

end
			

When we run it we're going to see...

How this behaves in Python?

And in Julia?

Decimal to Romans


This App will create a Roman Numeral based on a decimal number


This will include some nice commands...


Name your file "Decimal_to_Roman.fal"


				
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 = dictKeys(Roman_Table)
			
				
function Roman_Number(number)

result = ""

    while number > 0

        for key in [-1:-13]

            if number >= Keys[key]

                result += Roman_Table[Keys[key]]

                number -= Keys[key]

                break

            end

        end

    end

    return result

end
			
				
print("Enter a number: ")

num = int( input() )

print(Roman_Number(num))

printl("")
				

When we run it we're going to see...


Count Letters


In this example we're going to read a file and count how many time a letter appears...


Call your file "countletters.fal" (all in lowercase).


Create a file called "readme.txt" with the following text...


"This is a text file that we're going to read it using Falcon"


				
letters = ""



file = InputStream("readme.txt") 

t = Tokenizer( seps|" ", options|Tokenizer.groupsep )

t.parse(file)



for token in t

    letters += token

end
			
				
counter = [=>]

strarr = strSplit( letters, "" )

for word in strarr

    if word in counter

        ++counter[word]

    else

        counter[word] = 1

    end

end



for key, value in counter

	printl(@ "$key\t$value")

end
			

When we run it we're going to see...


That's it for now


Falcon's documentation is not finished...


But still there's a lot of thing to discover and try -:)


Contact Information


Blag --> blag@blagarts.com

@Blag on Twitter

Go back home...