Thursday, June 14, 2012

Calculating roots of a parabola with Fuzuli Programming Language

Fuzuli has its core libraries inhereted from C++ standard libraries. That is why adapting to Fuzuli does not require climbing a sharp learning curve. Only the requirement is to have a habitat of parenthesis syntax.

In this article, we write a small script for calculating roots of a parabola. This script includes two Fuzuli packages, io and math. This packages are required for readline, exit and sqrt functions. The remaining part of this script uses the internal functions of Fuzuli.

A parabola is a function in form of ax^2 + bx + c where a!=0. Delta, which is calculated as b^2 - 4ac is an indicator of number of real roots. If delta is less than zero, the parabola has no real roots. If delta is zero, all of the roots shares the same value. If delta is greater than zero, there are two distinct roots. This is not our subject now and it is well described at the Wikipedia.

Before coding, you can manually install the Fuzuli for Linux or Windows systems. Please visit the Fuzuli Official Web Page and Google Code page for installing instructions.
Our Fuzuli script is shown below:


(require "/usr/lib/fuzuli/nfl/io.nfl")
(require "/usr/lib/fuzuli/nfl/math.nfl")

(print "Please enter a b and c for parabola ax^2 + bx + c\n")
(print "Enter a:")
(let a (readline))

(print "Enter b:")
(let b (readline))


(print "Enter c:")
(let c (readline))

(let delta (- (pow b 2) (* 4 (* a c))))
(print "Delta is " delta "\n")

(if (< delta 0.0)
 (block
  (print "No real roots exist\n")
  (exit 0)
 )
)

(if (= delta 0.0)
 (print "There are two equal roots\n")
)

(if (> delta 0.0)
 (print "There are two different real roots\n")
)

(let x1 (/ (+ (* -1 b) (sqrt delta)) (* 2 a)))
(let x2 (/ (- (* -1 b) (sqrt delta)) (* 2 a)))
(print "x1=" x1 " and x2=" x2 "\n")
I write the codes above in the file "delta.fzl". You can run it by typing

#fuzuli delta.fzl


and program asks for values of a, b and c. The output screen is similar to the output given below:


Have fun with Fuzuli!

Wednesday, June 13, 2012

Extracting links from web pages using Fuzuli

We have too much written about Fuzuli and its core components but we didn't publish any real world applications run on it.

Fuzuli, our new general purpose programming language and interpreter, is first introduced in Practical Code Solutions blog and has the official web page http://www.fuzuliproject.org.

Here we have an example of extracting links from an HTTP connection. Program asks for a domain name. The default one is amazon.com and processed by just pressing enter key. Then program sends an HTTP GET request to the server and reads the content. After collecting all of the content, program starts to parse HTML codes and shows the tags start with an A tag.  The Fuzuli code is shown below:


# Loading required packages
(require "/usr/lib/fuzuli/nfl/io.nfl")
(require "/usr/lib/fuzuli/nfl/string.nfl")
(require "/usr/lib/fuzuli/nfl/net.nfl")

# Getting a domain name from user.
(puts "Please give a domain (for default just type enter):")
(let word (readline))

# If user did not type anything
# set the default page to amazon.com
(if (< (strlen word) 3)
   (let word "amazon.com")
)
(print "Doing " word "\n")


# Open a socket connection to host
(print "Connecting " word "\n")
(let socket (fsockopen word 80))

# Sending HTTP Request
(print "Sending request\n")
(fsockwrite socket (strcat (list "GET /\n\n")))

# Reading html content
(print "Retrieving result\n")
(def htmllist LIST)
(while 1
   (let c (fsockread socket 1))
   (if (= (typeof c) NULL) (break))
   (append htmllist c)
)
# Closing socket
(fsockclose socket)

(print "Constucting string\n")

(let html (strcat htmllist))
(let len (strlen html))
(print len " bytes read.\n")

(def part STRING)
(def i INTEGER)

# Parsing loaded content
(for (let i 0) (< i len) (inc i)
   (let part (substr html i (+ i 7)))
   (if (= part "<a href")
      (block
         (print "link found: \n")
         (while (!= part "</a>")
            (let part (substr html i (+ i 4)))
            (print (substr html i (+ i 1)))
            (inc i)
         )
         (print part "\n")
      )
   )
) 


The example given above combines variable definitions and scopes, loops, sockets and basic io. Please get more detailed information about the keywords, commands and functions using Fuzuli's documentation site.

PHP and Microsoft Access Database Connection

We all know that MySQL database connection is the best way for PHP. This make us so strong during coding. But what I have to say is that is not the only way. We have some opinions about connecting to databases. These may be between PHP&Access, PHP&Sqlite and PHP&PostgreSQL etc. But here, we interested in the first one, PHP&Access.

