Beginning Programming All-in-One For Dummies. Wallace Wang. Читать онлайн. Newlib. NEWLIB.NET

Автор: Wallace Wang
Издательство: John Wiley & Sons Limited
Серия:
Жанр произведения: Программы
Год издания: 0
isbn: 9781119884422
Скачать книгу
you know at least one curly-bracket language, you know one of the most popular programming languages in the world.

      Programming languages, such as C, are often considered procedural or functional languages because they divide a large program into separate procedures or functions that tell the computer how to solve a problem step-by-step.

      Although telling the computer what to do step-by-step might seem like the most logical way to program a computer, another way to program a computer is by using a declarative language. Instead of describing how to solve a problem, declarative programming languages describe

       Facts: Information about the problem

       Rules: Relationships between this information

      By using facts and rules, programs written in declarative languages can literally figure out an answer on their own without being told explicitly how to do it.

Ultimately, every program, including those written in declarative languages, must get translated into machine language. That means every program must eventually tell the computer how to solve a problem step-by-step. Declarative languages simply free you from having to describe these steps to the computer.

      The most popular declarative programming language is Prolog (short for Programming in Logic). A typical Prolog fact might look like this:

       father("Sally", "Jesse").

      The preceding fact tells the computer that Jesse is the father of Sally. Now if you want to know who the father of Sally might be, you could ask the following:

       ?- father("Sally", X).

      Using the fact that earlier stated that the father of Sally was Jesse, the preceding Prolog command would simply return:

       X = "Jesse".

      At this point, Prolog simply uses a predefined fact to come up with an answer. Notice that even in this simple example, no instructions told the Prolog program how to use the fact that Jesse is the father of Sally.

      A list of facts by themselves can be made more useful by including rules that define relationships between facts. Consider the following Prolog program that defines two facts and one rule:

       father("Jesse", "Frank").father("Sally", "Jesse").grandFather(Person, GrandFather) :- father(Person, Father), father(Father, GrandFather).

      The two facts tell the computer that Frank is the father of Jesse, and Jesse is the father of Sally. The grandfather rule tells the computer that someone is a grandfather if they’re the father of someone’s father.

      Suppose you typed the following Prolog command:

       ?- grandFather("Sally", Y).

      The Prolog program tells the computer to use its known facts and rules to deduce an answer, which is:

       Y = "Frank".

      In other words, Frank is the grandfather of Sally. (Frank is the father of Jesse, and Jesse is the father of Sally.)

      Just from this simple example, you can see how different a Prolog program works compared to a program written in C or Java. Instead of telling the computer how to solve a problem, declarative programming languages let you state the facts and the rules for manipulating those facts so the computer can figure out how to solve the problem.

      

A Prolog program can actually create additional facts (and delete old facts) while it’s running, so it can appear to think. That’s why Prolog is commonly used in the field of artificial intelligence (AI). The whole idea behind AI is to make computers smarter and literally think for themselves. (That’s because computer scientists have pretty much given up hope that people will ever get smarter or begin to think for themselves.)

      

Just as knowing two or more human languages can help you better understand how people communicate, so can knowing two or more drastically different programming languages help you better understand how programming can work. The key is to figure out two different programming languages, like C++ and Prolog. Knowing two similar programming languages, like C++ and C#, won’t show you much of a difference.

      One of the most popular programming languages favored by the AI community is LISP (which stands for LISt Processing). The basic idea behind LISP is that everything is a list that can be manipulated by the computer. For example, a typical LISP command might look like this:

       (print "Hello world")

      This LISP command is a list that displays the following onscreen:

       "Hello world"

      The enclosing parentheses define the start and end of a list. A different way to print "Hello world" onscreen would be to use this LISP command:

       (list "Hello world")

      The preceding command would print the following:

       ("Hello world")

      In this case, the list command tells the computer to treat "Hello world" as a list, so it encloses it in parentheses. Now consider what happens if you insert a command (list) inside another command (list):

       (list (print "Hello world"))

      This is how the preceding LISP command works:

      1 The innermost command (list) runs first, which is the (print "Hello world") list.This displays the following onscreen:"Hello world"From the computer's point of view, the original LISP command now looks like this:(list "Hello world")

      2 This command now displays the following onscreen: ("Hello world")

      So, the command

       (list (print "Hello world"))

      prints the following:

       "Hello world"("Hello world")

      In the previous example, LISP treats the (print "Hello world") list first as a command (to print "Hello world" onscreen) and then as data to feed into the list command to display the list ("Hello world") onscreen.

      With traditional programming languages, like C or Java, commands and data are separate where data may change but commands never change. With LISP, a list can be both a command and data. That makes it possible for a program to change its lists (treated either as data or as a command), essentially allowing a program to modify itself while running, which can mimic the learning and thinking process of a human being.

      

Although the idea that a LISP program can modify its own commands might seem like science fiction, LISP is actually the second-oldest programming language still in use today. (Fortran is the oldest programming language still in popular use.) LISP was invented in 1958, and although it's been used primarily