Pondering Prolog

Recently, I saw a talk about Prolog and the speaker was trying to make a connection between web development and this language. Prolog has facts, rules, and queries. The connection that was being made was between queries and SQL. I learned a little about the language and got the examples to run on my machine using SWI-Prolog. Though the point was tenuous, let me give my take on what we can see from the languages and queries.In Prolog, facts (clauses) are basically data or variables. Rules (also clauses) are conditionals such as those which check for a certain state of boolean logic. Queries call upon the rules to check the facts.

For example, let’s say the facts are named Homer, Bart, and Lisa that like a soda or energy drink. The queries act upon the rules which are applied to compare whether they have the same taste in drinks.

This could be shown like so:

likes(homer,soda).

likes(bart,energy drink).

likes(lisa,soda).

taste(X, Y) :- likes(X, Z), likes(Y, Z).

After loading the script in Prolog, we can query if these people have similar tastes based on their drink of choice:

taste(homer,bart).

false.

taste(homer,lisa).

true.

Homer (variable X) and Bart (variable Y) do not like the same drink (variable Z) based on the false result due to that facts that Homer likes soda and Bart likes energy drink. The rule above checks to see if the variables of X and Y each have the variable Z in common. The facts (variables) are like the columns of a database table.

Now let’s look at an MySQL table to see if we can get a similar result using an SQL query:

The table is named simpsons which contains First_ Name and Drinks columns. The names and drinks data would be inserted into the respective columns that we used from the Prolog example above.

We could then setup a query like so: 

SELECT * FROM simpsons WHERE drinks=’soda’;

This should give us the result from the simpsons table like:

+——————+————-+

|First_Name | Drinks  |

+——————-+————-+

| homer          |  soda    |

+——————–+————+

| lisa                 | soda      |

+———————+————-+

As you see we get a table that shows all rows that have soda in common.

If we set up a query in SQL like this:

SELECT * FROM simpsons WHERE drinks !=’soda’;

This should give us the result from the simpsons table like:

+——————-+———————-+

| First_Name |Drinks              |

+——————–+———————-+

| bart               |  energy drink |

+———————+———————-+

As you see we get a table that shows all rows that do not have soda in common and only Bart with an energy drink is displayed.

This is not a pure true or false output like the results of the Prolog query we made earlier. Why? Prolog is a 2 value logic (2VL) system (true or false), yet SQL is a 3 value logic (3VL) system comprised of true, false, and null values. A true result will display an output; but if you check for a value (like the above != query) in a column of a table and do not get an output (an empty result set), then it is like a false result.  A null value is also a third possibility in a boolean test of a database.

If we added Marge with a null value in the Drink column to the table and we ran a query of :

SELECT * FROM simpsons WHERE drinks !=’soda’;

This will give us the result from the simpsons table like:

+——————-+———————-+

| First_Name |Drinks              |

+——————–+———————-+

| bart               |  energy drink |

+——————–+———————-+

| marge           | null                  |

+——————–+———————-+

The above data set shows the possibility of a null value in the table making a test for a pure boolean condition more difficult and requiring more manipulation of the query to get the null to be seen as a false result (depending on the particular databases’ query language).

These two language examples are not exactly equivalent due to the constraints of the logic value systems; though you may notice that whether using Prolog or SQL, we can get to what we want to know from the data of who likes soda and who doesn’t. I hope this shows a way that queries from a language that is primarily used in artificial intelligence and one that is used primarily for databases to get a similar result from the same data. Happy coding!

Working with WordPress

I have recently changed the color of the background on the theme to a light blue to make it easier on the eyes. All changes are made through my mobile phone and all posts are placed through the WordPress application. I just installed the Bitnami WordPress plug-in for XAMPP on the Windows XP development machine. Now I have a WordPress development site. Success! This will allow me to explore more customization of the development site before I change it on my online one. Also, I will be able to explore themes, security, and other features in the future. More to come.

Learning markdown

Markdown is apparently the standard in Github repositories for readme files and is used often by developers. I completed the Lynda.com tutorial on Learning Markdown, but did not work with  the online editor the instructor used. I have CutemarkEd portable, a free and open source editor. I learned the standard markdown, but also the Github flavored markdown (GFM). A great site I found for this is http://guides.github.com . It has a cheat sheet for markdown in pdf for free download and guides for Github as well.

The editor that I use can render the markup in different styles, such as Github or Solarized. The different style allows the showing of code syntax for example in code blocks (i.e. blockquotes).

Many of the GFM can be rendered with the standard renderer in this application. It had auto-linking of images and tables, which you can do in the code itself or through the new helper built into the editor itself (I haven’t used the helper yet). The HTML view is better than the one in the tutorial because all I have to do is press the F5 button and it shows the code in the rendering window, whereas the tutorial editor showed in a small separate window. The app can also export the markdown to create an HTML or pdf file. I don’t know how much I will use it, but it is another great tool to have at my disposal.

Until next time, keep exploring the jungles of development!

Nodejs and MongoDb setup

I downloaded both packages to my cell phone and transfered them to my external hard drive. Glad I am comfortable with the DOS prompt. I installed Nodejs on this Windows XP machine. Ran a javascript file through the Node server from the command prompt and it worked!

Next I installed MongoDb and while setting up my path (allowing access to) for the local db file, I noticed it was hung up in a loop trying to access a specific port. I went to Windows firewall and granted access to that port. Hit Ctrl-C to stop the process and then executed the command again.

After executing the command and seeing it was no longer looping, I opened up the browser and could access the database page of the localhost! Shutting it down, I once again hit Ctrl-C where it terminated the database file access. Now I can work on the MEAN stack when I’m ready, but now back to more jQuery fun.