Access is one of the most popular databases in the world. It was made by Microsoft Corporation. You can find here more information about Access. A person who uses the Access can create, update, delete, etc tables on databases without using SQL.  That's why we can see easily how important  the interface is. In this respect this is so simple and useful.


An Example on Access : Getting data from access


<?php
$conn = new COM("ADODB.Connection") or die("ADODB Oops!");
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\Users\UserName\Desktop\ MyDatabaseFile .mdb");
$data = $conn->Execute("SELECT * FROM myTable ORDER BY users ASC");

print "<TABLE border='1'><TR><TD colspan='6'>DATA</TD><TR>";
while (!$data->EOF)
{
print "<tr>";
print "<td>" . $ data ->Fields[0]->value . " </td>";
print "<td>" . $ data ->Fields[1]->value . " </td>";
print "</tr>";
$ data ->MoveNext();
}
echo "</TABLE>";


Just save code above as access.php and run it. It's going to be like the screen belown.
Screen View
If you try to figure that out, codes belown will be helpful for you.

$conn = new COM("ADODB.Connection") or die("ADODB Opps!");
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\Users\UserName\Desktop\MyDatabaseFile.mdb");

We try to connect access database with php here.

odbc_connect(‘dsn name’, ‘user name’, ‘password’);

There is a problem with this! When you look at the code belown, can realize user_name and password field. What are these ones? How can we build on these? That's the point on this article actually. Just go to the localhost and this page:

phpinfo.php


<?php
Phpinfo();
?>



When run phpinfo.php file, need look into ODBC properties

ODBC with phpinfo.php
You must implement your user name and password for working on it. By the way, it is easier to make this operations with codes. That's why i show you as code, not screen views.

Well, let's code then :)

$conn = new COM("ADODB.Connection") or die("ADODB Opps!");
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\Users\UserName\Desktop\MyDatabaseFile.mdb");

$data = $conn->Execute("SELECT * FROM myTable ORDER BY users ASC");

print "<TABLE border='1'><TR><TD colspan='6'>DATA</TD><TR>";
while (!$data->EOF)
{
print "<tr>";
print "<td>" . $ data ->Fields[0]->value . " </td>";
print "<td>" . $ data ->Fields[1]->value . " </td>";
print "</tr>";
$ data ->MoveNext();
}
echo "</TABLE>";

This is my result page with php, html and sql.

See you guys next article!

Sunday, June 10, 2012

Installing Fuzuli for Windows

Well, it was difficult to shut down our Linux installed machines and start up Windows after a long time interval. Unfortunately, there are many Windows users around the world that want to try out Fuzuli, our new interpreter which is first introduced in Practical Code Solutions blog.

We compiled Fuzuli using GNU C++ compiler and prepared some Linux packages which are ready to download at Google Code page. In order to compile it Windows, we had to use GNU for Windows, namely CYGWIN. Finally, we have a running copy at hand and it is ready to download in page Download Packages. Installing Fuzuli in Windows is such an easy task. Just follow the required steps and start running and testing Fuzuli.

  1. Go to the page http://code.google.com/p/fuzuli/downloads/list and download Fuzuli for Windows.
  2. The current zip file is fuzuli-win_0.1-4.zip but it depends on the current release. It will have a new name in the future but in the same pattern.
  3. Extract the zip file, for example in C:\, so your Fuzuli folder becomes c:\fuzuli-win_0.1-4.

 Okay, by now on, you will have installed Fuzuli! Let's test it using following steps:
  1. Click button. Select run and type "cmd". Press enter. This will open a terminal screen.



  2. Type "cd C:\fuzuli-win_0.1-4" and type enter. This will get you in the Fuzuli folder. 





  3. Type "fuzuli" and type enter. 
 You would see an output like this:


Fuzuli build Jun  9 2012 12:59:18
usage:
 fuzuli source
 fuzuli --repl



If you see the output shown above, you probably installed Fuzuli successfuly. Lets try a program. There is fzl file shipped with Fuzuli with name fibonacci.fzl. This file is a text file and can be edited a text editor like Notepad or Notepad++. You can run it by typing



fuzuli fibonacci.fzl


and if there is not any problem, the output should be like this:

PASSED!
PASSED!
PASSED!
PASSED!
PASSED!


The output shown above proofs that Fuzuli is ready. You can create your own fzl files and run them in a similar way.

Ok. Let write a world classic, Hello World!. Open your favorite text editor, it is Notepad++ if I work with Windows.

 
Now save it with name "hello.fzl" in some folder. Do not forget where you saved the file. I saved my hello.fzl in directory D:\myfuzuli so it is D:\myfuzuli\hello.fzl







 Okay, see you later in next article!