Beginning Programming All-in-One For Dummies. Wallace Wang
Чтение книги онлайн.
Читать онлайн книгу Beginning Programming All-in-One For Dummies - Wallace Wang страница 35
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.
As you can see, both LISP and Prolog offer radically different ways to program a computer compared to C or Java. Just as languages, like C and Java, free you from the tedium of manipulating registers and memory addresses to program a computer, so do LISP and Prolog free you from the tedium of writing explicit step-by-step instructions to program a computer.
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 as a research tool, people have created commercial programs using LISP.
Scripting Languages
Languages, such as C and C++, are often dubbed system programming languages because they can create programs that access and manipulate the hardware of a computer, such as an operating system (for example, Linux or Windows) or a utility program (for example, an antivirus or anti-spyware program). However, using system programming languages, like C++, for everything can get clumsy. Instead of writing an entirely new program from scratch using a system programming language, more people are likely to use an existing program and customize it in some way. Programming languages that customize existing programs are typically called scripting languages.
Scripting languages work with one or more existing programs and act as “glue” that connects different parts of an existing program together. For example, Microsoft Office consists of several programs including a word processor (Microsoft Word), a spreadsheet (Microsoft Excel), and a database (Microsoft Access). By using the scripting language that comes with Microsoft Office, you can write a program that can automatically yank information from an Access database, create a chart from that information in an Excel spreadsheet, and then copy both the data and its accompanying chart into a Word document for printing.
Trying to yank information from a database, create a chart with it, and print the data and chart using a system programming language, like C++ or Java, would mean creating everything from scratch including a database, a spreadsheet, and a word processor. By using a scripting language, you use existing components and simply “glue” them together. The existing components do all the work, while the scripting language just passes the data from one component to another.
Because scripting languages work with existing programs, they differ from traditional programming languages (like C++ or Java) in two important ways:
Because scripting languages work with one or more existing programs, scripting languages are usually interpreted rather than compiled. Therefore, if someone else wants to run your program, written in a scripting language, they need the source code to your program along with all the programs your scripting program needs, such as Microsoft Word and Microsoft Access. As a result, scripting languages are used less to create commercial applications and more to create custom solutions.
To make scripting languages easy to understand and use, even for nonprogrammers, most scripting languages are typeless languages. System programming languages, like C++ and Swift, are strongly typed or type-safe languages. Strongly-typed languages force you to define the type of data your program can use at any given time. So, if your program asks the user to type a name, a strongly typed language makes sure that the user doesn’t type in a number by mistake. This protects a program from accidentally trying to manipulate the wrong type of data, which could crash the program as a result.In comparison, typeless languages don’t care what type of data the program stores at any given time. This makes writing programs much easier because your program assumes if it’s going to yank data from a particular program, such as Microsoft Excel, the data is probably going to be the right “type” anyway, so type-checking would just be restrictive and tedious.
Scripting languages are typically used in four different ways:
To automate repetitive tasks
To customize the behavior of one or more programs
To transfer data between two or more programs
To create stand-alone programs
Automating a program
At the simplest level, scripting languages (also called macro languages) can automate repetitive tasks that essentially record your keystrokes so you can play them back at a later time. For example, if you regularly type the term Campylobacteriosis (a disease caused by the Campylobacter bacteria), you have two choices:
Type the term manually, and hope that you spell it correctly each time.
Type the term just once (the easier solution), record your keystrokes, and use those captured keystrokes to create a scripting language program that you can save and run in the future.
Figure 3-6 shows a scripting language, Visual Basic for Applications (VBA), that has captured keystrokes and saved them in a VBA scripting language program.
FIGURE 3-6: Recording keystrokes automatically creates the equivalent VBA code in Microsoft Word.
Customizing a program
Besides letting you automate a program, scripting languages also let you customize a program, which can make the program easier to use. For example, you may have a spreadsheet that calculates your company’s invoices. However, to use this spreadsheet, you need to know the specific place in the spreadsheet to type new invoice information. Type this information in the wrong place, and the spreadsheet doesn’t work right.
To avoid this problem, you can write a program in a scripting language that can display a window with boxes to type in new invoice information. Then the scripting language program automatically plugs that new information in the correct place in the spreadsheet every time.
For even more power, a scripting language can combine automation with customization to make programs perform tasks on their own. By using VBA in Microsoft Office, you could write a VBA program that tells your computer to copy data from