Notepad++ error-fixed!

Tried to open default viewer to view a web page from Notepad++ from File->Open in default viewer menu, but kept getting “Another instance of Notepad++ is already running. Please close other instances of Notepad++ before launching Notepad++ portable.” Checked my running processes, but nothing. Found an answer on Stack Overflow website, but trying the direct application link rather than launcher link didn’t correct it. Removed the plug-ins folder and no change.

Found that I had Notepad++ installed on external drive and hard drive of computer. Uninstalled both (no uninstaller in control panel), then had to deleted approximately 100 registry keys due to plug-ins, and then restarted laptop. Reinstalled the desktop application (not portable one) on external drive and had same issue.

What could be causing this? My default browser is Chrome, so I went to open the html file outside application and it came up! Opened Notepad++ and went to Run->Launch in Firefox and voila..the html file opened in the browser! Run-> Launch in IE and it comes up. Now I try Run-> Launch in Chrome and same error! This is probably and issue between a call to the default (chrome in this case) browser through the OS from the application.

I went and changed my default browser back to IE and tried File->Open in default viewer in Notepad++ and it worked. Went to Run->Launch in Chrome and setup Chrome to be default browser again. Now it works in Notepad++ from File->Open in default browser again. This fixed the issue and now I can launch Google chrome as my default viewer again in the application. Hope this helps you solve the problem quicker, if you need it. Until next time, happy coding!

5 common approaches + 1

Five common approaches to learning a programming language.

This is my own summary and additions about an article of the above name on http://www.opensource.com:

  1. Learn by doing– See results before and after. Learn troubleshooting. I am adding tinkering with the code to see what works; whether it is adding/changing a feature, method, input, or output.  
  1. Practice regularly– Frequency creates connections between knowledge and implementation. Amazed at how a particular programmer can just start making a project from scratch? They probably practiced often. An expert learns by rote practice until it becomes almost second nature.
  1. Basics– You must grasp the basic vocabulary and concepts. Knowing that your language is case-sensitive or what type of loop works can go a long way.  What constitutes a skeleton/template to begin the project or how to create a function/method.
  1.  Respect the way a language is used– There is some reason a person decided to solve a problem with a whole new language. Try to learn the why and how (the philosophy so to speak), instead of trying to force your previous experience onto this new tool. Ask an experienced person to review your code and give you a constructive critique.
  1. Build projects– Try to tie passion to your projects by putting your own touch to them. Personalization can help you invest yourself into the process.
  1. Be comfortable with tools before you begin– It is ok if a book or tutorial walks you through using a particular tool or IDE; but if it does not, then use a tool with the lowest learning curve that does the job.

JavaScript Objects

Objects are made up of key:value pairs. Keys are always strings whether you put them in quotes or not. If in quotes then you may have spaces in the name, otherwise they may not have spaces.

To make an object, you var object {}; . You can put items into the object in two ways:

individually, var cat {}; cat[“name”] = “Jack”; cat[“legs”] = 5; cat[“color”] = “purple”;

or  through dot notation, such as cat.name = “Jack”;.

Another way is at the time of creation: var cat = {“name”: ”Jack”, ”legs”: 5, ”color”: ”purple”};.

Browsers may output keys in different order than setup or from one another. Dot notation is only for objects and not arrays.

If you are trying to get 123 out of some array, you can’t with dot notation because of the space in some array name. If the property name has a space, hyphen or begins with a number, you must use brackets.

var myCrazyObject = {

 “name”: “A ridiculous object”,

 “some array”: [7, 9, { purpose: “confusion”, number: 123 }, 3.3],

 “random animal”: “Banana Shark”

};

You can get the value ‘A ridiculous object’ from name with myCrazyObject.name; or myCrazyObject[“name”];

You can’t get to some array with dot notation because of space in string, instead you will get VM5869:2 Uncaught SyntaxError: Unexpected identifier message. You must instead use the bracket notation with myCrazyObject[“some array”]; . To get to the 123 inside some array you would do this…

myCrazyObject[“some array”][2].number; or myCrazyObject[“some array”][2][number]; to get

123.

More learning to come. Stay tuned.

Chrome and JavaScript

Typing javascript programs into the console of the inspect tools use the combination of shift-enter to type new lines into a program without executing it. This can allow you to test small programs using Google’s browser tools.

A string is an array of characters and not all programming languages support this feature (C for example). An array is a group of similar or different objects types grouped together by an index. The index starts at zero and some languages allow starting the index at 1, but not javascript.

Cool methods in javascript are shift and unshift, which allow you to remove or place an item at the front of the array respectively. Push and pop are used to remove or place items at the end of the array.

You can also use multiple methods on the same variable. For example, you have declared a variable name with a string of “jack”. You want to make the first letter a capital, but leave the rest lower case. You can do that by typing name = name.slice(0).toUpperCase(); and you will get the result of “Jack”. The slice method retrieves the first letter “j” then the toUpperCase method transforms the letter to “J” and then returns “J” to the name variable.

Natural Stacking Order

Natural Stacking Order of elements in web pages- Z index property

From lowest to highest:

  1.  Root Element.
  2. Viewport.
  3. Block Level Elements in normal page flow.
  4. Float elements not positioned.
  5. Inline descendant elements in normal page flow.
  6. Positioned elements.

Using z-index property in CSS can change that order.

Web page elements are displayed in the browser in three dimensions. They have an x, y, and z-axis. The z-axis is in and out of the screen. This means that elements stack on top of each other depending on where they are in the html content.

Note: You should not have to use this very often. You may just need to move your element up or down in the html code to change its stack order.